News for hackers. Stuff that matters.
Re:New string formatting in Python by cbsmith on Monday January 04, 2016 @03:31AM (#10834542)
That's a little misleading.

In practice, what you need is a notion of a "context" for your formatting (often something that is aware of both the current i18n and l10n factors), and that context needs access to the fundamental objects that would be rendered in to a string.

Effectively, the "format string" becomes an identifier for the particular message you want to render, though often the objects themselves are definitive enough.

In that paradigm, interpolation/formatting/whatever might be the default mechanism employed by the context, but you don't want to have that as your explicit mechanism. You want one more level of indirection before you get to it.

Some platforms make the mistake of having that context be tied to the thread, out worse still, a global, and that falls apart once you have any multiplexing logic (and the way python currently works with posix locales fully captures how terribly you can do this). Either way, I'd argue out really is a different problem from interpolation, and if you are trying to solve it using interpolation, you don't understand the problem.

Re:New string formatting in Python by tenfingers on Monday January 04, 2016 @08:16AM (#10835415)
Can you clarify the last point? I do not get it. Can you make an example of where an extra indirect mechanism (besides interpolation) is _required_ for translation?

I generally localize for western languages and write in many programming languages. The combination of gettext + python format strings has been working really great for me, and generally much better than other systems I've seen and put to use. In fact, the simplicity of gettext provides a very fast turn-around, and with translators experienced with the tool I never had problems. Python format strings also work great in this context, as I can supply an arbitrary dictionary of elements that the translator might need. The only real problem has been plural forms in complex text strings, where ngettext is not always sufficient.

What method (name a project I can inspect) do you recommend as a good localization architecture?

Re:New string formatting in Python by cbsmith on Monday January 04, 2016 @12:04PM (#10836713)
You're looking for something like:

     l8n_context.format('file_not_found', file)

'file_not_found' is just an identifier to lookup the actual format template (likely a format string) that will be combined with the file object to render the error message.
Re:New string formatting in Python by tenfingers on Tuesday January 05, 2016 @05:20PM (#10846512)
There's not much difference. In fact, you cannot expect translators to know how the underlying object is handled or write data extractor out of the file object.

If translators are cooperating with you, it's very easy to provide the needed elements directly in the format's dictionary (that is: you extract the translatable pieces for them). It also means you don't have to worry that they're going to fiddle with mutable state.

I generally write everything in english, and do back-translation to my own locale (I also cooperate for translating external projects into my locale), so I eat my own dogfood here.

I know I do not want to deal with extra lower-level subtleties here. Translation is hard already by itself. It's impressive how a good translation of a simple UI can take so much time. If I had to inspect the object to know what I can get out of it I would get crazy.

I'd take a pre-baked dictionary any time.

I've also already used the string-catalog approach in the past (heh, XUL), and I'd personally take gettext any day.

Re:New string formatting in Python by cbsmith on Wednesday January 06, 2016 @11:42AM (#10851495)
But do you see how that extra level of indirection basically maks arguments about the different formatting styles for localization kind of irrelevant?
Re:New string formatting in Python by tenfingers on Wednesday January 06, 2016 @12:22PM (#10851838)
I do, yes. But the translator needs to be aware of the extra indirection to produce the text he needs, and between a custom layer and a standard formatting syntax, the second is definitely friendlier for anyone approaching translation, even when technically less powerful.

At some point the translator will have to format some string himself.

Re:New string formatting in Python by cbsmith on Thursday January 07, 2016 @04:08PM (#10860769)
There's no reason why your "ID" can't be the template for your default language.
Re:New string formatting in Python by eru on Tuesday January 05, 2016 @01:08AM (#10841563)
OK. How is that different from using functions? Conceptually, there's a function that does arbitrary computation under the hood.

(I think we are already agreeing, only that you express your point differently in Python specific terms. I don't care whether you stuff your code into a context object or something else. I was talking about having the full power of the programming language available, vs using a limited language like format strings.)

Re:New string formatting in Python by cbsmith on Wednesday January 06, 2016 @11:41AM (#10851485)
The important part is you have one more level of indirection before you figure out the layout of the message.