README 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. PHP-gettext 1.0
  2. Copyright 2003, 2006 -- 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. Plural-forms field in MO header (translation for empty string,
  45. i.e. "") is treated according to PHP syntactic rules (it's
  46. eval()ed). Since these should actually follow C syntax, there are
  47. some problems.
  48. For instance, I'm used to using this:
  49. Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : \
  50. n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
  51. but it fails with PHP (it sets $plural=2 instead of 0 for $n==1).
  52. The fix is usually simple, but I'm lazy to go into the details of
  53. PHP operator precedence, and maybe try to fix it. In here, I had
  54. to put everything after the first ':' in parenthesis:
  55. Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : \
  56. (n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);
  57. That works, and I'm satisfied.
  58. Besides this one, there are probably a bunch of other bugs, since
  59. I hate PHP (did I mention it already? no? strange), and don't
  60. know it very well. So, feel free to fix any of those and report
  61. them back to me at <danilo@kvota.net>.
  62. Usage
  63. Put files streams.php and gettext.php somewhere you can load them
  64. from, and require 'em in where you want to use them.
  65. Then, create one 'stream reader' (a class that provides functions
  66. like read(), seekto(), currentpos() and length()) which will
  67. provide data for the 'gettext_reader', with eg.
  68. $streamer = new FileStream('data.mo');
  69. Then, use that as a parameter to gettext_reader constructor:
  70. $wohoo = new gettext_reader($streamer);
  71. If you want to disable pre-loading of entire message catalog in
  72. memory (if, for example, you have a multi-thousand message catalog
  73. which you'll use only occasionally), use "false" for second
  74. parameter to gettext_reader constructor:
  75. $wohoo = new gettext_reader($streamer, false);
  76. From now on, you have all the benefits of gettext data at your
  77. disposal, so may run:
  78. print $wohoo->translate("This is a test");
  79. print $wohoo->ngettext("%d bird", "%d birds", $birds);
  80. You might need to pass parameter "-k" to xgettext to make it
  81. extract all the strings. In above example, try with
  82. xgettext -ktranslate -kngettext:1,2 file.php
  83. what should create messages.po which contains two messages for
  84. translation.
  85. I suggest creating simple aliases for these functions (see
  86. example/pigs.php for how do I do it, which means it's probably a
  87. bad way).
  88. Usage with gettext.inc (standard gettext interfaces emulation)
  89. Check example in examples/pig_dropin.php, basically you include
  90. gettext.inc and use all the standard gettext interfaces as
  91. documented on:
  92. http://www.php.net/gettext
  93. The only catch is that you can check return value of setlocale()
  94. to see if your locale is system supported or not.
  95. Example
  96. See in examples/ subdirectory. There are a couple of files.
  97. pigs.php is an example, serbian.po is a translation to Serbian
  98. language, and serbian.mo is generated with
  99. msgfmt -o serbian.mo serbian.po
  100. There is also simple "update" script that can be used to generate
  101. POT file and to update the translation using msgmerge.
  102. Interesting TODO:
  103. o Try to parse "plural-forms" header field, and to follow C syntax
  104. rules. This won't be easy.
  105. Boring TODO:
  106. o Learn PHP and fix bugs, slowness and other stuff resulting from
  107. my lack of knowledge (but *maybe*, it's not my knowledge that is
  108. bad, but PHP itself ;-).
  109. (This is mostly done thanks to Nico Kaiser.)
  110. o Try to use hash tables in MO files: with pre-loading, would it
  111. be useful at all?
  112. Never-asked-questions:
  113. o Why did you mark this as version 1.0 when this is the first code
  114. release?
  115. Well, it's quite simple. I consider that the first released thing
  116. should be labeled "version 1" (first, right?). Zero is there to
  117. indicate that there's zero improvement and/or change compared to
  118. "version 1".
  119. I plan to use version numbers 1.0.* for small bugfixes, and to
  120. release 1.1 as "first stable release of version 1".
  121. This may trick someone that this is actually useful software, but
  122. as with any other free software, I take NO RESPONSIBILITY for
  123. creating such a masterpiece that will smoke crack, trash your
  124. hard disk, and make lasers in your CD device dance to the tune of
  125. Mozart's 40th Symphony (there is one like that, right?).
  126. o Can I...?
  127. Yes, you can. This is free software (as in freedom, free speech),
  128. and you might do whatever you wish with it, provided you do not
  129. limit freedom of others (GPL).
  130. I'm considering licensing this under LGPL, but I *do* want
  131. *every* PHP-gettext user to contribute and respect ideas of free
  132. software, so don't count on it happening anytime soon.
  133. I'm sorry that I'm taking away your freedom of taking others'
  134. freedom away, but I believe that's neglible as compared to what
  135. freedoms you could take away. ;-)
  136. Uhm, whatever.