ResourceBundle Editors
I have decided to use ResourceBundles to provide translations for my php application. While the ResourceBundle (http://www.php.net/manual/en/class.resourcebundle.php) functionality is quite new, I prefer the way it works over gettext.
开发者_运维技巧The question I have is that are there any good ResourceBundle editors? In particular I am looking for one that will scan all my source files and generate a list of message IDs that require translations and/or updating.
Previously, I have used POEdit to generate translation files for gettext. It is able to scan my source files for functions such as _() and hand me a list of message ids to translate.
I have tried installing an eclipse plugin (http://sourceforge.net/projects/eclipse-rbe/) and while it has a nice GUI, it doesn't scan my source files to generate a list of message IDs to translate.
Could anyone recommend any resource bundle editors?
After spending most of the day trying out loads of different tools and what not, I have devised this workflow, and howpfully it will help others as well.
ResourceBundles are quite new to PHP and there isn't really much information. Surprisingly, while resource bundles have been used by various languages such as Java for quite a while, I wasn't able to find any tools that can deal with RBs in a general manner.
SirDarius's suggestion to try RB Manager was a good start. It's straight forward to use, but there are some issues:
- To scan your source code, you need to set up the scanner using an xml file, which might not be intuitive.
- More importantly, the scanner will report a list of unused keys and new keys. These still need to be manually added into your resource bundles using the RB Manager main application.
- Finally, the ICU files exported by RB Manager are broken. Where as content should be exported as just content by itself, somehow RB Manager converts them into the decimal equivalent of the ASCII code. This makes the file to be unuseable.
I have tried various tools to convert between formats, namely the XLIFF format, but I find that the XLIFF files generated are often malformed, and the next piece of software would refuse to process it.
For those who might run into this issue in the future, here is what I have done:
I am assuming that you will have some classes or function to wrap around the MessageFormatter and ResourceBundle classes. In my application, I use something like
$translate->_('text');
to perform translation. The trick is to use POEdit. POEdit will scan your source files for _() and get a list of keys and remove old keys. Remember that in MessageFormatter, you only use a message id, for example system.warning.reason instead of a full string "Your action was denied. This has been logged".You should then use POEdit to write your translations. Dealing with plurals is a bit different. You should not set up any rules for dealing with plurals. Dealing with plurals is done inline for the translation string, which is really flexible. See here: http://userguide.icu-project.org/formatparse/messages for some examples.
Once your translation has been completed, I wrote a small PHP script to convert the .mo files generated by POEdit into the equivalent ICU files.For parsing the .mo files, I used the gettext adapter in Zend_Translate. It also contained a function to grab all message IDs and messages, which is extremely useful. You then convert that data into ICU format like so:
root { // message ID {" Pattern "} system.warning.reason { "Your action was denied. This has been logged" } }
Once this is done, you need to download the ICU package from : http://site.icu-project.org/download. In the bin directory, you will find an executable called genrb, which compiles the resourcebundle into a binary for PHP to use.
The command is genrb inputfile.txt -e UTF-8
notice that the input encoding is specified as UTF-8. You can change this to whatever encoding your input files uses.
That's it. I believe this is the simpliest and most productive workflow when generating and translating resourcebundles for PHP.
If anyone comes up with a better way, or perhaps even a full standalone program to take care of this, please post a comment!
You can try this: http://icu-project.org/download/rbmanager.html
The Resource Bundle Manager written for the ICU project on which PHP resource bundle class is based.
精彩评论