Welcome Guest, Not a member yet? Register   Sign In
Codeigniter+Gettext for multilingual site (i18n)
#1

[eluser]landitus[/eluser]
Hi all! I'm finally developing my first multilingual site which came from an old no-CI site which used Gettext cause there's a lot of text and not just actions. I'm really struggling to make gettext to work, having read the WIKI but I lacks information and examples. Is there someone here who has used the gettext library succesfully?

Can you post some examples of controller and views? I really thing everyone in the community would like to know more about this.
Thanks in advance
#2

[eluser]appelflap[/eluser]
I've just converted my CI website totally to gettext. I know it is not the standard way to use gettext i18n, because you actually want a text in stead of the CI_ variable. But it is a great way to convert you existing website to the gettext language method. So that's why I am sharing it.

I used the wiki code with a small adjustment. I added "$this->load_gettext();" to the constructor of MY_Language to always initialize the language file. You can also put it somewhere else if you switch languages dynamicly, but i only switch hardcoded at the moment.
Code:
function MY_Language() {
        parent::CI_Language();
        $this->gettext_domain = 'lang';
        log_message('debug','Gettext Class initialized');
        
        $this->load_gettext();
    }

I already had a lot of language entries that used the old method that looks like this:
Code:
$lang['FAL_user_name_label'] = 'User Name';
$lang['FAL_user_password_label'] = 'Password';

To convert your whole website to the gettext method use Notepad++ to search & replace all language related stuff. First of all try to convert your language/english/*_lang.php files using the following search&replace; strings. Remember to select "Regular expression" in Search Mode
Find what:
Code:
\$lang\['([^']*)'\].*= '(.*)';$
Replace with:
Code:
msgid "\1"\nmsgstr "\2"
It is possible you used double quotes in stead of single, then just change the quotes in the Find field. This trick does not work for multi lined language values. And also has trouble with quotes in the language value. So do check the result (search for "$lang[" in normal search mode)

Now the text can, for example, be used for the locale/en_EN/LC_MESSAGES/lang.po file. As described in the wiki.

The next step is to replace all lang->line() tags in your code. This can also be done with Notepad++ search&replace;. Use the "Find in Files" tab and select "Regular expression" in the "Search Mode". Also make sure the Director is set correctly and "In all sub-folders" is checked. Use "*.php" as a filter to skip all other files.
Find what:
Code:
\$this->lang->line\('([^']*)'\)
Replace with:
Code:
_("\1")

You can now type something like the following to use the language files of gettext.
Code:
<?=_("CI_admin_pending_succesfully_updated");?>
or if you need full php tags:
Code:
<?php echo _("CI_admin_pending_succesfully_updated");?>
#3

[eluser]Amitabh Roy[/eluser]
Interesting! Exactly what I was looking for. I will give this a try in my next project.
Of all the ways to implement multilingual sites like by maintaining different language.php files or language arrays or storing the strings in database the approach using gettext seems to be the best practice.




Theme © iAndrew 2016 - Forum software by © MyBB