It became interesting how it is possible to implement the multilingualism of the Internet page / site, I “googled” for a while, but I did not stumble upon a sensible explanation of what and how… Could you comrades poke your nose in the direction where you need to dig?
Answer 1, authority 100%
We need to make a file containing the declaration of constants, which will contain lines for output in different places on the site.
For example (already quoted somewhere)…
<?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.'); ?>
<!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 the extension. You should also provide for the output of the page in the default language if nothing is assigned in $language. Here, that’s the idea. If something is not clear, write! Have a nice day 🙂
Answer 2, authority 73%
The main idea of this library is that the original string itself is used to designate the translatable string, and not any special identifiers. In this case, if there is no translation of this string, then the original string is simply displayed. For example, the same response template 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 wikipedia.
PS The documentation for gettext says that the original strings must be in English, however, in recent versions of gettext, Russian strings can also be used, 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.
Answer 3, authority 55%
I would like to add, if you use a database, then in the list of site pages, you can add a text option in several languages, an example of a table:
id – page ID in the database
name – English name as alias
t_ru – title in Russian
t_en – title in English
c_ru – content in Russian
c_en – English content
and, if necessary, load only the desired column, give the language 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 it, in most cases you can do without a database and use files. The whole point of creating a site in different languages is to know the language that the visitor needs and display a version of the page in this language.
And if you need to translate button labels, headings, etc., then you can create a file with arrays and include it at the beginning of the page with the require function, exactly to her and exactly at the beginning, because it doesn’t slow down the program like include(); does.
<?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]'" />'; ?>
I apologize in advance for possible errors, because I write without the ability to check.