i18n-0.2: Internationalization for Haskell

Portabilityportable
Stabilityexperimental
Maintainereugene.grigoriev@gmail.com

Text.I18n.Po

Contents

Description

Internationalization support for Haskell based on GNU gettext (http://www.gnu.org/software/gettext). This module contains PO parser. PO files are assumed to be in UTF-8 encoding.

Plural forms are not yet implemented.

Text.I18n and Control.Monad.Trans are exported for convenience.

Use System.IO.UTF8 whenever you need to output a localized string.

Synopsis

Type Declarations

newtype Msgid Source

Constructors

Msgid String 

Instances

data L10nMode Source

Localization mode. L10nMust will throw an exception if unable to translate whereas L10nMay will just return the original Msgid String. L10nMay is used by localize.

Constructors

L10nMust 
L10nMay 

newtype Locale Source

Constructors

Locale String 

Instances

type I18n a = ReaderT (Locale, L10n, L10nMode, Maybe Context) IO aSource

The Internationalization monad allows the use of IO through liftIO.

type L10n = Map Locale (Map (Maybe Context) (Map Msgid [Msgstr]))Source

The Localization structure.

PO parsing

getL10nSource

Arguments

:: FilePath

Directory containing PO files.

-> IO (L10n, [ParseError])

Localization structure and a list of parse errors.

Builds L10n structure by parsing .po files contained in a given directory. L10n structure is to be passed to localize function. L10n structure is used internaly by the I18n monad.

I18n Monad Functions

localizeSource

Arguments

:: L10n

Structure containing localization data

-> Locale

Locale to use

-> I18n a

Inernationalized action

-> IO a

Localized action

The top level localization function.

 import Text.I18n.Po
 import qualified System.IO.UTF8 as Utf8

 main = do
   (l10n,errors) <- getL10n "dir/to/po"
   localize l10n (Locale "en") impl

gettext :: PrintfType a => String -> I18n aSource

The heart of I18n monad. Based on Text.Printf.printf.

 impl = do
       hello <- gettext "Hello, %s!"
       liftIO (Utf8.putStrLn (hello "Joe"))

withContextSource

Arguments

:: Maybe Context

Context to use

-> I18n a

Internationalized action

-> I18n a

New internationalized action

Sets a local Context for an internationalized action. If there is no translation, then no context version is tried.

 impl2 = withContext (Just "test") impl

withLocaleSource

Arguments

:: Locale

Locale to use

-> I18n a

Internationalized action

-> I18n a

New internationalized action. Note: while this action is localy localized already, it is to be a part of another internationalized action. Therefore the final type is internationalized.

Sets a local Locale for an internationalized action.

 impl3 = withLocale (Locale "ru") impl2

localize'Source

Arguments

:: L10n

Structure containing localization data

-> L10nMode

Localization mode

-> Locale

Locale to use

-> I18n a

Inernationalized action

-> IO a

Localized action

The top level localization function with a mode parameter.

 main2 = localize' (Text.I18n.Po.getL10n "dir/to/po")
                   L10nMust
                   (Locale "en")
                   impl