Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file gettextTranslate.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236(**************************************************************************)(* 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 *)(**************************************************************************)(** Signature of module for translation storage / access.
@author Sylvain Le Gall
*)openGettextTypesopenGettextUtilsopenGettextMoopenGettextFormatmoduletypeTRANSLATE_TYPE=sigtypeuvalcreate:t->filename->(string->string)->u(** create t filename recode : Create a translation
table using filename as the mo file and recode as the encoding
converter.
*)(* BUG : need update *)valtranslate:u->bool->string->(string*int)option->string(** translate str (plural_form,number) tbl : translate the string
str using tbl. It is possible that the operation modify tbl,
so it is returned also. It is also possible to get the plural
form of the translated string using plural_form and number.
*)endmoduleDummy:TRANSLATE_TYPE=structtypeu=string->stringletcreate_t_filenamecharset=charsetlettranslatecharsetprintf_formatstrplural_form=matchplural_formwith|None->charsetstr|Some(str_plural,x)->letcheck=ifprintf_formatthencheck_formatIgnoreelsefunx->xincharset(get_translated_valueIgnore(check(Plural(str,str_plural,[])))(germanic_pluralx))endmoduleMap:TRANSLATE_TYPE=structtypeu={dummy:Dummy.u;map:translationMapString.t;failsafe:failsafe;fun_plural_forms:int->int;}letcreatetfilenamecharset=letmap,fun_plural_forms=fold_mot.GettextTypes.failsafe(funtranslationaccu->matchtranslationwith|Singular(str_id,str)->MapString.addstr_id(Singular(str_id,charsetstr))accu|Plural(str_id,str_plural,lst)->MapString.addstr_id(Plural(str_id,str_plural,List.mapcharsetlst))accu)MapString.emptyfilenamein{dummy=Dummy.createtfilenamecharset;map;failsafe=t.GettextTypes.failsafe;fun_plural_forms;}lettranslateuprintf_formatstrplural_form=tryletplural_number=u.fun_plural_forms(matchplural_formwithSome(_,x)->x|None->0)inletcheck=ifprintf_formatthencheck_formatu.failsafeelsefunx->xinget_translated_valueu.failsafe(check(MapString.findstru.map))plural_numberwithNot_found->fail_or_continueu.failsafe(TranslateStringNotFoundstr)(Dummy.translateu.dummyprintf_formatstrplural_form)endmoduleHashtbl:TRANSLATE_TYPE=structtypeu={dummy:Dummy.u;hashtbl:(string,translation)Hashtbl.t;failsafe:failsafe;fun_plural_forms:int->int;}letcreatetfilenamecharset=lethashtbl,fun_plural_forms=fold_mot.GettextTypes.failsafe(funtranslationaccu->matchtranslationwith|Singular(str_id,str)->Hashtbl.addaccustr_id(Singular(str_id,charsetstr));accu|Plural(str_id,str_plural,lst)->Hashtbl.addaccustr_id(Plural(str_id,str_plural,List.mapcharsetlst));accu)(* 32 is only a guest on the number of string contains in the
future table *)(Hashtbl.create32)filenamein{dummy=Dummy.createtfilenamecharset;hashtbl;failsafe=t.GettextTypes.failsafe;fun_plural_forms;}lettranslateuprintf_formatstrplural_form=tryletplural_number=u.fun_plural_forms(matchplural_formwithSome(_,x)->x|None->0)inletcheck=ifprintf_formatthencheck_formatu.failsafeelsefunx->xinget_translated_valueu.failsafe(check(Hashtbl.findu.hashtblstr))plural_numberwithNot_found->fail_or_continueu.failsafe(TranslateStringNotFoundstr)(Dummy.translateu.dummyprintf_formatstrplural_form)endmoduleOpen:TRANSLATE_TYPE=structtypeu={dummy:Dummy.u;filename:filename;charset:string->string;failsafe:failsafe;fun_plural_forms:int->int;number_of_strings:int;}letcreatetfilenamecharset=(* Processing of the file *)letchn=open_in_binfilenameinletheader=input_mo_headerchninletinformations=input_mo_informationst.GettextTypes.failsafechnheaderinclose_inchn;{dummy=Dummy.createtfilenamecharset;filename;charset;failsafe=t.GettextTypes.failsafe;fun_plural_forms=informations.GettextTypes.fun_plural_forms;number_of_strings=Int32.to_intheader.GettextTypes.number_of_strings;}lettranslateuprintf_formatstrplural_form=letchn=open_in_binu.filenameinletres=tryletplural_number=u.fun_plural_forms(matchplural_formwithSome(_,x)->x|None->0)inletheader=input_mo_headerchninletrecfind_str_id(start_index,end_index)=letmiddle_index=(start_index+end_index)/2inletstr_id=letlst_str_id=input_mo_untranslatedu.failsafechnheadermiddle_indexinmatchlst_str_idwith|str_id::_->str_id|[]->(* BUG : should be a real exception *)raiseNot_foundinmatchString.comparestrstr_idwith|xwhenx<0&&start_index<=middle_index-1->find_str_id(start_index,middle_index-1)|xwhenx>0&&middle_index+1<=end_index->find_str_id(middle_index+1,end_index)|xwhenx=0->middle_index|_->raiseNot_foundinlettranslation=lettranslation=input_mo_translationu.failsafechnheader(find_str_id(0,u.number_of_strings-1))inmatchtranslationwith|Singular(str_id,str)->Singular(str_id,u.charsetstr)|Plural(str_id,str_plural,lst)->Plural(str_id,str_plural,List.mapu.charsetlst)inget_translated_valueu.failsafetranslationplural_numberwithNot_found->fail_or_continueu.failsafe(TranslateStringNotFoundstr)(Dummy.translateu.dummyprintf_formatstrplural_form)inclose_inchn;resend