README 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. PHP-gettext 1.0 (https://launchpad.net/php-gettext)
  2. Copyright 2003, 2006, 2009 -- Danilo "angry with PHP[1]" Segan
  3. Licensed under GPLv2 (or any later version, see COPYING)
  4. [1] PHP is actually cyrillic, and translates roughly to
  5. "works-doesn't-work" (UTF-8: Ради-Не-Ради)
  6. Introduction
  7. How many times did you look for a good translation tool, and
  8. found out that gettext is best for the job? Many times.
  9. How many times did you try to use gettext in PHP, but failed
  10. miserably, because either your hosting provider didn't support
  11. it, or the server didn't have adequate locale? Many times.
  12. Well, this is a solution to your needs. It allows using gettext
  13. tools for managing translations, yet it doesn't require gettext
  14. library at all. It parses generated MO files directly, and thus
  15. might be a bit slower than the (maybe provided) gettext library.
  16. PHP-gettext is a simple reader for GNU gettext MO files. Those
  17. are binary containers for translations, produced by GNU msgfmt.
  18. Why?
  19. I got used to having gettext work even without gettext
  20. library. It's there in my favourite language Python, so I was
  21. surprised that I couldn't find it in PHP. I even Googled for it,
  22. but to no avail.
  23. So, I said, what the heck, I'm going to write it for this
  24. disguisting language of PHP, because I'm often constrained to it.
  25. Features
  26. o Support for simple translations
  27. Just define a simple alias for translate() function (suggested
  28. use of _() or gettext(); see provided example).
  29. o Support for ngettext calls (plural forms, see a note under bugs)
  30. You may also use plural forms. Translations in MO files need to
  31. provide this, and they must also provide "plural-forms" header.
  32. Please see 'info gettext' for more details.
  33. o Support for reading straight files, or strings (!!!)
  34. Since I can imagine many different backends for reading in the MO
  35. file data, I used imaginary abstract class StreamReader to do all
  36. the input (check streams.php). For your convenience, I've already
  37. provided two classes for reading files: FileReader and
  38. StringReader (CachedFileReader is a combination of the two: it
  39. loads entire file contents into a string, and then works on that).
  40. See example below for usage. You can for instance use StringReader
  41. when you read in data from a database, or you can create your own
  42. derivative of StreamReader for anything you like.
  43. Bugs
  44. Report them on https://bugs.launchpad.net/php-gettext
  45. Usage
  46. Put files streams.php and gettext.php somewhere you can load them
  47. from, and require 'em in where you want to use them.
  48. Then, create one 'stream reader' (a class that provides functions
  49. like read(), seekto(), currentpos() and length()) which will
  50. provide data for the 'gettext_reader', with eg.
  51. $streamer = new FileStream('data.mo');
  52. Then, use that as a parameter to gettext_reader constructor:
  53. $wohoo = new gettext_reader($streamer);
  54. If you want to disable pre-loading of entire message catalog in
  55. memory (if, for example, you have a multi-thousand message catalog
  56. which you'll use only occasionally), use "false" for second
  57. parameter to gettext_reader constructor:
  58. $wohoo = new gettext_reader($streamer, false);
  59. From now on, you have all the benefits of gettext data at your
  60. disposal, so may run:
  61. print $wohoo->translate("This is a test");
  62. print $wohoo->ngettext("%d bird", "%d birds", $birds);
  63. You might need to pass parameter "-k" to xgettext to make it
  64. extract all the strings. In above example, try with
  65. xgettext -ktranslate -kngettext:1,2 file.php
  66. what should create messages.po which contains two messages for
  67. translation.
  68. I suggest creating simple aliases for these functions (see
  69. example/pigs.php for how do I do it, which means it's probably a
  70. bad way).
  71. Usage with gettext.inc (standard gettext interfaces emulation)
  72. Check example in examples/pig_dropin.php, basically you include
  73. gettext.inc and use all the standard gettext interfaces as
  74. documented on:
  75. http://www.php.net/gettext
  76. The only catch is that you can check return value of setlocale()
  77. to see if your locale is system supported or not.
  78. Example
  79. See in examples/ subdirectory. There are a couple of files.
  80. pigs.php is an example, serbian.po is a translation to Serbian
  81. language, and serbian.mo is generated with
  82. msgfmt -o serbian.mo serbian.po
  83. There is also simple "update" script that can be used to generate
  84. POT file and to update the translation using msgmerge.
  85. TODO:
  86. o Improve speed to be even more comparable to the native gettext
  87. implementation.
  88. o Try to use hash tables in MO files: with pre-loading, would it
  89. be useful at all?
  90. Never-asked-questions:
  91. o Why did you mark this as version 1.0 when this is the first code
  92. release?
  93. Well, it's quite simple. I consider that the first released thing
  94. should be labeled "version 1" (first, right?). Zero is there to
  95. indicate that there's zero improvement and/or change compared to
  96. "version 1".
  97. I plan to use version numbers 1.0.* for small bugfixes, and to
  98. release 1.1 as "first stable release of version 1".
  99. This may trick someone that this is actually useful software, but
  100. as with any other free software, I take NO RESPONSIBILITY for
  101. creating such a masterpiece that will smoke crack, trash your
  102. hard disk, and make lasers in your CD device dance to the tune of
  103. Mozart's 40th Symphony (there is one like that, right?).
  104. o Can I...?
  105. Yes, you can. This is free software (as in freedom, free speech),
  106. and you might do whatever you wish with it, provided you do not
  107. limit freedom of others (GPL).
  108. I'm considering licensing this under LGPL, but I *do* want
  109. *every* PHP-gettext user to contribute and respect ideas of free
  110. software, so don't count on it happening anytime soon.
  111. I'm sorry that I'm taking away your freedom of taking others'
  112. freedom away, but I believe that's neglible as compared to what
  113. freedoms you could take away. ;-)
  114. Uhm, whatever.