Difference between revisions of "Internationalization of Haskell programs"

From HaskellWiki
Jump to navigation Jump to search
(Internationalization of Haskell programs moved to Internationalization of Haskell programs using gettext: Allowing for listing other approaches to i18n on the general page.)
 
(Write a page describe three methods of i18n in Haskell)
Line 1: Line 1:
  +
== Approaches to internationalization in Haskell ==
#REDIRECT [[Internationalization of Haskell programs using gettext]]
 
  +
  +
There are several different approaches you can use to internationalize
  +
your Haskell program.
  +
  +
=== Using GNU gettext ===
  +
  +
You can internationalize your program using
  +
[http://www.gnu.org/software/gettext/ GNU gettext]
  +
and its Haskell bindings package
  +
[http://hackage.haskell.org/package/hgettext hgettext].
  +
  +
Set up your translations and integrate them into your
  +
application using
  +
[[Internationalization of Haskell programs using gettext|these instructions]].
  +
  +
=== Using native Haskell data types ===
  +
  +
You can internationalize your program using native Haskell data types.
  +
  +
Represent the individual
  +
texts to be translated as constructors of a Haskell data
  +
type. Then provide a function that automatically renders
  +
the texts appropriately in the current language context.
  +
  +
As an example, see
  +
[[Internationalization of Haskell programs using Haskell data types|this description]]
  +
of how this approach is implemented in the
  +
[[Web/Frameworks#Yesod|Yesod web framework]].
  +
  +
=== Using the Grammatical Framework ===
  +
  +
You can internationalize your program using the
  +
[http://www.grammaticalframework.org/ Grammatical Framework] (GF).
  +
  +
The GF provides a way to define human-language-independent syntax
  +
for expressing texts in Haskell.
  +
The GF can then render the texts automatically
  +
in any human language for which
  +
an appropriate GF grammer exists.
  +
  +
See this
  +
[https://github.com/creswick/gfI8N very simple example]
  +
of an application internationalized using GF.
  +
It is based on the "Foods" grammar (included in the example),
  +
so it's quite contrived,
  +
but it should be enough to get you started.
  +
  +
See the
  +
[http://www.grammaticalframework.org/download/index.html GF download page]
  +
for information about how to install GF.
  +
The standard installation of GF currently includes grammars
  +
for at least 26 languages.
  +
  +
== Comparison of approaches to internationalization ==
  +
  +
=== GNU gettext ===
  +
  +
Advantages:
  +
  +
* Easiest integration with other tools and programming languages
  +
* Little or no specialized knowledge required of translators
  +
* Little or no interaction needed between programmers and translators
  +
* Well-known and well-documented
  +
  +
Disadvantages:
  +
  +
* Inflexible use of static text literals creates awkwardness when there are complex differences between how the same idea is expressed in different languages
  +
* Translation selection happens at runtime, so there is no type safety
  +
* Texts are loaded from external files at runtime, which creates overhead and deployment issues
  +
* Requires a moderate amount of work to set up and integrate
  +
  +
=== Native Haskell data types ===
  +
  +
Advantages:
  +
  +
* Easy to implement in Haskell
  +
* Compile-time type safety
  +
* Fast
  +
* Flexible in handling complex differences between languages
  +
  +
Disadvantages:
  +
  +
* May require some training of translators and/or cooperative integration work between translators and programmers, depending on the level of sophistication needed in the rendering functions
  +
* May lead to many string literals in a Haskell source file. This requires a work-around for a current limitation of the GHC compiler.
  +
  +
=== Grammatical Framework ===
  +
  +
Advantages:
  +
  +
* Translations automatically generated in all languages for which GF grammars exist, without the need for human translators
  +
* High quality translations
  +
  +
Disadvantages:
  +
  +
* Learning curve for the programmer to express texts using existing GF grammars, and to extend GF grammars as needed
  +
* Extra work needed if you must support languages that do not yet have a GF grammar
  +
* Installing GF is not quite as simple as installing the usual Haskell package
  +
* A human translator may still be needed for domain-specific words and expressions not included in the standard GF grammars
  +
  +
== Other tools ==
  +
  +
The following tools may also be useful when internationalizing your Haskell program:
  +
  +
=== file-embed ===
  +
  +
Internationalization often leads to a large number of literal
  +
strings in a Haskell source file. This creates a technical
  +
problem due to a current limitation of the GHC compiler -
  +
the GHC compiler does not behave well when compiling a source
  +
file with a large number of literal strings.
  +
  +
One classic work-around to this problem is to place the literal
  +
strings in a small C library and import them via the FFI.
  +
  +
Another work-around, based on Template Haskell, is provided by
  +
the
  +
[http://hackage.haskell.org/package/file-embed file-embed]
  +
package.
  +
  +
=== numerals ===
  +
  +
The
  +
[http://hackage.haskell.org/package/numerals numerals]
  +
package renders numbers
  +
(currently only cardinal numbers)
  +
as text in many different languages.

Revision as of 12:02, 3 October 2011

Approaches to internationalization in Haskell

There are several different approaches you can use to internationalize your Haskell program.

Using GNU gettext

You can internationalize your program using GNU gettext and its Haskell bindings package hgettext.

Set up your translations and integrate them into your application using these instructions.

Using native Haskell data types

You can internationalize your program using native Haskell data types.

Represent the individual texts to be translated as constructors of a Haskell data type. Then provide a function that automatically renders the texts appropriately in the current language context.

As an example, see this description of how this approach is implemented in the Yesod web framework.

Using the Grammatical Framework

You can internationalize your program using the Grammatical Framework (GF).

The GF provides a way to define human-language-independent syntax for expressing texts in Haskell. The GF can then render the texts automatically in any human language for which an appropriate GF grammer exists.

See this very simple example of an application internationalized using GF. It is based on the "Foods" grammar (included in the example), so it's quite contrived, but it should be enough to get you started.

See the GF download page for information about how to install GF. The standard installation of GF currently includes grammars for at least 26 languages.

Comparison of approaches to internationalization

GNU gettext

Advantages:

  • Easiest integration with other tools and programming languages
  • Little or no specialized knowledge required of translators
  • Little or no interaction needed between programmers and translators
  • Well-known and well-documented

Disadvantages:

  • Inflexible use of static text literals creates awkwardness when there are complex differences between how the same idea is expressed in different languages
  • Translation selection happens at runtime, so there is no type safety
  • Texts are loaded from external files at runtime, which creates overhead and deployment issues
  • Requires a moderate amount of work to set up and integrate

Native Haskell data types

Advantages:

  • Easy to implement in Haskell
  • Compile-time type safety
  • Fast
  • Flexible in handling complex differences between languages

Disadvantages:

  • May require some training of translators and/or cooperative integration work between translators and programmers, depending on the level of sophistication needed in the rendering functions
  • May lead to many string literals in a Haskell source file. This requires a work-around for a current limitation of the GHC compiler.

Grammatical Framework

Advantages:

  • Translations automatically generated in all languages for which GF grammars exist, without the need for human translators
  • High quality translations

Disadvantages:

  • Learning curve for the programmer to express texts using existing GF grammars, and to extend GF grammars as needed
  • Extra work needed if you must support languages that do not yet have a GF grammar
  • Installing GF is not quite as simple as installing the usual Haskell package
  • A human translator may still be needed for domain-specific words and expressions not included in the standard GF grammars

Other tools

The following tools may also be useful when internationalizing your Haskell program:

file-embed

Internationalization often leads to a large number of literal strings in a Haskell source file. This creates a technical problem due to a current limitation of the GHC compiler - the GHC compiler does not behave well when compiling a source file with a large number of literal strings.

One classic work-around to this problem is to place the literal strings in a small C library and import them via the FFI.

Another work-around, based on Template Haskell, is provided by the file-embed package.

numerals

The numerals package renders numbers (currently only cardinal numbers) as text in many different languages.