Gettext php, idea ? - php

I installed gettext on a server and want to use for a php script..
I use with 3 languages. The original website is written in english..
Other are french and Italian.
I can without problem change language to French or Italian.. But when I want load the en_EN.mo file to replace the origine text of the php file, the translation are not loaded.
I try many things.. Remove file, replace name.. restart apache..
Nothing.. it work perfectly with all other langage, but impossible to use a english file.
I create new language.. it work too.. but never the en_EN..
All my files langage are in the same tree style
locale
en_EN
LC_MESSAGES
en_EN.mo
fr_FR
LC_MESSAGES
fr_FR.mo
...
if I force to use..
$directory = './locale';
$domain ='en_EN';
$locale ='en_EN';
putenv('LC_ALL='.$locale);
setlocale(LC_ALL, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
no result..
My problem is that I have no idea how to debug it ? Where can I find a log or something to help ? Where see errors ?
It's strange that it work with all lang, but not english.. I use Poedit to create my file, I have create the file many time.. in the same way that other..
Please share your idea :)

I create new language.. it work too.. but never the en_EN..
You misunderstand what the locale string means. It is not “2 language code letters followed by the same letters in uppercase” (that wouldn’t make any sense, would it), it is “language code followed by country code” per ISO 3166. And “EN” is not only not a valid country code for any country that speaks English, it’s not a valid country at all. You’re asking WordPress to run under the English locale (presumably), but only providing it translation files for “English in a made-up country” locale that WP is never going to look for.
You’re thinking of en_US.

Related

Internationalization with CakePHP

I'm trying to solve a Internationalization problem with CakePHP but I cant get cake to start localizing... even though I have everything set up as the documentation requires... my problem is twofold:
first I'd like cake to localize all __(''); strings I defined in the views. The second thing is that I have a datetime input field which I'd like to localize as well...
currently my Locale folder looks as follows:
/Locale
-cake_dev.pot
-cake.pot
-default.pot
-/DE/LC_MESSAGES/
--default.pot
and in my /config/bootstrap.php the last line is:
Configure::write('Config.language', 'DE');
thx so much for your support!
Indeed it should be deu. And you need rename deu/LC_MESSAGES/default.pot to deu/LC_MESSAGES/default.po, open it in poedit, update it (catalog) from default.pot and translate it. On save deu/LC_MESSAGES/default.mo will be compiled, this file is used by cake.
I'm not sure if Cake internally converts DE to DEU, further it should be lower cased. Systems other than windows are case sensitive. So try "deu" instead of "DE".

gettext() - on 3 languages 2 works one not

I've got a webpage that contains 3 languages - Polsih, English and German. English and German translations works perfect but Polish doesn't work at all. Where could be the problem? Here is configuration of gettext:
$locale = "pl_PL";
if (isSet($_GET["lg"])) $locale = $_GET['lg']."_". strtoupper($_GET['lg']);
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale.'.UTF-8');
bindtextdomain("messages", "./locale");
bind_textdomain_codeset("messages", 'UTF-8');
textdomain("messages");
Your test code is not particularly helpful. We'll simplify it this way:
var_dump( setlocale(LC_ALL, 'pl_PL.UTF-8') );
You vaguely suggest in your comments that you're getting FALSE. What does it mean? According to the documentation (I suppose you've already checked it, I'm just posting it here for completeness):
Returns the new current locale, or FALSE if the locale functionality
is not implemented on your platform, the specified locale does not
exist or the category name is invalid.
So, how do you get a list of available locales? As you've been said:
echo `locale -a`;

Make translations in multiple languages

I have my php gettext default language in English let's say
I would like in one of my controller, to translate some words in 2 other languages and put them all in an array.
ideally I could do
$word_sv = gettext($word, 'sv_SV');
$word_fi = gettext($word, 'fi_FI');
but it doesn't exist.
Is the only way to change the overall gettext settings each time?
function setLang($lang){
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("myPHPApp", "./locale");
textdomain("myPHPApp");
}
setLang('sv_SV');
$word_sv = gettext($word);
setLang('fi_FI');
$word_fi = gettext($word);
related: saw it on Google after : i18n with gettext but without the locale hassle?
Edit
here are the proposed answered solutions:
https://github.com/Philipp15b/php-i18n (seems best solution)
http://glotpress.trac.wordpress.org/browser/trunk/pomo (could use it if I find a good doc or tuto ;))
change locale on the fly, probably not good
I know the pains of using gettext, but its performance is what keeps me with it !
In your case, you might want to look at this little project ? i'm pretty sure this might help you !
this simply uses .ini files with translations, you can freely switch between files and echo the different languages for the same word.
https://github.com/Philipp15b/php-i18n
If you are bound to gettext here, Let the computer do the work for you.
You either have a list of words you then want to check against all languages, the do the wordlist for each language first. That will spare you some overhead to call the setlanguage function between each word and language.
If you want to go each language, each word, write the functions that way:
function gettext_by_lang($lang, $word) {
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("myPHPApp", "./locale");
textdomain("myPHPApp");
return gettext($word);
}
$word_sv = gettext_by_lang('sv_SV', $word);
$word_fi = gettext_by_lang('fi_FI', $word);
This would at least make your code more compact. Another idea that comes to mind is using a a parser for PO and MO files so that you can check for the data.
In PHP one of that is shipping with Wordpress / Glotpress:
http://glotpress.trac.wordpress.org/browser/trunk/pomo
Maybe this helps. That library is being maintained.
I guess an obvious answer is to roll your own global function:
function getLocalText($string, $lang)
{
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("myPHPApp", "./locale");
textdomain("myPHPApp");
return gettext($string);
}
$word_fi = getLocalText($word, 'fi_FI');

CakePHP Localization issue

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.

PHP How to handle different languages in config file?

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.

Categories