R
R
Robotex2011-01-27 03:33:01
PHP
Robotex, 2011-01-27 03:33:01

Site localization

The task was to translate the site into many languages ​​of the world. The first way that comes to mind is to create a static class in which we enter the language identifier, and which contains a function that returns the text, depending on the argument passed (the path to this text). The localization files themselves contain an array of texts. Let's say you need to localize the settings page, the "username" field, pass the Settings/UserName path to the function and it returns the value $localize["Settings"]["UserName"];

With this approach, several questions arise:
1. How to store these localization arrays more efficiently: in php files and include (include_once) a specific file depending on the language, or in json using memcache and select the right one?
2. How to determine the user's system language (and regardless of the operating system)?
3. Perhaps there is a more efficient approach?

PS As an option, the modification date of the php script looks, and if it is newer than the one in memcache, then the array is serialized and placed in memcache. Or is it better to pre-serialize localization files before uploading to the server?

Answer the question

In order to leave comments, you need to log in

10 answer(s)
A
Atrax, 2011-01-27
@Atrax

en.wikipedia.org/wiki/Gettext

K
Kyborg2011, 2011-01-27
@Kyborg2011

1. How to store these localization arrays more efficiently: in php files and include (include_once) a specific file depending on the language or in json using memcache and select the one you need?
In my opinion, the MySQL database is the most convenient in this case.
2. How to determine the user's system language (and regardless of the operating system)?
As far as I know, no way. Usually on sites some language is standard, but you can switch to others.
3. Perhaps there is a more efficient approach?
If you write from scratch, then I don’t know more efficiently, if on the Zend Framework, then special classes have already been developed there.

C
Chvanikoff, 2011-01-30
@Chvanikoff

I recommend taking a look at the implementation from the Kohana framework.
github.com/kohana/core/blob/3.0/develop/classes/kohana/i18n.php
github.com/kohana/core/blob/3.0/develop/base.php
In principle, it's not difficult to understand, I think.
In an application, for example, write:

//установка языка
I18n::$lang = 'ru-ru' //к примеру
//где нужен перевод пишете, например
__('hello :username', array(':username' => $username));

And create, for example, a ru-ru.php file (the Kohana_I18n class is written in such a way as to search for this file in accordance with its architecture - you will have to edit this moment if you want to use it) with the content:
<?php

return array(
    'hello :username' => 'Привет, :username!',
);

A
Alexander, 2011-01-27
@Ist

1. Recently on Habré there was just an article about PHP optimization. The fastest way to store in a file included. After all, it can be generated from the admin panel. So this is convenient.
2. Either by IP address (there are many geolocation databases on the Internet), or by browser headers (Accept-Language).
3. From scratch, this is the fastest way to join, probably. You can, of course, recycle pages to replace the corresponding. the words. But It will. probably slowly. Although with caching and so on ...

R
Robotex, 2011-01-27
@Robotex

What language are in the browser headers? Browser or OS?
I have the Ukrainian language in Ubuntu and the Ukrainian language is automatically set on piratebey, facebook, vkontakte. And in Vendian, Russian is, accordingly, Russian everywhere. How do they do it?

D
Denis Turenko, 2011-01-27
@Dennion

It's true that it hasn't been implemented yet, but there is an idea to make a lang-file on the fly, i.e. a script is connected that checks all occurrences of Russian words (initially, the interface was written in Russian) and, as it goes around, creates a language map, similar to
$localize["Settings"]["UserName"]; into a file, for example eng.php, then you just have to fill it in as you work. Of course, all this will need to be done in UTF.
Pros
- no need to worry about the lang-file and prescribe variables in php files
- you can connect an unlimited number of languages
​​Cons
- decrease in software speed
Thoughts along the way
You can play with speed and create another serialized array with binding to specific php (all sorts of editing windows, etc.), those set of language variables in such files is limited to the standard set.
Language phrases will need to be taken in the span id="lang" tag to increase speed.
PS While I was writing, I read the comment above and realized that it is already in Zend_Translate, my idea was stolen :)

D
Denis Turenko, 2011-02-01
@Dennion

Translation on the fly, it is better, of course, to write the result to a file so as not to waste resources every time.

function translate($text) {
        $text = urlencode(win_utf8($text));
        $domain = "ajax.googleapis.com";
        $result='';
        $fp = fsockopen($domain, 80, $errno, $errstr);
        if (!$fp) {
            return exit("Ошибка соединения с сервером переводчика");
        } else {
            fputs($fp, "GET /ajax/services/language/translate?v=1.0&q=".$text."&langpair=ru%7Cen&callback=foo&context=bar  HTTP/1.0\r\n");
            fputs($fp, "Host: $domain\r\n");
            fputs($fp, "Connection: close\r\n\r\n");
            while (!feof($fp)) {
                $result.=fgets($fp, 1000);
            }
            fclose($fp);
        }

        preg_match('|"translatedText":"(.*?)"|is', $result, $locale);
        return $locale[1];
    }

A
artoodetoo, 2011-01-27
@artoodetoo

In general, you yourself described everything well and you can do without extraneous hints :) Just a couple of remarks:
Serialized data is loaded a little faster than PHP code. If you add to this the overhead of memcache, the whole profit can disappear. Test - this is a dependent platform.
There are PHP extensions with fast serialization/deserialization functions. Again, the gain will be only for special cases, if the data is really _many_. Translation files are most likely not the case.

Z
zavg, 2011-01-27
@zavg

The Zend framework has a Zend_Translate class . It works with different data presentation options, ranging from php array and delimited csv file to ini files, xml files and GetText format files.
I recommend the last option for localization. All localization data is framed in a certain way in the code, a special utility (for example, POEdit) scans the project files for new values ​​and changing old ones, after which a convenient table appears for entering the translation. Thus, with the help of such a tool, localization can be delegated to a translator who has nothing to do with the project, code, etc. at all.

P
P0WERMIC, 2011-01-31
@P0WERMIC

I saw that they even do this: they put a link or a drop-down list for choosing a language, and by clicking on Chinese, the user will open the site in google translate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question