NYCPHP Meetup

NYPHP.org

[nycphp-talk] gettext help

John Campbell jcampbell1 at gmail.com
Sat Dec 15 17:58:30 EST 2007


> I have concatenated strings all over the place after many people yelled at me
> that my lines in my scripts are longer than 80 chars. Any advice on how to
> make xgettext understand the dot?

xgettext is not a full blown php parser.  Maybe making an exception to
the 80 char rule is a good idea for strings that need to be
translated.  Does your application have good separation between
presentation logic and application logic?  Keep in mind that if you
make your own translation method, there will be no xgettext at all.

> Also, how can I specify an entire directory tree to be searched for php files
> rather than just one directory. I did see that one can specify the locations
> to be read from a file, but I couldn't find conclusive instructions on how to
> format that file.

This is trivial with a *nix shell.  I am useless with the windows
shell.  Maybe someone on this list can explain how to emulate the
behavior of "xargs" and "find" in the windows shell.  My solution
would be to install cygwin, but that is like killing a gnat with a
sledgehammer.

> I also tried using poedit to create the catalogs, but all I get is an error
> message that it couldn't find source files. The help doesn't really specify on
> where to set where to look for the source files.

I have only used po edit to open and edit .po files.  The catalogue
manager is for translators who have many projects.  You need to
consolidate all of your po files to one master file, and just open
that with PoEdit.  Use msgmerge to merge po files.

> I guess with some more time I could get used to gettext, but the motto "as
> simple as possible, but not simpler" requires that I redefine what I
> understand under "simple". What makes it even worse, the several tutorials
> that are available all have their own, often overly complicated approach. Most
> create extra functions and wrap functions into new functions.

The expression "as simple as possible, but not simpler" is an Einstein
quote about General Relativity.  Gettext is not quite that bad, but it
ain't simple either.

> I also wonder how gettext differentiates between text strings. It is possible
> that a string in two spots is the same for English, but different for German.
> Since gettext apparently keys off the english string, how can it differentiate?

This is a pitfall, but I have never found it to be an issue. Do you
actually have this problem?  In theory, it seems like it would be a
problem, but in practice I have never found it to be an issue.

You may be trying to translate too many one word phrases.  For
instance, don't do:
_('Showing') ." $pos " . _("of") . " $total";
do :
sprintf(_('Showing %d of %d'),$pos,$total);
or if you need reordering:
sprintf(_('Showing %1$d of %2$d'),$pos,$total);

If it is still a problem, you could hack it with:
You could hack it with:
$text = _("back::as in - scratch my back, not a back button")
$text = preg_replace("/::.*/,'',$text);

> Does anyone know of a good tutorial that doesn't try to get fancy in step 0?

No, but I think you are taking the right approach.  Learn the basics,
and then proceed from there.  You are 60% of the way to a gettext
epiphany.

I didn't know you were trying to do this on a windows enviroment.
Without decent command line tools, it may be tricky to make it
seamless.  Consider writing some .bat files to help with the merging
and building of the po files.

A couple of other things:
Are you using a templating framework?  If you are using smarty, you
will need a plugin.

Does your editor allow regex search and replace? It is really helpful
if you have a bunch of strings.

Regards,
John Campbell



More information about the talk mailing list