Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file gettextCompile.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192(**************************************************************************)(* 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 *)(**************************************************************************)(** Functions for extraction/compilation/installation of PO/MO file.
@author Sylvain Le Gall
*)openGettextTypesopenFileUtilopenFilePathletpo_of_filenamefilename=letchn=tryopen_infilenamewithSys_errorstr->raise(CompileProblemReadingFile(filename,str))inletpo=GettextPo.input_pochninclose_inchn;po(** extract cmd default_option file_options src_files ppf : extract the
translatable strings from all the src_files provided. Each source file will
be extracted using the command cmd, which should be an executable that has
the same output as ocaml-xgettext. If cmd is not provided, it will be
searched in the current path. The command will be called with
default_option, or if the file being extracted is mapped in file_options,
with the option associated to the filename in file_options. The result will
be written using module Format to the formatter ppf. The result of the
extraction should be used as a po template file.
*)letextractcommanddefault_optionsfilename_optionsfilename_lstfilename_pot=letmake_commandoptionsfilename=Printf.sprintf"%s %s %s"commandoptionsfilenameinletextract_onepofilename=letoptions=tryMapString.findfilenamefilename_optionswithNot_found->default_optionsinletreal_command=make_commandoptionsfilenameinletchn=Unix.open_process_inreal_commandinletvalue=set_binary_mode_inchntrue;(Marshal.from_channelchn:po_content)inmatchUnix.close_process_inchnwith|Unix.WEXITED0->GettextPo.merge_popovalue|Unix.WEXITEDexit_code->raise(CompileExtractionFailed(filename,real_command,exit_code))|Unix.WSIGNALEDsignal|Unix.WSTOPPEDsignal->raise(CompileExtractionInterrupted(filename,real_command,signal))inletextraction=List.fold_leftextract_oneGettextPo.empty_pofilename_lstinletchn=open_outfilename_potinletdate=letcurrent_time=Unix.time()inletgmt_time=Unix.gmtimecurrent_timeinPrintf.sprintf"%04d-%02d-%02d %02d:%02d+0000"(gmt_time.Unix.tm_year+1900)(gmt_time.Unix.tm_mon+1)gmt_time.Unix.tm_mdaygmt_time.Unix.tm_hourgmt_time.Unix.tm_mininPrintf.fprintfchn"# SOME DESCRIPTIVE TITLE.\n\
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER\n\
# This file is distributed under the same license as the PACKAGE package.\n\
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n\
#\n\
#, fuzzy\n\
msgid \"\"\n\
msgstr \"\"\n\
\"Project-Id-Version: PACKAGE VERSION\\n\"\n\
\"Report-Msgid-Bugs-To: \\n\"\n\
\"POT-Creation-Date: %s\\n\"\n\
\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n\
\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"\n\
\"Language-Team: LANGUAGE <LL@li.org>\\n\"\n\
\"MIME-Version: 1.0\\n\"\n\
\"Content-Type: text/plain; charset=CHARSET\\n\"\n\
\"Content-Transfer-Encoding: 8bit\\n\"\n\
\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n"date;GettextPo.output_pochnextraction;close_outchn(** compile input_po output_mo : create a binary representation of the PO file
provided as input_pot. The output file is output_mo.
*)letcompilefilename_pofilename_mo=letpo=po_of_filenamefilename_poinletoutput_one_mapfilenamemap=letlst=MapString.fold(fun_commented_po_translationlst->letpo_translation=commented_po_translation.po_comment_translationinGettextPo.translation_of_po_translationpo_translation::lst)map[]inletchn=open_out_binfilenameinGettextMo.output_mochnlst;close_outchninletmake_filenamedomainfilename_mo=letdirname=dirnamefilename_moinletbasename=basenamefilename_moin(* BUG : should use add_extension *)make_filename[dirname;domain^"."^basename]inoutput_one_mapfilename_mopo.no_domain;MapTextdomain.iter(fundomainmap->output_one_map(make_filenamedomainfilename_mo)map)po.domain(** install destdir language category textdomain fln : copy the given
filename ( should be a MO file ) to the filename defined by all the
other parameters ( typically destdir/language/category/textdomain.mo ).
*)letinstallstrictdestdirlanguagecategorytextdomainfilename_mo_src=letfilename_mo_dst=GettextDomain.make_filenamedestdirlanguagecategorytextdomaininletdirname_mo_dst=dirnamefilename_mo_dstin(* Test of the mo file, it will raise an exception if there is any problem
in the MO structure *)let(),_=GettextMo.fold_mo(ifstrictthenRaiseExceptionelseInformStderr(function|MoInvalidPlurals_ase->Gettext.string_of_exceptione|e->raisee))(fun_x()->())()filename_mo_srcinmkdir~parent:truedirname_mo_dst;cp[filename_mo_src]filename_mo_dst(** uninstall orgdir language category textdomain : remove the MO file
defined by all the other parameters
( typically destdir/language/category/textdomain.mo ).
*)letuninstallorgdirlanguagecategorytextdomain=letfilename_mo_org=GettextDomain.make_filenameorgdirlanguagecategorytextdomaininrm[filename_mo_org](** merge fln_pot fln_po_lst backup_ext : use fln_pot as a POT file and
merge the current content of the listed PO file ( fln_po_lst ) with it.
Backup all the PO file using the provided backup extension backup_ext and
produce a merged PO file in place.
*)letmergefilename_potfilename_po_lstbackup_extension=letpot=po_of_filenamefilename_potinletmerge_onefilename_po=letpo=po_of_filenamefilename_poinletpo_merged=GettextPo.merge_potpotpoinlet_=(* BUG: should use add_extension *)(* BUG: should use mv *)Sys.renamefilename_po(filename_po^"."^backup_extension)inletchn=open_outfilename_poinGettextPo.output_pochnpo_merged;close_outchninList.itermerge_onefilename_po_lst