| 1 |
Locale::gettext |
| 2 |
version 1.07 |
| 3 |
|
| 4 |
This is a perl5 module quickly written to gain access to |
| 5 |
the C library functions for internatialization. They |
| 6 |
work just like the C versions. |
| 7 |
|
| 8 |
As of version 1.04, an object oriented interface more |
| 9 |
suitable to native Perl programs is available. |
| 10 |
|
| 11 |
Locale::gettext is Copyright 1996..2005 by Kim Vandry |
| 12 |
<[email protected]>. All rights reserved. |
| 13 |
|
| 14 |
This library is free software; you may distribute under the terms |
| 15 |
of either the GNU General Public License or the Artistic License, as |
| 16 |
specified in the Perl README file. |
| 17 |
|
| 18 |
Changes |
| 19 |
------- |
| 20 |
|
| 21 |
1.07 Fix test failures caused by $LANGUAGE being set |
| 22 |
|
| 23 |
1.06 Bugfix: #104667 Makefile.PL libaries need to be listed after .o files |
| 24 |
Bugfix: #104668 ensure availability of locale API, correct typo in documentation |
| 25 |
Add META.yml (Fixes #91921) |
| 26 |
|
| 27 |
1.05 Bugfix: [cpan #13042] useless #ifdef should be #if |
| 28 |
Bugfix: [cpan #13044] make test fails when using POSIX locale |
| 29 |
|
| 30 |
1.04 Add several functions provided by the GNU gettext library |
| 31 |
Create object oriented interface |
| 32 |
|
| 33 |
1.03 Fix error in README file |
| 34 |
|
| 35 |
1.02 Include a License |
| 36 |
|
| 37 |
1.01 Changed from "gettext" to "Locale::gettext" (i.e. |
| 38 |
moved under Locale::) on the advice of several |
| 39 |
people |
| 40 |
|
| 41 |
Small "lint" fix from [email protected] |
| 42 |
|
| 43 |
1.00 Initial version |
| 44 |
|
| 45 |
TODO |
| 46 |
---- |
| 47 |
|
| 48 |
A TIEHASH interface |
| 49 |
|
| 50 |
Here's a quick tutorial. |
| 51 |
----------------------- |
| 52 |
|
| 53 |
Note that your vendor's implementation of these functions |
| 54 |
may be a bit different, but I think that in general these |
| 55 |
are quite standard POSIX functions. |
| 56 |
|
| 57 |
Kim Vandry <[email protected]> |
| 58 |
Mlink Internet <http://www.Mlink.NET> |
| 59 |
July 1996 |
| 60 |
|
| 61 |
INTERNATIONALIZING YOUR PROGRAM |
| 62 |
|
| 63 |
Step 1 |
| 64 |
------ |
| 65 |
|
| 66 |
If you've already written your code, you need to |
| 67 |
wrap the gettext() function around all of the |
| 68 |
text strings. Needless to say, this is much easier if |
| 69 |
you do it while you write. |
| 70 |
|
| 71 |
# create object for oo interface |
| 72 |
my $d = Locale::gettext->domain("my_program"); |
| 73 |
|
| 74 |
print "Welcome to my program\n"; |
| 75 |
|
| 76 |
# oo |
| 77 |
print $d->get("Welcome to my program"), "\n"; |
| 78 |
|
| 79 |
# traditional |
| 80 |
print gettext("Welcome to my program"), "\n"; |
| 81 |
|
| 82 |
Note that you probably don't want to include that newline |
| 83 |
in the gettext() call, nor any formatting codes such as |
| 84 |
HTML tags. The argument to gettext() is the text string |
| 85 |
in the default language or locale. This is known as the |
| 86 |
C locale and should probably be usually English. |
| 87 |
|
| 88 |
Step 2 |
| 89 |
------ |
| 90 |
|
| 91 |
Do the apropriate initializations at the beginning of your |
| 92 |
program: |
| 93 |
|
| 94 |
# |
| 95 |
use POSIX; # for setlocale() |
| 96 |
use Locale::gettext; |
| 97 |
# |
| 98 |
# The following statement initializes the locale handling |
| 99 |
# code in the C library. Normally, it causes it to read |
| 100 |
# in the environment variables that determine the current |
| 101 |
# locale. |
| 102 |
# |
| 103 |
# The first parameter is the category you would |
| 104 |
# like to initialize locale information for. You can use |
| 105 |
# LC_ALL for this, which will set locale information for |
| 106 |
# all categories, including LC_CTYPE, LC_TIME, LC_NUMERIC, |
| 107 |
# etc.. |
| 108 |
# |
| 109 |
# I recommend that you set only LC_MESSAGES (text strings) |
| 110 |
# or LC_CTYPE (character sets) and LC_TIME (time |
| 111 |
# conventions) too at most. You may find that if you set |
| 112 |
# LC_NUMERIC or some other categories, you will start |
| 113 |
# outputting decimal numbers with strange thousand separators |
| 114 |
# and decimal points and they will be unparseable in |
| 115 |
# other countries. |
| 116 |
# |
| 117 |
# The second parameter is the locale name. If it is an |
| 118 |
# empty string, then this information will be fetched from |
| 119 |
# environment variables. |
| 120 |
# |
| 121 |
# Note that setlocale() will cause every part of your |
| 122 |
# program to start operating in the new, non default (C) |
| 123 |
# locale, including C library functions. So don't be |
| 124 |
# surprised if POSIX::ctime returns "Montag, 22. Juli 1996, |
| 125 |
# 12:08:25 Uhr EDT" instead of "Mon Jul 22 12:08:25 EDT 1996" |
| 126 |
# If you set LC_TIME or LC_ALL using setlocale(). |
| 127 |
# |
| 128 |
setlocale(LC_MESSAGES, ""); |
| 129 |
# |
| 130 |
# Decide on a unique identifier that will distinguish your |
| 131 |
# program's text strings in the LC_MESSAGES database. This |
| 132 |
# would usually be the name of your program |
| 133 |
# |
| 134 |
# By default, locale information is found in OS dependant |
| 135 |
# system directories such as /usr/lib/locale, or any directory |
| 136 |
# found in the $PATH-like environment variable $NLSPATH. |
| 137 |
# I recommend that you do _not_ install files in /usr. If |
| 138 |
# your program is installed in a directory tree such as |
| 139 |
# /opt/my_package_name/{bin,man,lib,etc}, then you could |
| 140 |
# use /opt/my_package_name/locale to store locale information |
| 141 |
# specific to your program, or you could put in somewhere |
| 142 |
# in /usr/local/lib. |
| 143 |
# |
| 144 |
# Wherever you put it, if it is not one of the default |
| 145 |
# directories, you will need to call bindtextdomain() to |
| 146 |
# tell the library where to find your files. The first |
| 147 |
# parameter is your database's identifier that you chose |
| 148 |
# above. |
| 149 |
# |
| 150 |
# oo interface: |
| 151 |
|
| 152 |
my $d = Locale::gettext->domain("my_domain"); |
| 153 |
$d->dir("/opt/my_package_name/locale"); |
| 154 |
|
| 155 |
# traditional interface: |
| 156 |
bindtextdomain("my_domain", "/opt/my_package_name/locale"); |
| 157 |
textdomain("my_domain"); |
| 158 |
|
| 159 |
# That's it for the initializations |
| 160 |
|
| 161 |
Step 3 |
| 162 |
------ |
| 163 |
|
| 164 |
Test to see if your program still works after all these mods :-) |
| 165 |
|
| 166 |
Step 4 |
| 167 |
------ |
| 168 |
|
| 169 |
TRANSLATE! |
| 170 |
|
| 171 |
Read msgfmt(1) for details on this. Basically, for each locale |
| 172 |
other than the default, you need to create a file like this: |
| 173 |
(Note: I do not speak German, I'm making an attempt here :-) |
| 174 |
Call this file with the .po extension. |
| 175 |
|
| 176 |
--BEGIN |
| 177 |
domain "my_domain" |
| 178 |
|
| 179 |
msgid "Welcome to my program" |
| 180 |
msgstr "Willkommen in mein Program" |
| 181 |
|
| 182 |
msgid "Help" |
| 183 |
msgstr "Hilfe" |
| 184 |
--END |
| 185 |
|
| 186 |
The "msgid" parameter must match exactly the argument to the |
| 187 |
gettext() function, and "msgstr" is the corresponding translation. |
| 188 |
|
| 189 |
You can use the xgettext(1) utility to initially construct this |
| 190 |
file from all of the gettext() calls in your source code. It was |
| 191 |
designed for C but it works OK with perl. |
| 192 |
|
| 193 |
Step 5 |
| 194 |
------ |
| 195 |
|
| 196 |
Compile the .po file |
| 197 |
|
| 198 |
$ msgfmt my_file.po |
| 199 |
|
| 200 |
This will create a file called my_domain.mo (default messages.mo) |
| 201 |
which you should place in the <locale>/LC_MESSAGES/my_domain.mo |
| 202 |
subdirectory of either a system default directory, a directory |
| 203 |
in $NLSPATH, or the directory argument to bindtextdomain(). |
| 204 |
Replace <locale> with the name of the locale for which this file |
| 205 |
is created. |
| 206 |
|
| 207 |
For example: |
| 208 |
|
| 209 |
$ mkdir -p /opt/my_package/locale/de/LC_MESSAGES |
| 210 |
$ mkdir -p /opt/my_package/locale/fr/LC_MESSAGES |
| 211 |
$ cd /path/to/my/source/code |
| 212 |
$ cd de |
| 213 |
$ msgfmt my_domain.po |
| 214 |
$ mv my_domain.mo /opt/my_package/locale/de/LC_MESSAGES |
| 215 |
$ cd ../fr |
| 216 |
$ msgfmt my_domain.po |
| 217 |
$ mv my_domain.mo /opt/my_package/locale/fr/LC_MESSAGES |
| 218 |
|
| 219 |
Step 6 |
| 220 |
------ |
| 221 |
|
| 222 |
Test it out |
| 223 |
|
| 224 |
$ my_program |
| 225 |
Welcome to my program |
| 226 |
$ LANG=fr my_program |
| 227 |
Bienvenue à mon programme |
| 228 |
$ LANG=de my_program |
| 229 |
Willkommen in mein Program |
| 230 |
|
| 231 |
(Or, set only the messages category instead of the whole locale) |
| 232 |
|
| 233 |
$ LC_MESSAGES=fr |
| 234 |
$ export LC_MESSAGES |
| 235 |
$ my_program |
| 236 |
Bienvenue à mon programme |