module Text.I18N.GetText (
getText,
nGetText,
dGetText,
dnGetText,
dcGetText,
dcnGetText,
bindTextDomain,
textDomain
) where
import Data.Maybe (fromMaybe)
import Foreign.C.Error
import Foreign.C.String
import Foreign.C.Types
import Foreign.Ptr
import System.Locale.SetLocale
foreign import ccall unsafe "libintl.h gettext" c_gettext
:: CString -> IO CString
foreign import ccall unsafe "libintl.h dgettext" c_dgettext
:: CString -> CString -> IO CString
foreign import ccall unsafe "libintl.h dcgettext" c_dcgettext
:: CString -> CString -> CInt -> IO CString
foreign import ccall unsafe "libintl.h ngettext" c_ngettext
:: CString -> CString -> CULong -> IO CString
foreign import ccall unsafe "libintl.h dngettext" c_dngettext
:: CString -> CString -> CString -> CULong -> IO CString
foreign import ccall unsafe "libintl.h dcngettext" c_dcngettext
:: CString -> CString -> CString -> CULong -> CInt -> IO CString
foreign import ccall unsafe "libintl.h bindtextdomain" c_bindtextdomain
:: CString -> CString -> IO CString
foreign import ccall unsafe "libintl.h textdomain" c_textdomain
:: CString -> IO CString
fromCString :: CString -> IO (Maybe String)
fromCString x | x == nullPtr = return Nothing
| otherwise = peekCString x >>= return . Just
fromCStringError :: String -> CString -> IO String
fromCStringError err x | x == nullPtr = throwErrno err
| otherwise = peekCString x
fromCStringDefault :: String -> CString -> IO String
fromCStringDefault d x = fromCString x >>= \r -> return (fromMaybe d r)
fromCStringPluralDefault :: (Eq a, Num a) => String -> String -> a -> CString -> IO String
fromCStringPluralDefault def def_plural n s
| n == 1 = fromCStringDefault def s
| otherwise = fromCStringDefault def_plural s
withCStringMaybe :: Maybe String -> (CString -> IO a) -> IO a
withCStringMaybe Nothing f = f nullPtr
withCStringMaybe (Just str) f = withCString str f
getText :: String -> IO String
getText s =
withCString s $ \s' ->
c_gettext s' >>= fromCStringDefault s
dGetText :: Maybe String
-> String
-> IO String
dGetText domainname msgid =
withCStringMaybe domainname $ \dn' ->
withCString msgid $ \msg' ->
c_dgettext dn' msg' >>= fromCStringDefault msgid
dcGetText :: Maybe String
-> Category
-> String
-> IO String
dcGetText domainname cat msgid =
withCStringMaybe domainname $ \dn' ->
withCString msgid $ \msg' ->
c_dcgettext dn' msg' (categoryToCInt cat) >>=
fromCStringDefault msgid
nGetText :: String
-> String
-> Integer
-> IO String
nGetText msgid msgid_plural n =
withCString msgid $ \msgid' ->
withCString msgid_plural $ \msgid_plural' ->
c_ngettext msgid' msgid_plural' (fromInteger n) >>=
fromCStringPluralDefault msgid msgid_plural n
dnGetText :: Maybe String
-> String
-> String
-> Integer
-> IO String
dnGetText domainname msgid msgid_plural n =
withCStringMaybe domainname $ \dn' ->
withCString msgid $ \msgid' ->
withCString msgid_plural $ \msgid_plural' ->
c_dngettext dn' msgid' msgid_plural' (fromInteger n) >>=
fromCStringPluralDefault msgid msgid_plural n
dcnGetText :: Maybe String
-> Category
-> String
-> String
-> Integer
-> IO String
dcnGetText domainname cat msgid msgid_plural n =
withCStringMaybe domainname $ \dn' ->
withCString msgid $ \msgid' ->
withCString msgid_plural $ \msgid_plural' ->
c_dcngettext dn' msgid' msgid_plural'
(fromInteger n) (categoryToCInt cat) >>=
fromCStringPluralDefault msgid msgid_plural n
bindTextDomain :: String
-> Maybe String
-> IO String
bindTextDomain domainname dirname =
withCString domainname $ \domain ->
withCStringMaybe dirname $ \dir ->
c_bindtextdomain domain dir >>= fromCStringError "bindTextDomain fails"
textDomain :: Maybe String
-> IO String
textDomain domainname =
withCStringMaybe domainname $ \domain ->
c_textdomain domain >>= fromCStringError "textDomain fails"