• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

lib/Data/H03-May-2022-2,2601,124

t/H03-May-2022-847699

tools/benchmark/H03-May-2022-135118

xt/H03-May-2022-7162

Build.PLH A D04-Oct-2019301 134

ChangesH A D04-Oct-20195.7 KiB174138

LICENSEH A D04-Oct-201936 11

MANIFESTH A D04-Oct-20191.1 KiB5050

META.jsonH A D04-Oct-20194.2 KiB147146

META.ymlH A D04-Oct-20192.6 KiB9392

MakefileH A D04-Oct-201929.4 KiB929611

README.mdH A D04-Oct-20199.1 KiB300195

cpanfileH A D04-Oct-2019478 1917

minil.tomlH A D04-Oct-201915 11

README.md

1# NAME
2
3Data::Localize - Alternate Data Localization API
4
5# SYNOPSIS
6
7    use Data::Localize;
8
9    my $loc = Data::Localize->new();
10    $loc->add_localizer(
11        class      => "Namespace", # Locale::Maketext-style .pm files
12        namespaces => [ "MyApp::I18N" ]
13    );
14
15    $loc->add_localizer(
16        class => "Gettext",
17        path  => "/path/to/localization/data/*.po"
18    );
19
20    $loc->set_languages();
21    # or explicitly set one
22    # $loc->set_languages('en', 'ja' );
23
24    # looks under $self->languages, and checks if there are any
25    # localizers that can handle the job
26    $loc->localize( 'Hellow, [_1]!', 'John Doe' );
27
28    # You can enable "auto", which will be your last resort fallback.
29    # The key you give to the localize method will be used as the lexicon
30    $self->auto(1);
31
32# DESCRIPTION
33
34Data::Localize is an object oriented approach to localization, aimed to
35be an alternate choice for Locale::Maketext, Locale::Maketext::Lexicon, and
36Locale::Maketext::Simple.
37
38# RATIONALE
39
40Functionality-wise, Locale::Maketext does what it advertises to do.
41Here's a few reasons why you might or might not choose Data::Localize
42over Locale::Maketext-based localizers:
43
44## Object-Oriented
45
46Data::Localize is completely object-oriented. YMMV.
47
48## Faster
49
50On some my benchmarks, Data::Localize is faster than Locale::Maketext
51by 50~80%. (But see PERFORMANCE)
52
53## Scalable For Large Amount Of Lexicons
54
55Whereas Locale::Maketext generally stores the lexicons in memory,
56Data::Localize allows you to store this data in alternate storage.
57By default Data::Localize comes with a BerkeleyDB backend.
58
59# BASIC WORKING
60
61## STRUCTURE
62
63Data::Localize is a wrapper around various Data::Localize::Localizer
64implementers (localizers). So if you don't specify any localizers,
65Data::Localize will do... nothing (unless you specify `auto`).
66
67Localizers are the objects that do the actual localization. Localizers must
68register themselves to the Data::Localize parent, noting which languages it
69can handle (which usually is determined by the presence of data files like
70en.po, ja.po, etc). A special language ID of '\*' is used to accept fallback
71cases. Localizers registered to handle '\*' will be tried _after_ all other
72language possibilities have been exhausted.
73
74If the particular localizer cannot deal with the requested string, then
75it simply returns nothing.
76
77## AUTO-GENERATING LEXICONS
78
79Locale::Maketext allows you to supply an "\_AUTO" key in the lexicon hash,
80which allows you to pass a non-existing key to the localize() method, and
81use it as the actual lexicon, if no other applicable lexicons exists.
82
83Locale::Maketext attaches this to the lexicon hash itself, but Data::Localizer
84differs in that it attaches to the Data::Localizer object itself, so you
85don't have to place \_AUTO everywhere.
86
87    # here, we're deliberately not setting any localizers
88    my $loc = Data::Localize->new(auto => 1);
89
90    # previous auto => 1 will force Data::Localize to fallback to
91    # using the key ('Hello, [_1]') as the localization token.
92    print $loc->localize('Hello, [_1]', 'John Doe'), "\n";
93
94# UTF8
95
96All data is expected to be in decoded utf8. You must "use utf8" or
97decode them to Perl's internal representation for all values
98passed to Data::Localizer. We won't try to be smart for you. USE UTF8!
99
100- Using Explicit decode()
101
102        use Encode q(decode decode_utf8);
103        use Data::Localizer;
104
105        my $loc = Data::Localize->new(...);
106
107        $loc->localize( $key, decode( 'iso-2022-jp', $value ) );
108
109        # if $value is encoded utf8...
110        # $loc->localize( $key, decode_utf8( $value ) );
111
112- Using utf8
113
114    "use utf8" is simpler, but do note that it will affect ALL your literal strings
115    in the current scope
116
117        use utf8;
118
119        $loc->localize( $key, "some-utf8-key-here" );
120
121# USING ALTERNATE STORAGE
122
123By default all lexicons are stored on memory, but if you're building an app
124with thousands and thousands of long messages, this might not be the ideal
125solution. In such cases, you can change where the lexicons get stored
126
127    my $loc = Data::Localize->new();
128    $loc->add_localizer(
129        class         => 'Gettext',
130        path          => '/path/to/data/*.po'
131        storage_class => 'BerkeleyDB',
132        storage_args  => {
133            dir => '/path/to/really/fast/device'
134        }
135    );
136
137This would cause Data::Localize to put all the lexicon data in several BerkeleyDB files under /path/to/really/fast/device
138
139Note that this approach would buy you no gain if you use Data::Localize::Namespace, as that approach by default expects everything to be in memory.
140
141# DEBUGGING
142
143## DEBUG
144
145To enable debug tracing, either set DATA\_LOCALIZE\_DEBUG environment variable,
146
147    DATA_LOCALIZE_DEBUG=1 ./yourscript.pl
148
149or explicitly define a function before loading Data::Localize:
150
151    BEGIN {
152        *Data::Localize::DEBUG = sub () { 1 };
153    }
154    use Data::Localize;
155
156# METHODS
157
158## add\_localizer
159
160Adds a new localizer. You may either pass a localizer object, or arguments
161to your localizer's constructor:
162
163    $loc->add_localizer( YourLocalizer->new );
164
165    $loc->add_localizer(
166        class => "Namespace",
167        namespaces => [ 'Blah' ]
168    );
169
170## localize
171
172Localize the given string ID, using provided variables.
173
174    $localized_string = $loc->localize( $id, @args );
175
176## detect\_languages
177
178Detects the current set of languages to use. If used in an CGI environment,
179will attempt to detect the language of choice from headers. See
180I18N::LanguageTags::Detect for details.
181
182## detect\_languages\_from\_header
183
184Detects the language from the given header value, or from HTTP\_ACCEPT\_LANGUAGES environment variable
185
186## localizers
187
188Return a arrayref of localizers
189
190## add\_localizer\_map
191
192Used internally.
193
194## set\_localizer\_map
195
196Used internally.
197
198## find\_localizers
199
200Finds a localizer by its attribute. Currently only supports isa
201
202    my @locs = $loc->find_localizers(isa => 'Data::Localize::Gettext');
203
204## set\_languages
205
206If used without any arguments, calls detect\_languages() and sets the
207current language set to the result of detect\_languages().
208
209## languages
210
211Gets the current list of languages
212
213## add\_fallback\_languages
214
215## fallback\_languages
216
217## count\_localizers()
218
219Return the number of localizers available
220
221## get\_localizer\_from\_lang($lang)
222
223Get appropriate localizer for language $lang
224
225## grep\_localizers(\\&sub)
226
227Filter localizers
228
229# PERFORMANCE
230
231tl;dr: Use one that fits your needs
232
233## Using explicit get\_handle for every request
234
235This benchmark assumes that you're fetching the lexicon anew for
236every request. This allows you to switch languages for every request
237
238Benchmark run with Mac OS X (10.8.2) perl 5.16.1
239
240    Running benchmarks with
241      Locale::Maketext: 1.23
242      Data::Localize:   0.00023
243                         Rate D::L(Namespace)   L::M D::L(Gettext) D::L(Gettext+BDB)
244    D::L(Namespace)    5051/s              --   -65%          -73%              -73%
245    L::M              14423/s            186%     --          -24%              -24%
246    D::L(Gettext)     18868/s            274%    31%            --               -1%
247    D::L(Gettext+BDB) 18987/s            276%    32%            1%                --
248
249## Using cached lexicon objects for all
250
251This benchmark assumes that you're fetching the lexicon once for
252a particular language, and you keep it in memory for reuse.
253This does NOT allow you to switch languages for every request.
254
255Benchmark run with Mac OS X (10.8.2) perl 5.16.1
256
257    Running benchmarks with
258      Locale::Maketext: 1.23
259      Data::Localize:   0.00023
260                          Rate D::L(Namespace) D::L(Gettext+BDB) D::L(Gettext)  L::M
261    D::L(Namespace)     6023/s              --              -65%          -69%  -96%
262    D::L(Gettext+BDB)  17202/s            186%                --          -12%  -87%
263    D::L(Gettext)      19548/s            225%               14%            --  -86%
264    L::M              135993/s           2158%              691%          596%    --
265
266# TODO
267
268Gettext style localization files -- Make it possible to decode them
269
270# CONTRIBUTORS
271
272Dave Rolsky
273
274# AUTHOR
275
276Daisuke Maki `<daisuke@endeworks.jp>`
277
278# COPYRIGHT
279
280- The "MIT" License
281
282    Permission is hereby granted, free of charge, to any person obtaining a
283    copy of this software and associated documentation files (the
284    "Software"), to deal in the Software without restriction, including
285    without limitation the rights to use, copy, modify, merge, publish,
286    distribute, sublicense, and/or sell copies of the Software, and to
287    permit persons to whom the Software is furnished to do so, subject to
288    the following conditions:
289
290    The above copyright notice and this permission notice shall be included
291    in all copies or substantial portions of the Software.
292
293    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
294    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
295    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
296    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
297    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
298    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
299    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
300