I am new to php. On my website I have created a list box of all the languages, but I don't know how to translate my English content to Chinese or to Spanish basically I need a translation code of a particular language.
I have made a langauge table with is attributes as Lang_id, Lang_name.
have a look here: Internationalization in PHP. PHP 5.3 supports native internationalization (http://de.php.net/manual/en/book.intl.php)
Make dedicated page like : "lang.php" having codes like
0 = for English
1 = Italian
2 = French
$me_user_name = array('Username','nome utente','','','Nom d\'utilisateur')
use this array on your site.
for ex.:
echo $me_user_name[0] // to print in english
Related
I 'm from peru present .
what happens is that eh followed a tutorial on gettext and CodeIgniter and I can not get it to work , I only translates the text into Spanish peru .
the only thing that changed was the helper eh , that after writing the function , execute it as follows . set_translation_language ( en_EU ) , or where I have to run this function . ?
From already thank you very much.
I suggest you to take a look at this: http://www.codeigniter.com/userguide2/libraries/language.html
CodeIgniter already support multi-language (location).
If your site will support only one language (Spanish), there is no need to use a location service.
But if you plan to support English and Spanish for example, what you need to do is:
Create a folder with the language name (ie: English)
Add a file with "_lang.php" (no quotes) at the end (ie: text_lang.php)
Create an array under the value $lang, each object inside will contain the
key, for example: $lang['title'] = "This is the title."; (English)
and then on the Spanish folder, the same but translated:
$lang['title'] = "Este es el tÃtulo.";
Load the language file: $this->lang->load('text_lang', 'spanish'); that's valid in our example.
Last, but not least, load the language line you want to translate, for example, title $this->lang->line('title');
I follow the cakePHP book to config the i18n and l10n of my site. Suppose I have English, Spanish, German.
Browsers usually have a list of languages ordered by priority. If at the first position of the list appears a language that is configured on my site all works fine. But if, for example, the list is this: French, German, Spanish, English; localization fails and shows the i18n identifiers that I used in my code.
I would like that in this case the page appears translated in German, because in the list is the first language that my site can provide. This would be the perfect solution but at least I would like that I can configure a default language (for example, English) if the first language of the browser list is unknown by my page.
What I can do to achieve this and where should put the code?
Thank you in advance.
Well, I did the next in app/Config/bootstrap.php:
$browserLangs = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
$find = 'eng';
foreach($browserLangs as $browserLang) {
$lang = substr($browserLang,0,2);
var_dump($lang);
if ($lang == 'en') {
$find = 'eng';
break;
} else if ($lang == 'es') {
$find = 'spa';
break;
} else if ($lang == 'ca') {
$find = 'cat';
break;
}
}
Configure::write('Config.language', $find);
In Accept Language header we have a string with all the list of languages of the browser. Then I define English as default, and try to find other language in the list, breaking the bucle when I get one.
I put this as an answer because it works, but its dirty because:
I don't know if this is the best way and the best place,
this is very static, you have to put the languages one by one and when you add another language you have to update the algorithm. The best solution will check automatically for the locales defined on the cake application, which I don't know how to ask for.
Perhaps this code should be defined as a plugin, component or something like that.
Perhaps someone can improve this or post another better solution as a new answer.
You will have to open the created .po file, with an editor (for example: http://poedit.net) there you can create multiple language versions.
Dont forget to change the plural forms for every language, for german it's nplurals=2; plural=(n != 1), here a list: http://translate.sourceforge.net/wiki/l10n/pluralforms.
After that, you'll have to save the language file in app/Locale/deu/LC_MESSAGES/...
You should have a default.mo and default.po file in that folder at the end.
I am working on a project and there are going to be 3 different languages: English, French and Spanish. This will be defined when the user signs up.
Now in my config file I have the following:
define("DEFAULT_SLOGAN", "The default slogan will go here.");
Until I started realizing that I needed to accept different languages.
The user has an assigned language code (EN, FR, SP). How would I go about having different language strings for each page? Would I need to have something like this:
define("DEFAULT_SLOGAN_EN", "Slogan in english");
define("DEFAULT_SLOGAN_FR", "Slogan in french");
define("DEFAULT_SLOGAN_SP", "Slogan in spanish");
And for each string just have 3 different versions of it? Not too sure the best way to approach this.
Thanks!
A simple strategy that is often used are arrays:
$lang['en'] = array(
'DEFAULT_SLOGAN' => 'The default slogan will go here.',
);
// Same for other languages
Then, in your actual code, make sure that $lang is available (you can use the global keyword for this) and use these arrays.
A better approach would be to create
locale_definitions_EN.php containing
define("DEFAULT_SLOGAN", "The english default slogan will go here.");
local_definitions_FR.php containing
define("DEFAULT_SLOGAN", "The french default slogan will go here.");
etc. and then do something like
include "locale_definitions_$userLocale.php";
This has the advantage, that you don't need the memory to hold the unused other language constants.
Hi i am devloping site in php i need to translate whole website in other language like german,spanish etc... how can it possible in php i have tried using some csv but it all goes static i mean i can not convert the whole website ..
if you have any csv or api information ..
please help..
-Div
<?php
$GLOBAL['langs']=array(
'en'=>array(
'Welcome to my site!'=>'Welcome to my site!'
),
'it'=>array(
'Welcome to my site!'=>'Benvenuto sul mio sito web!'
)
);
function _($text){
$lang=$_COOKIE['lang'];
return $GLOBAL['langs'][$text];
}
?><html><head><?php
echo '<title>'._('Welcome to my site!').'</title>';
?></head><body>
....
</body></html>
While Christian's answer will do the trick there's a more cleaner and efficient way to achieve your needs: gettext is PHP's built-in function for internationalization.
Zend Translate is a library exactly build for this.
Gettext is not thread-safe. PHP supports only gettext and native array. Zend_Translate supports several source formats, including those supported by PHP, and other formats including TMX and CSV files.
If you want to translate 'a whole page' to any language, you could use Google Translate.
In php (or maybe gettext in general), what does gettext do when it sees a variable to dynamic content?
I have 2 cases in mind.
1) Let's say I have <?=$user1?> poked John <?=$user2?>. Maybe in some language the order of the words is different. How does gettext handle that? (no, I'm not building facebook, that was just an example)
2) Let's say I store some categories in a database. They rarely, but they are store in a database. What would happen if I do <?php echo gettext($data['name']); ?> ? I would like the translators to translate those category names too, but does it have to be done in the database itself?
Thanks
Your best option is to use sprintf() function. Then you would use printf notation to handle dynamic content in your strings. Here is a function I found on here a while ago to handle this easily for you:
function translate()
{
$args = func_get_args();
$num = func_num_args();
$args[0] = gettext($args[0]);
if($num <= 1)
return $args[0];
return call_user_func_array('sprintf', $args);
}
Now for example 1, you would want to change the string to:
%s poked %s
Which you would input into the translate() function like this:
<?php echo translate('%s poked %s', $user1, $user2); ?>
You would parse out all translate() functions with poEdit. and then translate the string "%s poked %s" into whatever language you wanted, without modifying the %s string placeholders. Those would get replace upon output by the translate() function with user1 and user2 respectively. You can read more on sprintf() in the PHP Manual for more advanced usages.
For issue #2. You would need to create a static file which poEdit could parse containing the category names. For example misctranslations.php:
<?php
_('Cars');
_('Trains');
_('Airplanes');
Then have poEdit parse misctranslations.php. You would then be able to output the category name translation using <?php echo gettext($data['name']); ?>
To build a little on what Mark said... the only problem with the above solution is that the static list must be always maintained by hand and if you add a new string before all the others or you completely change an existing one, the soft you use for translating might confuse the new strings and you could lose some translations.
I'm actually writing an article about this (too little time to finish it anytime soon!) but my proposed answer goes something like this:
Gettext allows you to store the line number that the string appears in the code inside the .po file. If you change the string entirely, the .po editor will know that the string is not new but it is an old one (thanks to the line number).
My solution to this is to write a script that reads the database and creates a static file with all the gettext strings. The big difference to Mark's solution is to have the primary key (let's call it ID) on the database match the line number in the new file. In that case, if you completely change one original translation, the lines are still the same and your translator soft will recognize the strings.
Of course there might be newer and more intelligent .po editors out there but at least if yours is giving you trouble with newer strings then this will solve them.
My 2 cents.
If you have somewhere in your code:
<?=sprintf(_('%s poked %s'), $user1, $user2)?>
and one of your languages needs to swap the arguments it is very simple. Simply translate your code like this:
msgid "%s poked %s"
msgstr "%2$s translation_of_poked %1$s"