| Portability | portable |
|---|---|
| Stability | experimental |
| Maintainer | eugene.grigoriev@gmail.com |
Text.I18n.Po
Description
Internationalization support for Haskell. This module contains PO parser. PO files are assumed to be in UTF-8 encoding.
Plural forms are not yet implemented.
modules Text.I18n, Control.Monad.Trans, and function putStrLn and putStr from System.IO.UTF8 are exported for convenience.
import Prelude hiding (putStr,putStrLn)
- newtype Msgid = Msgid String
- type Msgstr = String
- newtype Locale = Locale String
- type Context = String
- type I18n a = ReaderT (Locale, L10n, Maybe Context) Identity a
- type L10n = Map Locale (Map (Maybe Context) (Map Msgid [Msgstr]))
- getL10n :: FilePath -> IO (L10n, [ParseError])
- localize :: L10n -> Locale -> I18n a -> a
- gettext :: PrintfType a => String -> I18n a
- withContext :: Maybe Context -> I18n a -> I18n a
- withLocale :: Locale -> I18n a -> I18n a
- putStrLn :: String -> IO ()
- putStr :: String -> IO ()
- module Control.Monad.Trans
Type Declarations
type I18n a = ReaderT (Locale, L10n, Maybe Context) Identity aSource
The Internationalization monad built using monad transformers.
PO parsing
Arguments
| :: FilePath | Directory containing PO files. |
| -> IO (L10n, [ParseError]) | Localization structure and a list of parse errors. |
I18n Monad Functions
Arguments
| :: L10n | Structure containing localization data |
| -> Locale | Locale to use |
| -> I18n a | Inernationalized expression |
| -> a | Localized expression |
The top level localization function.
import Text.I18n.Po
import Prelude hiding (putStr,putStrLn)
main = do
(l10n,errors) <- getL10n "dir/to/po" -- directory containing PO files
putStrLn $ localize l10n (Locale "en") (example "Joe")
gettext :: PrintfType a => String -> I18n aSource
The heart of I18n monad. Based on Text.Printf.printf.
example :: String -> I18n String
example name = do
hello <- gettext "Hello, %s!"
return (hello name)
Arguments
| :: Maybe Context | Context to use |
| -> I18n a | Internationalized expression |
| -> I18n a | New internationalized expression |
Sets a local Context for an internationalized expression.
If there is no translation, then no context version is tried.
example2 :: String -> I18n String example2 = withContext (Just "test") . example
Arguments
| :: Locale | Locale to use |
| -> I18n a | Internationalized expression |
| -> I18n a | New internationalized expression. Note: while this expression is localy localized already, it is to be a part of another internationalized expression. Therefore the final type is internationalized. |
Sets a local Locale for an internationalized expression.
example3 :: String -> I18n String example3 = withLocale (Locale "ru") . example2
UTF-8 Output Functions
module Control.Monad.Trans