Welcome Guest, Not a member yet? Register   Sign In
codeigniter gettext poedit php
#1

[eluser]wormhit[/eluser]
Hi!

Simple tutorial how to use gettext in your codeigniter application.

1) To translate something use function _('my text') in your controllers and views.

2) Create language files structure as follows:
Code:
/application/language/locales/lv_LV/LC_MESSAGES/
where lv_LV your language

3) Configure poedit (http://goo.gl/zSyfz) (use codeigniter paths) and generate *.mo and *.po files using poedit and name them as "lang":
Code:
result: /application/language/locales/lv_LV/LC_MESSAGES/lang.mo and lang.po

4) Create new codeigniter helper /application/helpers/my_language_helper.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
function set_translation_language($language){
    $lang_path = FCPATH.APPPATH.'language/locales';
    putenv('LANG='.$language.'.UTF-8');
    setlocale(LC_ALL, $language.'.UTF-8');
    bindtextdomain('lang', $lang_path);
    textdomain('lang');
}
?>

5) set helper in autoloader: /application/config/autoload.php
Code:
$autoload['helper'] = array(..., 'my_language');

6) Initialize translation with function. (You can also set language in cookie and set language in helper)
Code:
set_translation_language('lv_LV');


That's all! =]

I tried to find something like this all over the web, but found nothing.
So, I looked at default php gettext functions (http://www.php.net/manual/en/function.gettext.php) and it was so easy to make it work with them.

Hopefully that will be useful to someone..
#2

[eluser]toopay[/eluser]
First post, and try to share something valuables. Great.
#3

[eluser]T-productions[/eluser]
HI everyone,

I've tried your solution on Code Igniter 2.0.2 but it doesn't work at all.
In fact when I echo a string with "_" like :

Code:
echo _("My string");

I work in a local environment with Wamp Server and my firefox says :

Quote:The connection was reset

However I've put an autoload for this helper and copy your function in a helper like this :

Code:
<?php
set_translation_language('fr_FR');
function set_translation_language($language){
    $lang_path = FCPATH.APPPATH.'language/locale';
    putenv('LANG='.$language.'.UTF-8');
    setlocale(LC_ALL, $language.'.UTF-8');
    bindtextdomain('messages', $lang_path);
    textdomain('messages');
}
?>

Any ideas ? Thanks in advance for your help.

Cyril
#4

[eluser]wormhit[/eluser]
insted of _() try gettext() function
#5

[eluser]T-productions[/eluser]
Thanks for your answer but it's the same result...
It's kind of weird cause gettext extension is activated.

Anyone knows what's wrong ?
#6

[eluser]wormhit[/eluser]
I don`t understand.

You followed instructions and when you started application it`s sad:

Code:
The connection was reset
?

Do you created *.po and *.mo files?
Check if language file structure is correct and file domain name is ok.
#7

[eluser]T-productions[/eluser]
It's not like that I'm getting this message once I display variables with _('');
I used to use GetText but not in Code Igniter.

Here is the process :

Structure with :
Quote:application/language/locale/en_US/LC_MESSAGES/messages.mo
application/language/locale/fr_FR/LC_MESSAGES/messages.mo
Then I try what I put above

I get this error only when I put an echo even with GetText

I'm working on WampServer with gettext activated. I'm gonna try to upload on my hosting to test it also
#8

[eluser]wormhit[/eluser]
I think it is not the gettext problem.
You tryed to google error message?
http://www.wampserver.com/phorum/read.php?2,57192
#9

[eluser]Truong Chuong DUong[/eluser]
I just make a library which can auto translate all text between {t} and {/t} in the view, I posted here in case some one want to use it instead calling the gettext function in the view:

http://www.chuongduong.net/page/15/codei...-view.html

The view code might be:

Code:
<html>
<head>
<title>{blog_title}</title>
</head>
<body>

<h3>{blog_heading}</h3>
{blog_entries}

<h5>{t}Title is{/t}  {title}</h5>

<p>{t 1="<b>" 2="</b>"}Click here %1to see%2 me{/t}{body}</p>

<p>{t 1="{id}" 2="author"}The id is: %1 wrote by %2{/t}</p>

<p>{t 1="<a >" 2="</a>"}Please lick on me%2{/t}</p>

{/blog_entries}

&lt;/body&gt;

&lt;/html&gt;

#10

[eluser]Syed Abidur Rahman[/eluser]
I think, you have misspelled the 'locales' directory. The statement should be like the following:
Code:
$lang_path = FCPATH.APPPATH.'language/locales';
Thank You.




Theme © iAndrew 2016 - Forum software by © MyBB