1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--**********************************************************************-->
<!-- 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 -->
<!--**********************************************************************-->
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
"/usr/share/xml/schema/dtd/4.3/docbookx.dtd" [
<!-- Source code -->
<!ENTITY expl '../examples'>
<!ENTITY xi 'http://www.w3.org/2001/XInclude'>
<!ENTITY fbk '<xi:fallback xmlns:xi="ξ">TO DO</xi:fallback>'>
<!ENTITY po-makefile '<xi:include href="&expl;/po/Makefile" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY LINGUAS '<xi:include href="&expl;/po/LINGUAS" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY POTFILES '<xi:include href="&expl;/po/POTFILES" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY makefile '<xi:include href="&expl;/program/Makefile" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY library-ml '<xi:include href="&expl;/library/library.ml" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY library-gettext-ml '<xi:include href="&expl;/library/libraryGettext.ml" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY program-ml '<xi:include href="&expl;/program/program.ml" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY program-gettext-ml '<xi:include href="&expl;/program/programGettext.ml" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY program-xml '<xi:include href="&expl;/doc/program.xml" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY makefile-doc '<xi:include href="&expl;/doc/Makefile" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY gui-glade '<xi:include href="&expl;/gui/gui.glade" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY gui-ml '<xi:include href="&expl;/gui/gui.ml" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY makefile-gui '<xi:include href="&expl;/gui/Makefile" parse="text" xmlns:xi="ξ">&fbk;</xi:include>' >
<!ENTITY ocaml-gettext-manual '<xi:include href="ocaml-gettext.xml" xmlns:xi="ξ" xpointer="xpointer(/refentry/node())"/>' >
<!ENTITY ocaml-xgettext-manual '<xi:include href="ocaml-xgettext.xml" xmlns:xi="ξ" xpointer="xpointer(/refentry/node())"/>' >
<!ENTITY ocaml-gettext-options-manual '<xi:include href="ocaml-gettext-options.xml" xmlns:xi="ξ" xpointer="xpointer(/refentry/node())"/>' >
<!-- Links -->
<!ENTITY download-ocaml 'http://caml.inria.fr/ocaml/release.en.html'>
<!ENTITY site-ocaml 'http://caml.inria.fr/'>
<!ENTITY download-gettext 'http://www.gnu.org/software/gettext/gettext.html#TOCdownloading'>
<!ENTITY site-gettext 'http://www.gnu.org/software/gettext/gettext.html'>
<!ENTITY site-documentation-gettext 'http://www.gnu.org/software/gettext/manual/gettext.html'>
<!ENTITY download-camomile 'http://prdownloads.sourceforge.net/camomile/'>
<!ENTITY site-camomile 'http://camomile.sourceforge.net/'>
<!ENTITY download-ocaml-fileutils 'http://www.gallu.homelinux.org/download/'>
<!ENTITY site-ocaml-fileutils 'http://www.gallu.homelinux.org/ocaml-fileutils.html'>
<!ENTITY download-ounit 'http://www.xs4all.nl/~mmzeeman/ocaml/'>
<!ENTITY site-ounit 'http://www.xs4all.nl/~mmzeeman/ocaml/'>
<!ENTITY download-ocaml-benchmark 'http://sourceforge.net/project/showfiles.php?group_id=117069'>
<!ENTITY site-ocaml-benchmark 'http://ocaml-benchmark.sourceforge.net/'>
<!ENTITY download-docbook 'http://prdownloads.sourceforge.net/docbook/'>
<!ENTITY site-docbook 'http://docbook.sourceforge.net/'>
<!ENTITY download-xsltproc 'http://xmlsoft.org/XSLT/downloads.html'>
<!ENTITY download-findlib 'http://www.ocaml-programming.de/packages/'>
<!ENTITY site-gtranslator 'http://gtranslator.sourceforge.net/'>
<!ENTITY site-kbabel 'http://i18n.kde.org/tools/kbabel/'>
<!ENTITY download-fop 'http://www.apache.org/dyn/closer.cgi/xml/fop'>
]>
<book>
<title>ocaml-gettext reference manual</title>
<bookinfo>
<legalnotice>
<para>
This user manual is considered as a source code and is licensed under the GNU Lesser General Public
License v2.1 with OCaml static compilation exception.
</para>
</legalnotice>
<author>
<firstname>Sylvain</firstname>
<surname>Le Gall</surname>
</author>
<copyright>
<year>2005</year>
<holder>Sylvain Le Gall</holder>
</copyright>
<revhistory>
<revision>
<!-- BUG: to complete wiht the right tags -->
<revnumber>svn:revision</revnumber>
<date>svn:date</date>
</revision>
</revhistory>
</bookinfo>
<chapter>
<title>Overview</title>
<section>
<title>What is ocaml-gettext ?</title>
<para>
ocaml-gettext is a library to support string translation in OCaml. It provides a simple interface
to help programmers and translators to create programs that can support different languages. This
allows the internationalisation of programs.
</para>
<para>
ocaml-gettext provides two main functionalities:
<itemizedlist>
<listitem>
<para>
translate English strings into localised strings ( depending on which gettext data are installed ),
</para>
</listitem>
<listitem>
<para>
convert charset from the one used by the translator into the charset of the user.
</para>
</listitem>
</itemizedlist>
</para>
<para>The library is build according three points of view:
<itemizedlist>
<listitem>
<para>
for the programmer: this library provides functions that can be used in OCaml,
</para>
</listitem>
<listitem>
<para>
for the translator: this library provides a standard file format ( PO file ) to help the translator,
</para>
</listitem>
<listitem>
<para>
for the user: this library provides a set of command line options to set the language
and the charset.
</para>
</listitem>
</itemizedlist>
</para>
<para>
ocaml-gettext was initially only a wrapper of gettext. It comes with a patch against the source of
xgettext to add support of the OCaml language. As i already used this approach,
i was convinced that this library should be better integrated by using more advanced features of
OCaml ( such as <command>camlp4</command> ).
</para>
<para>
As a result of porting gettext to ocaml-gettext, we have :
<itemizedlist>
<listitem>
<para>
a library that can understood the native format of gettext file ( MO file ),
</para>
</listitem>
<listitem>
<para>
two implementations: a wrapper around gettext and a pure OCaml implementation using
camomile,
</para>
</listitem>
<listitem>
<para>
<command>ocaml-gettext</command>: a command line tool to help you to extract, to merge and to install
gettext data.
</para>
</listitem>
</itemizedlist>
</para>
</section>
<section>
<title>How is ocaml-gettext related to gettext ?</title>
<para>
ocaml-gettext is a close cousin of gettext. In fact, the API is based on gettext. Almost everything
in ocaml-gettext is compatible with gettext:
<itemizedlist>
<listitem>
<para>functions provided in the API are very close to the gettext one,</para>
</listitem>
<listitem>
<para>the library contains a binding of the gettext library,</para>
</listitem>
<listitem>
<para>
all the file used are gettext compatible: the library uses PO file for translator
and MO file for translation,
</para>
</listitem>
<listitem>
<para>
the library tries to use the same initialisation sequence as gettext :
it uses the same environment variable, tries to find MO files in the
same places...
</para>
</listitem>
</itemizedlist>
</para>
<para>
This documentation will not covered the point that are better explained in the
<link linkend="gettext-documentation">gettext documentation</link>. It is highly recommended
to read this documentation, before reading this manual. Most of the point that are explained there
are not explained again here. However, we have tried to be as precise as possible to enable
programming with ocaml-gettext without having the need to be a gettext expert.
</para>
</section>
</chapter>
<chapter>
<title>How to install ocaml-gettext</title>
<section>
<title>Using source</title>
<para>To compile ocaml-gettext, you need to install the following prerequisites :
<itemizedlist>
<listitem>
<para>
<ulink url="&download-ocaml;">OCaml</ulink> v3.10.1 or later,
</para>
</listitem>
<listitem>
<para>
<ulink url="&download-findlib;">findlib</ulink> v1.0.4 or later,
</para>
</listitem>
<listitem>
<para>
<ulink url="&download-ocaml-fileutils;">ocaml-fileutils</ulink> v0.3.0 or later,
</para>
</listitem>
<listitem>
<para>
<ulink url="&download-camomile;">camomile</ulink> v0.7.1 or later,
</para>
</listitem>
<listitem>
<para>
<ulink url="&download-gettext;">gettext</ulink> v0.14.3 or later,
</para>
</listitem>
<listitem>
<para>
<ulink url="&download-ounit;">OUnit</ulink> v1.0.1 or later
<footnote id="build-test"><para>Only if you want to build unitary test tool</para></footnote>,
</para>
</listitem>
<listitem>
<para>
<ulink url="&download-ocaml-benchmark;">ocaml-benchmark</ulink> v0.6 or later
<footnote id="build-benchmark"><para>Only if you want to build benchmarking tool</para></footnote>,
</para>
</listitem>
<listitem>
<para>
<ulink url="&download-docbook;">Docbook DTD and stylesheets</ulink> v4.3
<footnote id="build-doc"><para>Only if you want to build the documentation</para></footnote>,
</para>
</listitem>
<listitem>
<para>
<ulink url="&download-xsltproc;">xsltproc</ulink> v1.1.12 or later,
<footnoteref linkend="build-doc"/>,
</para>
</listitem>
<listitem>
<para>
<ulink url="&download-fop;">fop</ulink> v0.20.5 or later.
<footnoteref linkend="build-doc"/>,
</para>
</listitem>
</itemizedlist>
</para>
<para>
Camomile and gettext are optional but you need at least one of them in order to be able to build
the command line tool <command>ocaml-gettext</command>.
</para>
<para>
After having build and install all prerequisites, extract the source
code of ocaml-gettext to a directory. Go to the source directory and type :
<itemizedlist>
<listitem>
<para>
<command>./configure</command>
<footnote>
<para>
If needed, you can use <command>./configure <option>--help</option></command>
to have a complete help on every option you can use to tweak the
installation of ocaml-gettext. To enable documentation generation, use
<option>--enable-doc</option>. To enable benchmark executable, use
<option>--enable-bench</option>. To enable test executable, use
<option>--enable-test</option>.
</para>
</footnote>
</para>
</listitem>
<listitem>
<para>
<command>make</command>
</para>
</listitem>
<listitem>
<para>
<command>make install</command>
</para>
</listitem>
<listitem>
<para>
<command>cd test</command>
</para>
</listitem>
<listitem>
<para>
<command>./test</command>
<footnoteref linkend="build-test"/>
</para>
</listitem>
<listitem>
<para>
<command>./benchmark</command>
<footnoteref linkend="build-benchmark"/>
</para>
</listitem>
</itemizedlist>
</para>
<para>
Since ocaml-gettext is not yet a stable release, building the benchmark and the unitary test tools
is important, especially for debugging purpose. If you encounter problems, you should first
try to run this two commands. They will give you good hints on what causes the problem.
</para>
<note>
<para>
There is a <filename>patches</filename> directory in the source tree. This directory contains patches
against different programs that should be compatible with ocaml-gettext. Please have a look to the
<filename>README.patches</filename> of this directory, to know how to handle patching these programs.
</para>
</note>
</section>
<section>
<title>Debian distribution</title>
<para>
<!-- Debian package -->
A debian package is available in the official Debian archive ( release "unstable",
"main" section ).
</para>
<para>
To install it, use the command :
<command>apt-get
<option>install</option> <varname>libgettext-ocaml-dev</varname>
</command>.
</para>
</section>
<section>
<title>Other distributions</title>
<para>
There is no plan to release packages for other distributions. If you have any skill
concerning the packaging of ocaml-gettext for any other distribution, please contact me.
</para>
</section>
</chapter>
<chapter>
<title>Programming with ocaml-gettext</title>
<section>
<title>Overview</title>
<para>
The API of ocaml-gettext is really reduced. It is made on purpose. The design is heavily
based on modules and functors. There is no real reason for this design, it was just
really useful at the time the code was written.
</para>
<para>
The library supposes that all the <varname>textdomain</varname> that will be used during
translation, are declared before using it. It is a real constraint, but it enables more
optimisation. Moreover, it allows having a more "functional" use.
</para>
<para>
<!-- Description of the parameter t -->
First of all, a parameter <varname>t</varname> should be defined. This parameter holds
all the required value to initialise ocaml-gettext. In particular, it contains information
about :
<itemizedlist>
<listitem>
<para>
which textdomain will be used,
</para>
</listitem>
<listitem>
<para>
which language will be used,
</para>
</listitem>
<listitem>
<para>
how the error should be handle,
</para>
</listitem>
<listitem>
<para>
which directory to search.
</para>
</listitem>
</itemizedlist>
This parameter is build and updated through internal functions. You don't have direct access
to it.
</para>
<para>
<!-- Description of the parameter t' -->
The parameter <varname>t</varname> is not directly used for translation. It must be converted into
a parameter <varname>t'</varname> which is a real function to access translation. The transformation
from <varname>t</varname> to <varname>t'</varname> is handled through a function
<varname>realize</varname>. The parameter <varname>t'</varname> is used in low level translation.
</para>
<para>
<!-- Description of the function realize -->
All the work of the library is done in the function <varname>realize</varname>. This function is
not provided in the base package. It is build out of real implementation of ocaml-gettext ( such as
<varname>gettext-camomile</varname> or <varname>gettext-stub</varname> ). This function could handle
all the parameters in different ways. Concerning <varname>gettext-camomile</varname>, it builds a
translation table for all the files found which correspond to a declared <varname>textdomain</varname>.
</para>
<para>
<!-- Justification of the global variable -->
Since it should be very difficult to pass a parameter <varname>t</varname> or <varname>t'</varname> in
all functions that should use translation, we provide a more simple way to use the library. The
top level functions use a global reference to store the parameters <varname>t</varname> and
<varname>t'</varname>. This helps to integrate ocaml-gettext more easily into existing application.
</para>
</section>
<section>
<title>Makefile and source layout</title>
<!-- PO -->
<para>
The source layout should conform to the one described in
<link linkend="gettext-documentation">gettext documentation</link>. In particular, it should
contain a <filename>po</filename> directory. There should be in this directory :
<itemizedlist>
<listitem>
<para>
a file <filename>LINGUAS</filename> describing available translations,
</para>
</listitem>
<listitem>
<para>
a file <filename>POTFILES</filename> containing the listing of all files that
contain translatable strings,
</para>
</listitem>
<listitem>
<para>
a <filename>Makefile</filename> to build the whole thing,
</para>
</listitem>
<listitem>
<para>
a set of PO file which contains translated strings for different languages.
</para>
</listitem>
</itemizedlist>
During the build, the <filename>Makefile</filename> should generate a file
<filename>your-domain.pot</filename> that contains a template PO file that can be used
by translator.
</para>
<example>
<title><filename>Makefile</filename> for <filename>po</filename></title>
<programlisting>&po-makefile;</programlisting>
</example>
<example>
<title><filename>LINGUAS</filename></title>
<programlisting>&LINGUAS;</programlisting>
</example>
<example>
<title><filename>POTFILES</filename></title>
<programlisting>&POTFILES;</programlisting>
</example>
<!-- Source -->
<para>
To build programs and libraries with ocaml-gettext, the preferred way is to use ocamlfind.
There are five findlib packages:
<itemizedlist>
<listitem>
<para>
gettext.base: the base package of ocaml-gettext. It contains the top level type
required to compile any library,
</para>
</listitem>
</itemizedlist>
<itemizedlist>
<listitem>
<para>
gettext.extension
<footnote id="advanced-features">
<para>
This feature is described here for your information only. Since it belongs to
low level implementation of ocaml-gettext, it should not be used.
</para>
</footnote>
: a package used to extend ocaml-gettext. It is reserved for very
particular functions ( such as creating a new <varname>realize</varname> function ),
</para>
</listitem>
</itemizedlist>
<itemizedlist>
<listitem>
<para>
gettext.extract<footnoteref linkend="advanced-features"/>: a package that enables
to create special <command>camlp4</command> program ( such as
<command>ocaml-xgettext</command> ),
</para>
</listitem>
</itemizedlist>
<itemizedlist>
<listitem>
<para>
gettext-stub: an implementation of ocaml-gettext using gettext,
</para>
</listitem>
</itemizedlist>
<itemizedlist>
<listitem>
<para>
gettext-camomile: an implementation of ocaml-gettext using camomile. It is a pure
ocaml implementation.
</para>
</listitem>
</itemizedlist>
</para>
<para>
In order to link an application or a library using ocaml-gettext, you should link with one of : gettext.base,
gettext-camomile or gettext-stub.
</para>
<example>
<title><filename>Makefile</filename></title>
<programlisting>&makefile;</programlisting>
</example>
</section>
<section>
<title>Library</title>
<para>
Library should use the module <varname>Gettext.Library</varname>. It doesn't need any real implementation
of ocaml-gettext. By this way, you can let the library user choose the most appropriate ocaml-gettext
implementation. This point is essential : a library could be used as well in a GUI program or in short run
command line program. These two examples don't require the same kind of implementation at all: GUI program
loads most of their translated strings, command line program only use one among them. So by using the module
<varname>Gettext.Library</varname> framework, you don't restrict programs to use one particular implementation
of ocaml-gettext.
</para>
<para>
The library should define, using the functor <varname>Init</varname> provided:
<itemizedlist>
<listitem>
<para>
his textdomain through the <varname>textdomain</varname> value,
</para>
</listitem>
<listitem>
<para>
his dependencies through the <varname>dependencies</varname> value,
</para>
</listitem>
<listitem>
<para>
if needed his charset and directory binding ( but it is not recommended to do so ).
</para>
</listitem>
</itemizedlist>
</para>
<para>
After having instantiated the module <varname>Gettext.Library</varname> with the appropriate <varname>Init</varname>,
you should use the function provided :
<itemizedlist>
<listitem>
<para>
<varname>s_</varname> : for translating singular strings,
</para>
</listitem>
<listitem>
<para>
<varname>f_</varname> : for translating singular strings which will be used with <varname>Printf</varname>
function
<footnote id="printf-strings">
<para>
Strings which should be used by <varname>Printf</varname> function are checked to be sure
that the returned strings are equivalent to the provided English string. In particular, every
"%"-symbol should be the same in the provided string and the returned string. If not,
it is the untranslated string which is returned.
</para>
</footnote>,
</para>
</listitem>
<listitem>
<para>
<varname>sn_</varname> : for translating plural strings,
</para>
</listitem>
<listitem>
<para>
<varname>fn_</varname> : for translating plural strings which will be used with <varname>Printf</varname>
function<footnoteref linkend="printf-strings"/>,
</para>
</listitem>
</itemizedlist>
</para>
<warning>
<para>
You must keep the function name <varname>s_</varname>, <varname>f_</varname>, <varname>sn_</varname> and <varname>fn_</varname>.
The extraction of translatable strings matches these names. If you don't keep it, the extraction of translatable strings
will fail.
</para>
</warning>
<example>
<title><filename>library.ml</filename></title>
<programlisting>&library-ml;</programlisting>
</example>
<example>
<title><filename>libraryGettext.ml</filename></title>
<programlisting>&library-gettext-ml;</programlisting>
</example>
<para>
All the calls to translation functions, use the textdomain provided in <varname>Init</varname>.
</para>
<para>
The only constraint when using ocaml-gettext in your library is to provide an access to the value
<varname>Gettext.Library.init</varname>. This value is used as dependencies for other libraries and
programs that depend on it. For example, since you use the library ocaml-gettext, your primary dependency
is the function <varname>init</varname> provided in the top level ( the function <varname>Gettext.string_of_exception</varname>
is localised ).
</para>
<warning>
<para>
If you distribute your library, don't forget to mention that ocaml-gettext will only be able to translate
the string defined in your library, if and only if the MO file build with is also installed. If not installed
ocaml-gettext is useless.
</para>
</warning>
</section>
<section>
<title>Program</title>
<para>
Program should use ocaml-gettext just as libraries do. The only difference lies in the fact that you should provide
a <varname>realize</varname> function in the <varname>InitProgram</varname>. The other difference is that the
<varname>init</varname> value is not a dependency that should be used by other program. It is a <varname>Arg</varname>
usable value. It allows user to define some important parameters.
</para>
<example>
<title><filename>program.ml</filename></title>
<programlisting>&program-ml;</programlisting>
</example>
<example>
<title><filename>programGettext.ml</filename></title>
<programlisting>&program-gettext-ml;</programlisting>
</example>
<example>
<title>Output of <command>program <option>--help</option></command></title>
<screen>
<prompt>$></prompt><command>./program <option>--help</option></command>
<computeroutput>
--my-name name Your name. Default : ""
--gettext-failsafe {ignore|inform-stderr|raise-exception}
Choose how to handle failure in ocaml-gettext. Default: ignore.
--gettext-disable Disable the translation perform by ocaml-gettext. Default: enable.
--gettext-domain-dir textdomain dir Set a dir to search ocaml-gettext files for the specified domain. Default: [ ].
--gettext-dir dir Add a search dir for ocaml-gettext files. Default: [ "/usr/share/locale";
"/usr/local/share/locale" ].
--gettext-language language Set the default language for ocaml-gettext. Default: (none).
--gettext-codeset codeset Set the default codeset for outputting string with ocaml-gettext. Default: ISO-8859-1.
-help Display this list of options
--help Display this list of options
</computeroutput>
</screen>
</example>
<para>
If you want to include a manpage ( or info file ), that describes the command line option of ocaml-gettext, you should use the Docbook
XML fragment distributed with this application. Docbook should be enough generic to allow you to link it into your documentation.
If you don't want to link it with your documentation, you can refer to <link linkend="ocaml-gettext-options">
ocaml-gettext-options manpage</link>.
</para>
<example>
<title><filename>program.xml</filename> : Docbook manpage</title>
<programlisting>&program-xml;</programlisting>
</example>
<example>
<title><filename>Makefile</filename> : Makefile for docbook manpage</title>
<programlisting>&makefile-doc;</programlisting>
</example>
<para>
You should take care of what implementation of ocaml-gettext you are using. In order to choose the right implementation
you should consider your program and every characteristic of it ( how many strings does it need to fetch ? Does it use already
a C library that link with gettext ? ).
<table>
<title>Characteristics of ocaml-gettext implementation.</title>
<tgroup cols="3">
<thead>
<row>
<entry>Implementation</entry>
<entry>Characteristics</entry>
<entry>Use</entry>
</row>
</thead>
<tbody>
<row>
<entry>GettextCamomile.Map</entry>
<entry>
<itemizedlist>
<listitem>
<para>
Pure OCaml implementation,
</para>
</listitem>
<listitem>
<para>
Full load of all MO files before any translation,
</para>
</listitem>
<listitem>
<para>
Use OCaml standard <varname>Map</varname>.
</para>
</listitem>
</itemizedlist>
</entry>
<entry>
<itemizedlist>
<listitem>
<para>
Pure OCaml program,
</para>
</listitem>
<listitem>
<para>
Program that requires to translate a lot of strings,
</para>
</listitem>
<listitem>
<para>
Threaded program ( since it use OCaml Map, it should be thread safe without
problem ).
</para>
</listitem>
</itemizedlist>
</entry>
</row>
<row>
<entry>GettextCamomile.Hashtbl</entry>
<entry>
<itemizedlist>
<listitem>
<para>
Pure OCaml implementation,
</para>
</listitem>
<listitem>
<para>
Full load of all MO file before any translation,
</para>
</listitem>
<listitem>
<para>
Use OCaml standard <varname>Hashtbl</varname>.
</para>
</listitem>
</itemizedlist>
</entry>
<entry>
<itemizedlist>
<listitem>
<para>
Pure OCaml program,
</para>
</listitem>
<listitem>
<para>
Program that requires to translate a lot of strings,
</para>
</listitem>
<listitem>
<para>
Should work with threaded program, provided that the <varname>Hashtbl</varname>
works in threaded environment.
</para>
</listitem>
</itemizedlist>
</entry>
</row>
<row>
<entry>GettextCamomile.Open</entry>
<entry>
<itemizedlist>
<listitem>
<para>
Pure OCaml implementation,
</para>
</listitem>
<listitem>
<para>
Load strings from MO if needed,
</para>
</listitem>
<listitem>
<para>
Use OCaml standard <varname>Hashtbl</varname>,
</para>
</listitem>
<listitem>
<para>
Use a dichotomic search for the strings,
</para>
</listitem>
<listitem>
<para>
Compute MO file to open at initialisation,
</para>
</listitem>
<listitem>
<para>
Open a file when fetching string,
</para>
</listitem>
<listitem>
<para>
Doesn't memorise already translated strings,
</para>
</listitem>
<listitem>
<para>
Implementation design copied from gettext.
</para>
</listitem>
</itemizedlist>
</entry>
<entry>
<itemizedlist>
<listitem>
<para>
Pure OCaml program,
</para>
</listitem>
<listitem>
<para>
Program that require to translate very few strings,
</para>
</listitem>
<listitem>
<para>
Should work with threaded program, provided that <varname>open_in</varname> function call
work.
</para>
</listitem>
</itemizedlist>
</entry>
</row>
<row>
<entry>GettextStub.Native</entry>
<entry>
<itemizedlist>
<listitem>
<para>
Native gettext library,
</para>
</listitem>
<listitem>
<para>
Partial load of all MO file before any translation, use <varname>mmap</varname>.
</para>
</listitem>
</itemizedlist>
</entry>
<entry>
<itemizedlist>
<listitem>
<para>
OCaml program that uses library compiled with gettext,
</para>
</listitem>
<listitem>
<para>
Should work with threaded program, provided that the <varname>gettext</varname>
work in threaded environment.
</para>
</listitem>
</itemizedlist>
</entry>
</row>
<row>
<entry>GettextStub.Preload</entry>
<entry>
<itemizedlist>
<listitem>
<para>
Native gettext library,
</para>
</listitem>
<listitem>
<para>
Forced load of all MO file before any translation, the preload is realized by trying to load
the string "" for all the textdomain defined.
</para>
</listitem>
</itemizedlist>
</entry>
<entry>
<itemizedlist>
<listitem>
<para>
OCaml program that uses library compiled with gettext,
</para>
</listitem>
<listitem>
<para>
Program that needs to translate a lot of strings,
</para>
</listitem>
<listitem>
<para>
Should work with threaded program, provided that the <varname>gettext</varname>
work in threaded environment.
</para>
</listitem>
</itemizedlist>
</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</section>
<section>
<title>Graphical user interface</title>
<para>
Graphical user interface works just as a program or a library. The only difference is that the file which contains
the graphical user interface should not be written in OCaml. You have two cases :
<itemizedlist>
<listitem>
<para>
You use glade file, so you should extract the strings from this file using <command>xgettext</command>
</para>
</listitem>
<listitem>
<para>
You use a hand written interface written in OCaml, the extraction of the strings follow the same way
as a library.
</para>
</listitem>
</itemizedlist>
</para>
<para>
You should use the first alternative : it is easier for a translator to extract strings, without having to compile
your application ( it enables translators that don't know OCaml to help you ). In order to do so, you should use the native
<command>xgettext</command> binary ( provided with gettext ). It should support the format of the translatable strings found
in your GUI file ( for example, <command>xgettext</command> supports glade file ).
</para>
<para>
But for now you can only use the second alternative, because OCaml is not yet supported in <command>xgettext</command>. This
should be fixed, once <command>ocaml-gettext</command> will be enough stable to become a back-end for <command>xgettext</command>
<footnote>
<para>
For now, extracting strings from OCaml source file and glade file, requires to patch <productname>gettext</productname>.
You can find this path in patches. This patch will be sent to upstream author once it will be considered enough stable.
</para>
</footnote>.
</para>
<para>
In the two cases, you just have to add your GUI file ( in OCaml or native form ) to <filename>POTFILES</filename>.
</para>
<para>
Graphical user interfaces are good candidates for settings a fixed <varname>Init.codeset</varname>. Typically, GTK2 interfaces require
to have these parameters set to UTF-8.
</para>
<example>
<title><filename>gui.ml</filename></title>
<programlisting>&gui-ml;</programlisting>
</example>
<example>
<title><command>./program --gettext-dir ../build/share/locale --gettext-lang C --my-name Sylvain</command></title>
<screenshot>
<graphic fileref="gui.png" format="PNG"/>
</screenshot>
</example>
<example>
<title><command>./program --gettext-dir ../build/share/locale --gettext-lang fr --my-name Sylvain</command></title>
<screenshot>
<graphic fileref="gui-fr.png" format="PNG"/>
</screenshot>
</example>
<warning>
<para>
If you build <productname>lablgtk</productname> application, you must keep in mind that some locale settings are made
in the function <varname>GMain.Init</varname>. Those settings could override those done through the command line
options. In order to correctly use ocaml-gettext, you must be sure to call <varname>GMain.Init</varname> before
using <varname>Arg.parse</varname> with the ocaml-gettext args.
</para>
</warning>
</section>
</chapter>
<chapter>
<title>Translating ocaml-gettext programs and libraries</title>
<para>
ocaml-gettext has been built around gettext. This allows translators to use exactly the same technics as they should
use with gettext. All the documentation required for translating can be found in <link linkend="gettext-documentation">
gettext documentation</link>.
</para>
<para>
It is recommended to use GUI which allows easier translation such as :
<itemizedlist>
<listitem>
<para>
<ulink url="&site-gtranslator;">gtranslator</ulink>,
</para>
</listitem>
<listitem>
<para>
<ulink url="&site-kbabel;">kbabel</ulink>.
</para>
</listitem>
</itemizedlist>
</para>
</chapter>
<chapter>
<title>Using ocaml-gettext programs</title>
<para>
ocaml-gettext program can be used just as any OCaml program. The only difference with standard OCaml program is that
they come with a bunch of command line options which are specific to OCaml program. In most cases, you just have to define
a well suited <varname>LC_ALL</varname> or <varname>LANG</varname> environment variable. Since ocaml-gettext is compatible
with gettext, if your environment variable works with gettext, it should also works with ocaml-gettext.
</para>
<para>
You can find more details in the <link linkend="gettext-documentation">gettext documentation</link> or in the <link
linkend="ocaml-gettext-options">ocaml-gettext-options manpage</link>.
</para>
</chapter>
<appendix>
<title>Tips and tricks</title>
<section>
<title>Adding gettext support without depending on ocaml-gettext</title>
<para>
You want to 'gettextify' your program, adding 's_' and 'f_'
annotations throughout. However now your program has an additional
dependency, ocaml-gettext. You can make ocaml-gettext optional by
creating a set of dummy functions which do nothing:
</para>
<example>
<title><filename>prog_gettext.ml</filename></title>
<programlisting>
(* This file is generated automatically by ./configure. *)
module Gettext =
struct
external s_ : string -> string = "%identity"
external f_ : ('a -> 'b, 'c, 'd) format -> ('a -> 'b, 'c, 'd) format = "%identity"
let sn_ : string -> string -> int -> string =
fun s p n ->
if n = 1 then s else p
let fn_ : ('a -> 'b, 'c, 'd) format -> ('a -> 'b, 'c, 'd) format -> int -> ('a -> 'b, 'c, 'd) format =
fun s p n ->
if n = 1 then s else p
end
</programlisting>
</example>
<para>
You have to arrange for your configure script to place the above
content or the real ProgramGettext module into 'prog_gettext.ml',
depending on whether it detects that ocaml-gettext is installed
(eg. using 'ocamlfind query gettext' or some other method).
</para>
</section>
</appendix>
<appendix>
<title>Links</title>
<itemizedlist>
<listitem>
<para>
<ulink url="&site-ocaml;">OCaml website</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="&site-gettext;">Gettext website</ulink>
</para>
</listitem>
<listitem id="gettext-documentation">
<para>
<ulink url="&site-documentation-gettext;">Gettext documentation</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="&site-camomile;">Camomile website</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="&site-ocaml-fileutils;">ocaml-fileutils website</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="&site-ounit;">OUnit website</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="&site-ocaml-benchmark;">ocaml-benchmark website</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="&site-docbook;">Docbook website</ulink>
</para>
</listitem>
</itemizedlist>
</appendix>
<appendix>
<title>Manpages</title>
<refentry>
&ocaml-gettext-manual;
</refentry>
<refentry>
&ocaml-xgettext-manual;
</refentry>
<refentry id="ocaml-gettext-options">
&ocaml-gettext-options-manual;
</refentry>
</appendix>
</book>
|