Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


Internationalization Using PHP and GetText



Producing mo Files



You have to produce a .mo file to use gettext, this is done using the 'msgfmt' command for each .po file:


 
   $msgfmt messages.po -o messages.mo 



Setting up Directories


The best way to use gettext is to build a "locale" directory in some branch of your code tree and inside this directory you create one subdirectory for each language, inside each language's direcory you create an LC_MESSAGES directory, where you put .mo and .po files for the language. Example:


 
   /src 
   /locale/en/LC_MESSAGES/messages.mo 
                          messages.po 
           es/LC_MESSAGES/messages.po 
                          messages.mo 



Use this URL http://lcweb.loc.gov/standards/iso639-2/bibcodes.html#op to find the 2 character codes for languages. It's nice to follow standards...

You'd probably start liking your "neat" structure of languages a lot, you are ready to decide which language to use in PHP, simply add, in your main script, this code:


// Set language to Spanish 
putenv ("LC_ALL=es"); 

// Specify location of translation tables 
bindtextdomain ("messages", "./locale"); 

// Choose domain 
textdomain ("messages");


The important line is putenv("LC_ALL=es") which basically tells which language to use, with this PHP will use your /locale/es/LC_MESSAGES/messages.mo file to translate all the strings. If you want a different language just change:


putenv ("LC_ALL=en);



And "auto-magically" you have an English application.

Conclusions and Code


Gettext is a very good way to standardize your PHP code to support internationalization, you modify or write your code only once and you can have your site or application in several different languages with only a minor modification. One side effect of this strategy is that you have to make sure that all the strings are set in the content or logic layer not in the presentation layer. If you use XSLT to parse XML generated from PHP for example you'll have to make sure that no strings are added in the XSLT layer. I think this is quite good.

In inter.tar.gz you'll find a subtree that you can put wherever you want inside your document tree, I ship examples in English and Spanish, change the environment variable from "en" to "es" and browse "prueba.php" (the Spanish file),and see the changes. Edit the .po files and rebuild the .mo files using msgfmt to play the translator role. As perl fans says "there are many ways of doing things..." this is a neat one, try it.


<< Previous page | 1 | 2 | < 3 > | Printer-friendly page |

Comment List


Topic: Author:
Time:
internationalization using GETTEXT Suma Abey 13.07.2007 13:13

explain the stepwise procedure to install gettext in windows/mysql platform


Use gettext in windowsXP Hitesh Kanjaria 30.01.2006 08:18

Hi,
Can you explain me how to use gettext in window os.
Or any tutorial for that.


mo error jase dixon 21.10.2002 14:49

when i compile the po file to convert it into an mo file i get this error msg

"the character set has no portable coded name"

however the mo file is still created but when i run the php file no translation takes place . Does anyone know what this type of error is?

thanks in advance

jas


   RE: mo error ota fred 05.06.2003 12:56

Hello, I have the same problem.

Can you tell me if you have resolved it, and how.

Thanks


> when i compile the po file to convert it into an mo file i
> get this error msg
>
> "the character set has no portable coded name"
>
> however the mo file is still created but when i run the php
> file no translation takes place . Does anyone know what this
> type of error is?
>
> thanks in advance
>
> jas


xgettext Rakesh Shroingi 30.04.2002 14:31

xgettext -a langtest.php

While using xgettext i am getting the following error.

warning: file `langtest.php' extension `php' is unknown; will try C

My php is having the gettext option enabled.

Please help.
Thanks


   RE: xgettext Jose Cortina 07.06.2002 09:51

I had the same problem, and finally found the way out... Try with:

xgettext -C script.php -o messages.po --keyword=_

(The keyword "_" is because I use the short form of gettext in php _("My Text") instead of gettext("My Text") )

If you don't specify the keyword, then it will get *all* strings from your script... (it gets messy)

Hope this helps.


gettext and here docs? Colin Viebrock 31.07.2001 00:45

Imagine this php script:

<?php

echo _( <<< EOF
It's a great day!
EOF
);

?>

When I run "xgettext file.php" on it, I get warnings about "unterminated character constant" ... which I assume is the single quote in "it's".

Is there a way around this?

Also, how do I tell xgettext to only look for strings inside _( ... ) blocks?

- Colin


How to produce .mo file harvail singh 05.03.2001 06:31

What ia m doing is
After writng .php file i
run xgettext -a command to get the po file
After this i run i msgfmt file.po -o fil1.mo
but this command is not producing .mo
file

any help will be appreciated


Common problem Luis Argerich 26.01.2001 16:50

If you get that message you don't have the gettext extension compiled into php, add the following string...

--with-gettext

To your configuration script or ./configure command line.

Luis.


   RE: Common problem W L 01.11.2001 12:13

> If you get that message you don't have the gettext extension
> compiled into php, add the following string...
>
> --with-gettext
>
> To your configuration script or ./configure command line.
>
> Luis.

So does that mean we enter the following code in command prompt?

./configure --with-gettext

coz I did that and it still doesn't work.


Hmmm Urban Weigl 14.12.2000 21:38

I don't think I'm sure I know what you mean... ?


   RE: Hmmm Paul Kenneth Egell-Johnsen 15.12.2000 11:28

The problem with that is that I don't have any idea at all with what your problem might be:)

I'd love to help out (and probably Luis as well), but you need to be much more specific in describing what you have problem with understanding.

I'm looking forward to that clarification and the opportunity to help you.


     RE: Hmmm W L 07.11.2001 04:54

> The problem with that is that I don't have any idea at all
> with what your problem might be:)
>
> I'd love to help out (and probably Luis as well), but you
> need to be much more specific in describing what you have
> problem with understanding.
>
> I'm looking forward to that clarification and the
> opportunity to help you.

I have PHP installed but it's pointing gettext=/usr

how do i change that to gettext=/usr/bin?

What do I have to type in the command prompt?


       RE: Hmmm nobody important 29.08.2007 00:14

> > The problem with that is that I don't have any idea at
> all
> > with what your problem might be:)
> >
> > I'd love to help out (and probably Luis as well), but
> you
> > need to be much more specific in describing what you
> have
> > problem with understanding.
> >
> > I'm looking forward to that clarification and the
> > opportunity to help you.
>
> I have PHP installed but it's pointing gettext=/usr
>
> how do i change that to gettext=/usr/bin?
>
> What do I have to type in the command prompt?

I just compiled the so file
./configure --with-gettext=shared

and then copied the .so file in

$PHP_BUILD_DIR/modules/gettext.so

to the folder pointed at in the line

extensions_dir=/some/dir

in php.ini (on my machine it was /usr/local/lib/php/modules), and then added the following line to php.ini

extension=gettext.so

Pretty much the same thing for every shared library I didn't compile when I first compiled PHP.

Just make sure that the version you're building from is the same as your current PHP build.


     harvail singh 02.01.2001 07:17

hi
I ahve a problem Using gettext() or its alias _
It gives error
Call to undefined function: gettext() in /home/httpd/html/testing/hello.php on line 10


I am using php 4.0.3pl1

can u help
thanx




Forgot your password?

Register a new user

Results

Polls