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.
Related
I'm writing a webapp in german, so all buttons,text,tooltips etc. are in german for now. But I want to use some kind of template file for the webapp so I can quick change to another language if needed. I thought about textfile that I explode with "\n" and load into a sessionvariable to have always all text the user will need in his session. An other approach would be to parse such a file i.e. a XML document like this:
<?xml version="1.0"?>
<phrase>
<placeholder></placeholder>
<value></value>
</phrase>
where every field has it's own name/value that represents a textsnippet or button or whatever on the website, and then cast it into an object an cache it for everyone. I think the second approach is the best for working with multiple languages for a webapp. Anybody perhaps some pointers what I could do even better, or just post how you did this kind of languagetemplating for mutlinational webpages/webapps in the past.
Since you are looking for a translation solution, I understand you don't use a framework to develop your site, since most of them provide you with solutions to handle translations.
Most frameworks and apps I've seen in php use arrays, where the original sentence is the key and the translation is the value. So, to make easier to translate it to several languages, the key is in english.
In case you use gettext as suggested, or another aproach, it'll be useful also to parse your code to catch all strings to be translated automatically, since it can be a mess doing it manually when the base code grows and you want to keep up to date your translations.
Take a look at GNU Gettext, its very handy for multilanguage support.
The main idea is that you just wrap your words or phrases into a function, like
echo _g('Hello');
so you do not have any engine changes. You will have to add translation files for each language you are using.
You've come up with 2 solutions for storing the data, but I suggest you need to think further about the architecture and take a more complete view of the lifecycle of each request.
Regarding architecture: neither solution scales up to describe an extensive vocabulary very well - although for one or 2 pages it will suffice. The alternative approach, to manage a translation database (such as gettext) which might be overkill - and performs less optimally with small numbers of pages but importantly performance does not deteriorate significantly with large/multiple dictionaries. A compromise solution might be to have a dataset for each URL/language (which might be extracted from a consolidated database).
If it were me, I would not use either method you proposed for storing the data: parsing XML creates a sginficant overhead for each page request : using \n as a delimiter precludes the use of \n within a translation. Using a serialized PHP array seems to be the least expensive solution.
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 have an existing, database driven PHP application with around 40 pages printing an unknown (but rather large) number of English strings. The strings are currently all hard coded. There is also a set of static documentation pages. I now need to add language support to this application.
As far as I can tell gettext seems to be the standard solution to this problem, but gettext feels very much like a "hack" to me. I am also not certain about the additional overhead (both in development and run-time) it will cause. Are there any other solutions or frameworks that could be better suited to my requirements? Any best practices or pitfalls I should be aware of when starting this project?
gettext is the "standard" for multi-lingual text storage, but in the end, it is just a storage engine. It does nothing to put the proper text into your page. You need to abstract the text from your View so the proper text can be inserted.
In the end, you need a way to place the language text in your document. This means "tagging" the text in a certain way so that it can be searched and replaced by with desired language text. For the e-commerce site I manage (3 languages) I used a technique derived from Facebook FBML tagging system.
You can wrap your text in "tags" <trans id="slt">something like this</trans>. Then use the DOM tools in PHP to extract the ID, look up the translated text based on the id, replace the content between the tags with the proper language text. You can still use gettext for your storage mechanism, or your database. Your browser will ignore the tags, so your page will still look fine during development.
This is just one tagging example. You can use any tagging mechanism and use grep instead to extract, search and replace.
For static pages, you can pre-generate the translated versions and load the appropriate language version. This way there is no extra overhead for different languages.
I'm looking to i18n-ize a web app. The site will be constantly changing: text will be rewritten, new stuff added etc. The web app is written in PHP, but the same applies to any language.
Basically I want:
1) The code to be readable and maintainable
2) Translators to be sent an email when new stuff is added in English OR the English is changed
3) To know whether something is up-to-date or not.
4) Translators be able to update things online
I guess the best idea is to store everything in a database and handle things that way rtaher than PO files and gettext. But what's the best way:
$lang('contactus') has the disadvantage of being unreadable (code-wise), and slower to develop (as all English needs to be given a unique key and stored in the database)
$lang('Please contact us for more information') is readable and quicker, but if the English changes (typo, grammar edit, updated) then the translation disappears entirely.
How do other apps/frameworks handle it?
Avoid storing strings that need to be translated in the database. Did that myself and regretted it. Use external files that you can send to translators. Use strings in your preferred language as the keys. So as for your example $lang('Please contact us for more information') is the way to go. A text change in the English probably means a corresponding change in the translations so yeah it's a maintenance headache.
Plus there's more to it than just string translations. There are currency symbols and formats, number formats (decimal, digit grouping symbols, where the symbol appears), date formatting. Name formatting - in some locales people often have middle names in others they usually don't. Text reading left to right vs right to left as well. ugh.
For a web app it's sometimes just much simpler to have the web pages separated into language-locale directories and deal with it that way. You do have all you business logic separate from the view code right? This is where embedding business logic directly within html ala the typical PHP approach really starts to hurt.
Make some translation function with an easy to type name, for example t($key) or use existing solution (for example Zend_Translate) or PHP gettext, that provides the _($key) function.
The format of your translation files should not matter so much - whatever fits your translation process best.
Use your original language, for example English, for the key. For longer strings it makes sometimes sense to invent artificial keys, for example "introduction_text_1"
Never use constants or any shortened translation keys, don't use the database, avoid using just arrays. The only professional way to handle translations is a gettext(-like) function.
You should only use _("Original english text queries"). This has the advantage that at least the default language text is still available, should translation data/files be inaccessible.
Don't worry about changes in the text strings. In reality this is rare. If you use the gettext syntax, there are even tools to adapt the language files automatically then. Not many good tools, mind you. But more than for homebrew translation methods, and it gets the job done. If your PHP interpreter doesn't support native gettext, search for the "php-gettext" emulation or "upgradephp".
I'm developing a website that will be available in different languages. It is a LAMP (Linux, Apache, MySQL, PHP) setup, and it makes use of Smarty, mostly for the template engine.
The way we currently translate is by a self-written smarty plugin, which will recognize certain tags in the HTML files, and will find the corresponding tag in an earlier defined language file.
The HTML could look as follows:
<p>Hi, welcome to $#gamedesc;!</p>
And the language file could look like this:
gamedesc:Poing 2009$;
welcome:this is another tag$;
Which would then output
<p>Hi, welcome to Poing 2009!</p>
This system is very basic, but it is pretty hard to control, if I f.e. would like to keep track of what has been translated so far, or give certain users the rights to translate only certain tags.
I've been looking at some alternative ways to approach this, by either replacing the text-file with XML files which could store some extra meta-data, or by perhaps storing all the texts in the database, and retrieving it there.
My question is, what would be the best way to make this system both maintainable and perform well with high user-traffic? Are there perhaps any (lightweight) plugins I could take a look at?
You could give a shot at gettext. It is the way it is done in most C/C++ linux applications and it is an extension to PHP too. The idea is not very different from what you're already doing, but there are tools that ease the mantainance of translations (i.e. poedit).
For user rights to translations, gettext won't be of much help, I think you'll need to do it on your own or look at some frameworks if they have smarter solutions.
Maybe taking a look to gettext lib could help you get some hints http://php.net/manual/en/book.gettext.php hope it helps!
You will need to have a table in your database that you can use to store strings of text, each with an composite ID. the composite ID will be made up of language ID and text node ID.
You will need to give the user a chance to select a preferred language. You should make sure that you either have a default "this has not been translated" for every language you use, or a default language that your entire site can be vied in.
For every bit of text with in your web site, rather then store the text with in the page, you just assign it an ID.
When serving the page, look up the text node ID and preferred language ID and load that string of text, or the string for the default.
in our project, http://pkp.sfu.ca/ojs, we use XML files to store translation key-value pairs. Browse our code: http://github.com/pkp/pkp-lib/blob/master/classes/i18n/PKPLocale.inc.php
We use that class to read the XML files for each locale and in our code we use Locale::translate('locale.key.name');. Similar to gettext, but using an XML file for easier updating.
Looking around at web stuff today I came across this website: http://translateth.is/
It looks simple to use... copy paste in some javascript.