It became interesting how it is possible to implement the multilingualism of the Internet page / site, "googled" for a while, but it didn’t stumble upon an explanatory explanation ... Could you comrades stick your nose at where to dig?
3 answers
It is necessary to make a file containing a declaration of constants, in which there will be lines for output in different parts of the site.
For example (cited already somewhere) ...
Main file:
<?php //объявляем константу содержащую путь до файлов языков define('LANGAGE_DIR', $_SERVER['DOCUMENT_ROOT']."/language/", false); //тоже, путь до шаблона вывода define('TEMPLATE_DIR', $_SERVER['DOCUMENT_ROOT']."/template/", false); //получаем переменную языка $language = $_GET['lang']; //не обязательно получать переменную гет-ом //можно брать ее откуда угодно, например - из базы данных, или из сессии //загружаем файл перевода include_once(LANGUAGE_DIR . $language . '.php'); //загружаем файл шаблона, начинаем вывод include_once(TEMPLATE_DIR . 'default.php'); ?>
Russian language file:
<?php define('LANG_TITLE', 'Главная страница'); define('LANG_H1', 'Добро пожаловать!'); define('LANG_MESSAGE1', 'Рады приветствовать вас на нашем сайте.'); ?>
English language file:
<?php define('LANG_TITLE', 'Main page'); define('LANG_H1', 'Welcome!'); define('LANG_MESSAGE1', 'Welcome to our site.'); ?>
Template file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title><?=LANG_TITLE?></title> </head> <body> <h1><?=LANG_H1?></h1> <p> <?=LANG_MESSAGE1?> </p> </body> </html>
You just need to put in $ language the name of the language file, for convenience without extension. It should also provide for the output of the page in the default language, if nothing is assigned in $ language. That is the idea. If something is not clear, write! Have a good day :)
- And how to do it through separate text files (.mo && .po) - Basil Jimmy
The process of creating multilanguage is usually divided into two parts: internationalization and localization . Both of these processes are well documented in wikipedia.
The most popular and convenient tool for internationalization, both sites and application programs is gettext . For PHP, there is also a corresponding extension .
The main idea of this library is that the original string itself is used to designate the translation, and not any special identifiers. At the same time, if the translation of this string is missing, then the original string is simply output. For example, the same response pattern above would look like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title><?= _('Main page') ?></title> </head> <body> <h1><?= _('Welcome!') ?></h1> <p> <?= _('Welcome to our site.') ?> </p> </body> </html>
More details about gettext and its capabilities can be found in the same wikipedia .
PS The documentation for gettext says that the original lines should be in English, however, Russian lines can be used in recent versions of gettext, provided that utf-8 is used everywhere. In this case, there may be some confusion in the English localization, but this problem is easily solved.
I would like to add, if you use a database, then in the list of site pages, you can add an example of the text in several languages:
id - page identifier in the database
name - the name in English as an alias
t_ru - name in Russian
t_en - name in English
c_ru - content in Russian
c_en - content in English
and, if necessary, load only the required column, the language should be given a choice initially, and save its parameters in a cookie, for example:
<?php setcookie('lang', $_POST['lang']); ?> <form action="?set=lang" method="post"> <select name="lang"> <option value="ru">ru</option> <option value="en">en</option> </select> <input type="submit" value="ok" /> </form> <?php $pid = $_GET['pid']; $pref_lang = '_'.$_COOKIE['lang']; mysql_query("SELECT t.$pref_lang, c.$pref_lang FROM pages WHERE id='$pid'"); ?>
Something like this, there are a lot of ways to implement, in most cases you can do without a database and use files. The whole point of creating a website in different languages is to know the necessary language for the visitor and display a version of the page in that language.
And if you need to translate the labels on the buttons, headers, etc., then you can create a file with arrays, and connect it at the beginning of the page with the require function, namely to it and at the beginning, since it does not slow down the program, as does include () ;.
example:
<?php //langpack.php $arroflangs['button1_ru'] = 'отправить'; $arroflangs['button1_en'] = 'submit'; ?> <?php require("./langpack.php"); $lang = '_'.$_COOKIE['lang']; echo '<input type="button" value="'.$aroflangs['button1'.$lang]'" />'; ?>
Immediately apologize for any errors, because I write without the ability to check.
- oneIt should be clarified that when using the database, it is highly desirable to cache the result, for example, in memcache. When using gettext, you do not need to cache anything, since Translation data is stored in shared memory. - Ilya Pirogov
- So it is, he wrote in haste, I think a person will figure it out. - Andrey Arshinov
- Thank you all for the answers! - chi100v