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



If you are a web developer using PHP from small web applications to large corporate portals with millions of page views, you may suddenly find that you need your site or application translated to another language. When your company opens www.company.com.br you need your application in Portuguese, then you'll need it in Japanese and so on. You don't have a "translation" problem, you have an internationalization problem.

Translating only implies simple string translation of code, you may do it in a clever way or a painful (very) way scanning all the code and translating strings. This method tends to produce errors (Have you ever seen a site in Spanglish?) and it is not reusable, just a patch. Internationalization defines a strategy to build sites and applications that you can easily translate into other languages. This is the goal of this article.

Enter GNU


GNU has a very good set of tools to produce internationalizable applications called gettext, it was developed with "C" and "C++" applications in mind but is also very easy to use in other languages such as PHP. Gettext is probably installed on your Unix station, try a gettext command like 'gettext' from the command line to see if you have it. If not (rare) download the package from ftp.gnu.org and install it. If you have gettext the next stage is to use it from PHP.

Internationalizing PHP Scripts


To use gettext in your PHP scripts you'll have to modify all your scripts, however this will have to be done only once. The modifications are not very difficult and you'll probably want to build some utility to scan the code and prompt 'want to modify this?' etc... If you don't want to do that, no problem at all. So what do you have to do in your code? Very simple just replace all the "strings" with something like:


print(_("Hello world"));



Yes, you use the very unknown PHP function "underscore", which is an alias to the not-short-to-write "gettext" function. Every string that you may translate sometime will have to be translated. Once you get used to this and see the advantages you'll find that you always write your strings wrapped with the "_" function.


| < 1 > | 2 | 3 | Next page >> | 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


   RE: internationalization using GETTEXT Walter Torres 25.03.2009 16:20

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

Sure: http://web.walters-way.com

I have a detailed install process.

Walter


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