Reputation: 275
Currently I have my php application running on english. We have to now cater for few other languages. We notice some do a whole set of translation of the all the form and put in a new folder label according to the translated language. Is that an optimize method?
Upvotes: 0
Views: 342
Reputation: 90
You should never try to store translations for user interface elements (let's call them 'static') in your database. Instead use a localization file format that is appropriate for your technology. When using PHP, this could be Gettext (PO), CSV or just a simple PHP Array file.
Advantages: - You can send your files to a translator/translation service without having to extract them from the database - You don't have to ship database dumps for a development/staging version of your application - You can use your translations very easy for another technology - Speed: Parsing a CSV/Ini file in PHP is way faster than performing n queries on every page
I think Andy Gee and FaceOfJock displayed quite nicely how your solution should look like.
Upvotes: 0
Reputation: 2195
Most implementations basically use a look-up table indexed by the language (either in a database or a text file).
Then in your code whenever you display a user interface element you would have a function that looks up the correct string for the language you want to display.
A very simple way might be with including the right file depending on the language.
In your text file (lets call it 'de.php') something like:
$lang[] = ['Are you Sure?']['Sind Sie sicher?'];
$lang[] = ['Cancel']['Abbrechen'];
then your code could have:
//load the appropriate language
include($userLang.".php");
....
echo $lang['Are you sure?'];
Upvotes: 1
Reputation: 7034
I'm still for the idea of database.
Let's say you have input field, next to it there is "Username" in english, it's value by default is "Enter your username". You can have something like:
<td><?=$obj->translate('username');?></td>
<td><input type="text" name="username" value="<?=$obj->translate('enter_username');?>" /></td>
So you have i.e. in your db table translations
:
id | placeholder | en | pl |
-------------------------------------------------------------------------
1 | username | Username | Nazwa użytkownika |
2 | enter_username | Enter your username | Wprowadź nazwę użytkownika |
So your client is polish, you set him $_SESSION['language'] = 'pl'
You build function/method translate(), which returns the current language translation.
public function translate($word) {
$sql = "SELECT $_SESSION['language'] FROM translations WHERE placeholder = '$word';";
$this->db->query($sql);
return $this->db->result();
}
So basicly, when you run $obj->translate('enter_username');
for your Polish client, you make query like this:
SELECT pl FROM translations WHERE placeholder = 'enter_username';
which returns 'Wprowadź nazwę użytkownika'
Everytime you need something to be printed, you have to insert it's placeholder in your db, and later or immediately it's translations.
If you need and error alert via javascript, for wrong username for example, you add a placeholder in the db i.e. 'wrong_username_from_login_form" lately translated in English as "You have entered wrong username. Please go back and try again".
You can have:
<script>
alert("<?=$obj->translate('wrong_username_from_login_form');?>");
</script>
If your client is English, he will have alerted on its screen: "You have entered wrong username. Please go back and try again"
Upvotes: 1
Reputation: 8334
You should use files that contains your sentences(or use database) and include them according to selected language.
For example :
Language_FR.txt:
hello = 'Salut'
delete='Supprimer'
Language_EN.txt:
hello = 'Hello !'
delete='Delete'
Upvotes: 2
Reputation: 3345
It depends on the number of translations. You could use one php include file for all languages or break each language into a separate file.
$lang['en']['submit'] = 'submit';//english
$lang['fr']['submit'] = 'soumettre';//french
$lang['es']['submit'] = 'presentar';//spanish
and in your placeholder based html with
<?php
$lang = 'fr'; //set the language somewhere
<button><?=$tang[$lang]['submit'];?></button>
?>
This would output <button>soumettre</button>
Don't make a copy of the entire site though or editing will mean editing translation count number of times each time you want to make a change.
Upvotes: 1