So I just started web development on my new job, meaning I only have very basic knowledge of php, my html, css and js knowledge is a lot better. I\'m trying to build a basic
I agree with K0pernikus in that your solution won't scale well. You can write a simple translation library yourself in under an hour and you can then test it to see if it will be robust enough for your needs.
The idea is to have language files without any logic in them. You simply call the file you need. All the logic about which language, file, translation key etc. is contained in your library.
I'll keep it very simple and down to a single file; of course, the actual translations will each need to be stored in a file too.
Directory structure:
Within each messages.php
file you need to return an array of translation keys and their respective translation. The translation keys are what you will use in your views. These files will become large with many hundreds or thousands of lines for larger applications. If you intend to keep this home-grown solution you'll need to implement caching. That being said, I have files with hundreds of translations and don't notice any significant performance hit.
'Matt\'s Marvelous Mysteries',
...
'Les merveilleux mystères de Matt',
...
Next, you need a library to read these translations and return the actual translation text to you. This file could simply be a collection of helper functions or a full-blown OOP system.
For simplicity, here is a collection of helper methods that get the job done. It does not implement parameterized substitution and you can add that feature relatively easily by making t()
accept a 2nd argument, but that is a whole different topic for another time.
The main method in here is t()
. It is very simple and accepts a single translation key. Ex. applicationName
or greeting
.
Firstly, it tries to determine which language to use. It does this in a sequence of priority: URL, session, browser, fallback.
lang
. If you think about it, that makes sense because a user intends to switch their language by clicking a
link that says "English" or "French".$defaultLanguage
.Once a language has been found, it puts it into the session so the next request doesn't need to go through all that again. It also loads the appropriate messages.php
file based on the discovered language.
Finally, once the language has been found and right file has been loaded into memory it searches for the given $key
and returns the appropriate translation. If the $key
is not found then it simply returns the given $key
which will show up in your views so you know something went horribly wrong and you need to start debugging.
0.8
$languages = array_combine($lang_parse[1], $lang_parse[4]);
// set default to 1 for any without q factor
foreach ($languages as $lang => $val) {
if ($val === '') $languages[$lang] = 1;
}
// sort list based on value
arsort($languages, SORT_NUMERIC);
}
}
$supportedLanguages = getSupportedLanguages();
foreach ($languages as $locale => $weighting) {
// We're dealing with locale: Ex. en-US
if (preg_match("/[a-z]{2}-[A-Z]{2}/", $locale)) {
$browserLanguage = substr($locale, 0, 2);
} else {
// Probably dealing with a language: Ex. en
$browserLanguage = $locale;
}
if (in_array($browserLanguage, $supportedLanguages)) {
return $browserLanguage;
}
}
return $defaultLanguage;
}
/**
* Returns an array of languages this web application supports.
*
* @return array
*/
function getSupportedLanguages()
{
return [
'en',
'fr'
];
}
To use it, save these methods into a file called translator.php
and then include that file in every page you want to use translations.
Sample:
French';
} else {
echo 'English';
}
// Your code... blah blah
// Ahh.. Finally, a translation!
echo '' . t('applicationName') . '
';
Edit
The last thing I will say is that there is a whole world out there of localization, internationalization (often abbreviated as i18n) which you can learn about.
In my example, I simplistically called it language but often people referer to it as locale but that has a different meaning and syntax. For example, there is a difference between en_CA and en_US and en_GB; all of which are English but have regional differences.
echo t('salutation');