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.
Related
I need to generate .po files for the Php code of my web application. This is a very large application that needs to be translated to
several different languages. So far, I have been using PoEdit in order to generate my .po files. The problem lies in that many
of my files lack the Gettext notation echo _("message") and in the past I only used echo "message".
This is what I think it could be the best solution for my issue:
Create a script that scans my Php code and tells me which of my messages are being and not being displayed using Gettext. How would I do this?
Replace those string not using Gettext with the appropriate Gettext pattern.
Can you please advise me what is the best aproach in order to get all my code using Gettext that I should look to?
You have one way how to convert echoing messages without translation by gettext to echoing messages with translation by gettext:
If you know that all messages are represented by only one variable, for example $Message, following way will be made relatively fast, else you would have to find all used echoing of any messages ... and do that manually (mostly if message is only one - and it is represented by text, not by variable holding that text).
In your editor to start global search (in all files) and then, to go file by file and use replacing - and set $Message for search and _($Message) for replacement ... unless your editor allows replacing in more files at the same time. Then you would do all replacement at one time.
I would not suggest to do this replacing directly in php.
I need to translate several PHP files (HTML Code + PHP Tags) into another language.
Google Translator's Kit allows this, but clears the PHP Tags, erases class="" attributes (?!) and adds html, head tags & what not. Completely useless.
How can I (ideally in batch) translate these files using any kind of automated translation service?
Thanks.
bmargulies is the most clear-cut way of doing it. However, it takes times.
If you're in a pinch, or want to cut corners, a relatively simple way to do it is to use regular expressions to filter your code out yourself. Match over multiple lines (/s flag in preg), store the match, and replace with a hash. Any hash. Just make sure it doesn't map to anything in any language.
Do the same for HTML tags if they are proving to be annoying to Google.
Translate with Google.
Replace back the hashes. Voila! Job done! If you're feeling even more daring, instead of replacing the hashes back, replace them with an l18n-suitable structure might prove to be even more worthwhile.
You need to internationalize the code. You need to move all the translatable strings out into a separate file, so that you can shove that through Google and then easily drop in the results.
Researching the topic of PHP I18N will prove rewarding.
Google Translator toolkit is for documents - not so much for source code. You can organize your program's strings as documents and translate them in Google Translator toolkit, and there are, in fact, software projects that do it, but it's contrived. It would be much better to use a different method, as the other people here say.
Put the translatable strings in separate files - you can use something like YAML or JSON, for example, or to just organize your strings as PHP arrays (that's how it's done in MediaWiki, for example). Each message should have a key. Use one file per language or one file with all the languages, and the strings grouped by languages. (By the way, use ISO 639-3 language codes - don't make up your own. Then you'll be able to reuse them in HTML lang attributes.)
After you organized your strings like that, write functions that load the strings from these files by message key and language code, and use these functions to display the messages - never use hardcoded strings.
Finally, put your files up for translation using software such as Pootle, Transifex, Zanata, or the MediaWiki Translate extension.
(Disclaimer: I am a developer of the MediaWiki Translate extension.)
Using something like Gettext (namely php-gettext) is IMHO best approach to do that. Another widely used option is to simply extract strings to separate files (be it PHP or JSON) and translate these. However I'd recommend to use Gettext as you will be using standard format with wide range of available tools.
I did some search about localization files, see that we can use .po file or gettext but, is there any tutorial or sample of a unique solution that will work both in javascript and in php.
I want to only maintain one localization file per language that will work with both JS and PHP languages.
Would appreciates if someone can point me to some links or samples...
I found that it is typically a sign of a questionable design when translatable text is coded inside JS functions. JS is meant to implement logic, not content. The content should be provided by PHP (typically by using a templating engine) and should be used by JS. That way you only need a localization solution for PHP.
If (exceptions always occur) you really need to translate a phrase inside a JS routine you use an ajax call to fetch the translation. This also simplifies the access to the dictionary holding the translation tokens since it is again done by PHP. The dictionary can be kept in a single place.
Yep, there is. I've successfully used gettext.js a while ago, which is operating on .json or .po files. This way, you only have to maintain one translation source. The webpage I've used this for is located here so you can check out how I've did it. Good luck!
First, try to avoid gettext if you can. It's popular and stable, but it puts some burden on the translations maintenance: you will need to change the source strings, and when this happens, gettext needs to update all the message keys, and this may mess up the existing translations. An approach with constant message keys is much easier to maintain in the long run - you will need to remember to delete the keys you don't use any more, but it's a very small burden.
You can use the same translations storage format for PHP and JavaScript. If you use a key-based approach, as I suggest, it will probably be some JSON-based format. JSON is easily accessible in both PHP and JavaScript.
There are several ready-made JavaScript libraries for JSON-based internationalization. I happen to be a developer of one such library: https://github.com/wikimedia/jquery.i18n . It should be reasonably easy to adapt it to PHP.
I have been working on a site from last 2 years.
Now my client want's make it bilingual, with English and Chinese.
Any idea what should I do for it?
You can have seperate constant files, each file has a bunch of constants in it, then you go through the pages replacing actual sentences with the constants.
Have a cookie that selects the correct constants file that the user has chosen.
This sort of design is good as it allows for more language to be added easily in the future.
It is however a pain in the ass to do.
You could go for some sort of automated translator but it wont read naturally.
I had to do this and since you are working with PHP, I recommend gettext
http://php.net/manual/en/book.gettext.php
And here's an article to get you started on it
http://mel.melaxis.com/devblog/2005/08/06/localizing-php-web-sites-using-gettext/
The benefit of gettext is that the translator never has to touch the code. Rather, the translator works by translating .po files with Poedit ( http://www.poedit.net/ ). You place those .po files in your locale directories from which php-gettext accesses and uses it to replace the strings. Have fun and good luck, I got it to work pretty well
Decent introduction article on the subject:
http://onlamp.com/pub/a/php/2002/11/28/php_i18n.html
We could really do with more info.
I'm writing a WordPress plugin to create an eBook from a selected category in most major eBook formats. I would like to support MobiPocket since that's the format used by the Kindle but I'm not sure how to go about it. From what I've read .mobi files are actually Palm Resource Databases (PRC) but I haven't been able to find a PHP class to work with these.
I thought about using exec along with KindleGen but that would be undesirable as it would complicate initial setup. I've also thought about hosting a web service somewhere and using XML-RPC to accomplish this but that also complicates things.
My question is: is there a PHP class/library (PHP-only preferred) that can work with PRC or even better, a class that specialises in creating MobiPocket ebooks? (needs to be open source since I'm releasing under the GPL)
I've tried searching but haven't been able to find anything.
I don't know whether you're still looking for this PHP library, but just in case: https://github.com/raiju/phpMobi. This is a library that creates mobi files from html files.
It's should still be seen as an experimental version, but it should work without a problem for basic document with a few images.
Unfortunately not; however, the binary compiled format is an open specification available at:
http://www.mobipocket.com/dev/article.asp?BaseFolder=prcgen
The only direct way of transforming the uncompiled format is using the native XML functionality of PHP to create them and then invoking a compiler with exec, which I understand you don't want to do. If you go with this route, the link above also has details about this XML format.
You might want to try the mobiperl tools,
https://dev.mobileread.com/trac/mobiperl/wiki
Please note I haven't tested them yet. But they have been
around since at least 2007 so they should work well by now.
google "Mobiperl - Perl tools for handling MobiPocket files" to
find a thread on mobileread board discussing it. As a new
poster I can't put 2 hyperlinks into my reply.
Another tool I have recently found (but not yet tested), is: http://www.phpclasses.org/package/8173-PHP-Generate-Kindle-ebook-file-in-mobi-format.html#files
It is based upon KindleGen, and looks pretty straight forward to implement.