[Haskell-cafe] haskell i18n best practices
Ertugrul Soeylemez
es at ertes.de
Fri Sep 30 10:47:54 CEST 2011
Paulo Pocinho <pocinho at gmail.com> wrote:
> I don't know if this is a bad habit, but I had already separated the
> dialogue text in the code with variables holding the respective
> strings. At this time, I thought there could be some other way than
> gettext. Then I figured how to import localisation data, that the
> program loads, from external files. The data type is basically a tuple
> with variable-names associated with strings. This is bit like the
> file-embed package [3].
>
> Still uncomfortable with i18n, I learned about the article "I18N in
> Haskell" in yesod blog [4]. I'd like to hear more about it.
>
> What is considered the best practice for localisation?
I can't help you with best practice for Haskell, and I don't think there
is any. Gettext is probably the easiest approach, because it integrates
nicely with the rest of the environment. It automatically uses the
usual LANG and LC_* variables, which are used in Unix-like systems.
An even simpler (but not necessarily easier) approach is to hard-code
the languages in a Map and just look up the string you need. In this
case you have to code the integration yourself. It somewhat sounds like
you are targetting the Windows platform anyway. Personally I'd likely
prefer Gettext for its integration and all the existing translation
tools.
In either case, the best practice is not to work with variables, but
with a default language. You write your text strings in your default
language (usually English), but wrap them in a certain function call.
The function will try to look up a translated message for the current
language. This makes both programming and translating easier. This is
how I imagine it works (or should work):
main :: IO ()
main = do
tr <- getTranslator
putStrLn (tr "This is a test.")
The 'tr' function is called just '_' in other languages, but you can't
use the underscore in Haskell. A translater (person) would use a
program to search your entire source code for those translatable
strings, then they would use a translation program, which shows an
English string and asks them to enter the translated string over and
over, until all strings are translated.
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
More information about the Haskell-Cafe
mailing list