NYCPHP Meetup

NYPHP.org

[nycphp-talk] Reading in strings from text file

David Krings ramons at gmx.net
Mon Dec 10 21:31:49 EST 2007


John Campbell wrote:
> I now know:
> - i18n is a hard problem.

Not really when done right, coding is harder.

> - it has already been solved

Well, this is for a picture and video viewer. There are tons out there. I am 
sure that with most of my endeavours in PHP I can find a faster, better, 
safer, more featured version in 5 seconds on the web ready for download and 
use. My goal is to learn on how to do it myself. I do want to learn and get 
experience. That said, my plan was to create string files one for each 
language with the following format
<numeric key>	<tring text in quotes>
or as an example line
12345	"This is a test!"

Plus some simple formatting codes or even a small subset of XHTML tags so that 
I can control line breaks, make text bold, and such. From all string files I 
ever worked with the ones following the <numeric key>	<tring text in quotes> 
format worked the best. As a tool one needs a plain old text editor, one can 
run a spellchecker against the flat file (and it doesn't choke on the numeric 
keys), and when selecting the keys so that they build groups for each section 
or page it is very easy to put new blocks in and take blocks out. Comparisons 
and cross-checks can be made with a spreadsheet, although I would have built a 
simple tool for doing so. And changing anything is as easy as editing the file 
in place or dropping in a new one. Sure, plurals and potential segments would 
need to be their own string, but German being the first language to translate 
to, I know that there isn't an easy algorithm to pluralize the words. It is 
not as easy as in English were slapping and "s" or "es" at the end works in 
99% of the cases. In German there are several endings, the entire word stem 
may change, or the singular word is the same as the plural word and the only 
difference is the different article in front of it. I am very sure that having 
a few strings more in the string file is more efficient than to figure out the 
inner workings of the German language.

> - gettext is the free solution
That is a must anyway.

> - gettext works well as an afterthought to an already existing application.
So how would one go ahead when starting a new application? Not use gettext, 
but something else?

> The caveat is that gettext is "As simple as possible, but not
> simpler".  My advice is to spend a day reading he man pages for
> "msgfmt", "msgmerge", and "msgfmt"  and reading php.net/gettext.

And simple is a very relative term. From what I have seen, gettext's .po 
format is quite different from what I was going for, but it may just be the 
better approach. I start reading and try it with a small sample script.

Thanks for all the pointers.


David



More information about the talk mailing list