E
E
etc2012-01-15 09:46:38
Internationalization and localization
etc, 2012-01-15 09:46:38

Implementing multilingualism in PHP?

Good day! I recently started working on an idea based on PHP + MVC. Recently, I thought about supporting a second language (the first Russian, the second, respectively, English) and realized that I don’t know how to implement it optimally. My knowledge of this you is at the level of the 00s, when they used just constants, gettext, and something like that everywhere.
I would be glad if someone opens their eyes to modern methods of implementing multilingualism on the site and tells you where to dig. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

9 answer(s)
F
FrostMoon, 2012-01-15
@FrostMoon

Personally, I prefer i18n. For each module, we create our own language file containing all the words\phrases\labels used in this module.
ru.ini:
[first]
0 = "Hello World!"
en.ini:
[first]
0 = "Hello World!"
directly in the template itself (for example, smarti) we write:
...
{_ first}
...
Do not forget to process the prefix in the url through .htaccess:
site.ru/ru/
site.ru/en/
or otherwise track the selected locale.
As a result, we see a phrase in the language we need.

A
Alexey Sundukov, 2012-01-15
@alekciy

Nothing has really changed since 00. If you read the comments, it becomes obvious that some people reinvent the wheel in their projects and end up with gettext. In general, they did it, they did it, and the i18n module turned out to be gettext.
I would recommend that. gettext is used for rarely changing information. Menu, various inscriptions to do through it. At the same time, we get a number of advantages:
1) The format has existed for many years and works on a huge variety of platforms. And this means that it will be understandable to anyone who knows about it, even if the person is not familiar with the language in which the system is written. Those. all the advantages of standards. With bicycles, you still have to figure out how it works.
2) Caching. gettext is actively cached in RAM.
3) Abundance of software for translators.
4) Support for plurals. Not all bikes think about it the way it is done in gettext.
5) Gettext support in template engines.
6) You can work with gettext not only from PHP. Relevant for heterogeneous projects.
Well, I recommend moving the content part to the database level. Personally, I use a separate table for each language. A plus compared to storing in a separate column is the absence of ALTERs when adding / removing a language.

M
max_rip, 2012-01-15
@max_rip

I like the option when the original language is called first, and then the current language, if it differs from the original. Thus, if there is no translation, then you can get at least something. Although gettext is successfully used in php (for example in phpmyadmine), it is more difficult to enter it. Utilities are required, of which there are plenty for various systems.
$lang['mTitle']='Главная';
$lang['mAbout']='О нас';

P
Push_Ok, 2012-01-15
@Push_Ok

see how it's done in yii. there the file of the necessary language is connected. and the output is made through
yii::t(' file_name','token',array_replacements);

L
Lachezis, 2012-01-15
@Lachezis

I am a supporter of string constants, i.e. constructs like i18n::get('Hello %s!',$username), or l('Hello %s',$username) for short. But I'm using namespaces for strings, not global storage. It is implemented very simply (opened, read, added, saved), and it is convenient that a new line is automatically added to the namespace and saved.
For myself, in any i18n I consider it important to be able to export and import localizations (I used CSV before, but due to problems with UTF I switch to PO), word-formation support for various languages ​​​​(1 apple, 2 apples, 6 apples), caching and accelerated (via meta construct) localization of view files.
Unfortunately, all this is part of the framework, but on request I can send files with explanations.

D
Dmitry, 2012-01-15
@StraNNikk

Plus one to gettext! To mine it is most that on is unix-way. Support for plurals through formulas, convenient editing of textual content, implementation decoupled from a specific programming language. The only thing is that you will have to install an additional PHP extension.
By the way, gettext translation PO files can also be used in JavaScript via an extension: jsgettext.berlios.de/

S
Sergey, 2012-01-15
Protko @Fesor

There are many implementations of the solution to this problem, there is no best among them. For example, if we talk about PHP, I liked Yii's implementation of i18n more than others. There is a static method t() , the input of which is the translation context (consider the name of the file containing line translations), and the second parameter is the string itself. Then there is a search in an array of strings, where the hash is a string in the original language ... well, the essence is actually clear. In this case, translation files can be stored anywhere.
Regarding the localization of content, here you can also apply several options. The most banal is a table with languages, one of which is the default language. Next is a table of content containing entries for various languages. In this case, you can use a composite primary key and select by ID and by language. but this is just the first option taken from the head.
I advise you to go through the ready-made solutions to this problem and choose the best one for your project.

S
selitskas, 2012-01-15
@selitskas

If you want to explore this issue in detail, take a look at how internationalization is implemented in MediaWiki. 350 languages, support for complex plural forms of nouns, grammatical forms (declensions, etc.), gender context, date/time, etc.

E
Eternalko, 2012-01-15
@Eternalko

Here on Habré there were many articles on the topic. I take it you've reviewed them all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question