Answer the question
In order to leave comments, you need to log in
How to do website localization?
Hello!
So far, I'm just learning and creating a site that I would like to have in two languages: in Russian and English. Since I'm just learning, I would not want to resort to ready-made solutions (like Gettext) yet - personal experience, it seems to me, will be useful, at least in the first project.
This is done through the function below. If there is no English text, then Russian will be displayed; and if Russian is also missing, then an error will be displayed:
function __locale($class) {
if (isset($locale[$class][$_SESSION['language']]) && strlen($locale[$class][$_SESSION['language']]) > 0) {
return $locale[$class][$_SESSION['language']];
}
elseif (isset($locale[$class][0]) && strlen($locale[$class][0]) > 0) {
return $locale[$class][0];
}
else { return 'locale error '.$class; }
}
$locale = array(
'index.menu.home' => array('Главная', 'Home'),
'index.menu.regi' => array('Регистрация', 'Registration'),
'index.menu.login' => array('Вход', 'Login')
// и так далее
);
Answer the question
In order to leave comments, you need to log in
read
php.net/gettext
php.net/setlocale
// didn't want to resort to ready-made solutions (like Gettext)
uh, didn't notice) but from personal experience I'll tell you - if you write your own, it will take a lot of time, but there will be little sense. Do you know the features of all the languages of the world? it means that the solution will be tailored only to a couple of languages \u200b\u200bknown to you, and you will fill the sea with cones.
regarding the language from the browser - it is better to offer a transition to a version with this language, and make the site available to everyone in both versions. it’s more humane (for example, I have a system in English) and friendlier to bots.
Well, it’s worth separating the interface and content. the content is likely to differ more than it seems, so it may be convenient not to translate each line in the database / start fields, but to use a separate database, or filter records by a separate field with a locale mark.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question