Archæology

The assorted finds of Artefact Publishing

Internationalisation madness

I don’t understand gettext (specifically the Python implementation using classes). For a while there I thought I did (I could change my LANG from en_NZ.UTF-8 to nl_NL.UTF-8 and it would give me messages in Dutch), but the more I’ve worked with it, the less I understand (and the less it works). I have tried to find other Python code which uses it, but haven’t been particularly fortunate in that search. The web has let me down with regards to a tutorial on the matter, and the manual doesn’t seem to cover the questions I have.

This does create an opportunity, however, for me to use CVS in new ways. For there is a bug in all released versions of IPA Zounds which is fixed in the development version which is currently suffering the instabilities of the gettext problem(s). This means I need to create a branch and make a new release off that which just fixes the bug. (Speaking of that bug, is there a convenient method within a regular expression that I’ve missed for replacing [abc] with [def], where a is replaced by d, b with e, etc?)

So, a-branching I go.

Posted by jamie on December 1, 2003 18:48+13:00

Comments

To replace abc with def, Perl offers the tr operator (stands for “translate” I think).

You’d write something like tr/abc/def/ to get the effect you want.

I can’t remember all of the details. Surely Python has something similar.

Posted by: michael on December 2, 2003 11:14+13:00

Yeah, I know about tr in Perl. The problem is that in this instance the [abc] is part of a regular expression rather than simply being part of a string. For example, here’s a pattern used in one of the zounds unit tests: (?P<start>[aeiouy])(?P<match>[fsq])(?=[aeiouy]) with the group being replaced by one three different characters, corresponding with f, s, or q.

My current solution is to simply create new patterns for each character to be substituted. If there was an elegant way to avoid that looping, though, I would be interested.

Posted by: Jamie on December 2, 2003 12:38+13:00

I think I might do this by first doing a simple match with the thing (your [abc]) to be replaced marked out as a group. In Perl:

$var =~ m/pattern([abc])/;

And then I'd calculate the new string by calling some function to which I'd pass $1 (which would be one of "a", "b" or "c"). No doubt you'd want other pattern groups also available to the calculating function, so your pattern would have more parentheses in it.

This would avoid the loop you mention.

Perl does provide a e option to its search-replace command, so that you could call the function I describe above from within the RHS of the search-replace command. If you look at the man-page for perlop, there's an example there of replacing a string of digits with their value doubled.

The problem with this approach would be one of readability, so even if Python had a similar /e functionality, I might stick with my original plan.

Posted by: Michael on December 17, 2003 13:58+13:00