123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139(**************************************************************************)(* ocaml-gettext: a library to translate messages *)(* *)(* Copyright (C) 2003-2008 Sylvain Le Gall <sylvain@le-gall.net> *)(* *)(* This library is free software; you can redistribute it and/or *)(* modify it under the terms of the GNU Lesser General Public *)(* License as published by the Free Software Foundation; either *)(* version 2.1 of the License, or (at your option) any later version; *)(* with the OCaml static compilation exception. *)(* *)(* This library is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)(* Lesser General Public License for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public *)(* License along with this library; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *)(* USA *)(**************************************************************************)(** Concrete implementation based on native gettext library.
@see <http://www.gnu.org/software/gettext/gettext.html/> Gettext library
@author Sylvain Le Gall *)(** {1 Concrete implementations} *)openGettextTypesopenGettextUtilsopenGettextCategory(** Native implementation of gettext. All translation are bound to C library
call. Still use check_format, to ensure that strings follow printf format.
*)moduleNative:GettextTypes.REALIZE_TYPE=struct(**/**)letrealizet=(* Here we do the binding between C library call and the information we
have in parameter t. *)letnative_category_of_categorycat=matchcatwith|GettextCategory.LC_CTYPE->GettextStubCompat.LC_CTYPE|GettextCategory.LC_NUMERIC->GettextStubCompat.LC_NUMERIC|GettextCategory.LC_TIME->GettextStubCompat.LC_TIME|GettextCategory.LC_COLLATE->GettextStubCompat.LC_COLLATE|GettextCategory.LC_MONETARY->GettextStubCompat.LC_MONETARY|GettextCategory.LC_MESSAGES->GettextStubCompat.LC_MESSAGES|GettextCategory.LC_ALL->GettextStubCompat.LC_ALLinletdefault_dir=matcht.pathwithdefault_dir::_->Somedefault_dir|[]->Noneinletbind_textdomain_onetextdomain(codeset_opt,dir_opt)=(letcodeset=matchcodeset_optwithSomecodeset->codeset|None->t.codesetinignore(GettextStubCompat.bind_textdomain_codesettextdomaincodeset));matchdir_optwith|Somedir->ignore(GettextStubCompat.bindtextdomaintextdomaindir)|None->(matchdefault_dirwith|Somedir->ignore(GettextStubCompat.bindtextdomaintextdomaindir)|None->())in(* We only use the first path of t.path, since there is no notion of search
path in native gettext. So the MO file should be in :
- first component of t.path,
- directory pointed by bindtextdomain,
- default directory of gettext.
*)let_=GettextStubCompat.textdomaint.defaultinlet_=matcht.languagewith|Somelanguage->(tryGettextStubCompat.setlocaleGettextStubCompat.LC_ALLlanguagewithFailure_asexc->let()=fail_or_continuet.failsafeexc()inGettextStubCompat.setlocaleGettextStubCompat.LC_ALL"")|None->GettextStubCompat.setlocaleGettextStubCompat.LC_ALL""inlet()=MapCategory.iter(funcatlocale->ignore(GettextStubCompat.setlocale(native_category_of_categorycat)locale))t.categoriesinlet()=MapTextdomain.iterbind_textdomain_onet.textdomainsinfunprintf_formattextdomain_optstr_idstr_plural_optcat->letcheckx=ifprintf_formatthenmatchGettextFormat.check_formatt.failsafe(Singular(str_id,x))with|Singular(_,str)->str|_->str_idelsexinletncat=native_category_of_categorycatinlettextdomain=matchtextdomain_optwith|Sometextdomain->textdomain|None->t.defaultinlettranslation=matchstr_plural_optwith|Some(str_plural,n)->GettextStubCompat.dcngettexttextdomainstr_idstr_pluralnncat|None->GettextStubCompat.dcgettexttextdomainstr_idncatinchecktranslationend(** Native implementation of gettext. Use the Native module, but use
informations provided to preload all textdomain translation. The preload is
made by trying to translate the string "", which is mandatory in MO file.
This is not the default behavior of gettext. Use this module if you know
that it is better to preload all string. Don't use this module if you think
you will only have a few strings to translate. *)modulePreload:GettextTypes.REALIZE_TYPE=struct(**/**)letrealizet=lett'=Native.realizetinlet()=MapTextdomain.iter(funtextdomain_->(* We only load LC_MESSAGES, since it is what is mainly use with
* gettext. Anyway, this is just a local optimization...
*)ignore(t'false(Sometextdomain)""NoneLC_MESSAGES))t.textdomainsint'end