How would one give users the option to change the language of the text on a website to phrases that are pre-defined in PHP?
An example of this would be facebook, changing the language to English (Pirate) yields not a translation but a retrieval of many different of phrases that have been defined somewhere else.
I am hoping to do the same kind of thing by adding a Pirate language into my website giving users the option to change it themselves.
The best thing that I can think of is a file for each language that is full of variables that are then called upon wherever applicable and have some sort of method of changing between these files. Though that idea seems tacky and impractical and I am wondering if anyone has any other ideas on how I could do this.
Thanks heaps!
Your idea is similar to what I have experienced using using CakePHP which includes internationalization support using gettext files. The ides is pretty simple. You output all strings that need translation using a transalator function. In Cakephp 1.X it was __() . So if i want to translate a string "hello world" , i would do
<?php
__('hello world');
?>
the trick to knowing which language it should be translated into lies in setting the session language or locale. In Cakephp it is setting "Config.language" to the shortcode of the language I want it to be translated in (For french i would set it to 'fre'). The application would then look for 'fre' directory in its locale folder, read the file from there and look for the string match of "hello world" and get its corresponding translate string from there (This may not be 100% accurate). I can think of this as returning an array from the file and then using keys to match translation.
<?php
return array(
'hello world' => '123456',
);
?>
Another way to do it would be storing strings and their translations in the database as
string,translation,languageID
and then querying db using the string to translate and language ID or short code.