1Locale::Maketext::FuzzyU(s3e)r Contributed Perl DocumentaLtoicoanle::Maketext::Fuzzy(3)
2
3
4
6 Locale::Maketext::Fuzzy - Maketext from already interpolated strings
7
9 This document describes version 0.10 of Locale::Maketext::Fuzzy,
10 released October 14, 2007.
11
13 package MyApp::L10N;
14 use base 'Locale::Maketext::Fuzzy'; # instead of Locale::Maketext
15
16 package MyApp::L10N::de;
17 use base 'MyApp::L10N';
18 our %Lexicon = (
19 # Exact match should always be preferred if possible
20 "0 camels were released."
21 => "Exact match",
22
23 # Fuzzy match candidate
24 "[quant,_1,camel was,camels were] released."
25 => "[quant,_1,Kamel wurde,Kamele wurden] freigegeben.",
26
27 # This could also match fuzzily, but is less preferred
28 "[_2] released[_1]"
29 => "[_1][_2] ist frei[_1]",
30 );
31
32 package main;
33 my $lh = MyApp::L10N->get_handle('de');
34
35 # All ->maketext calls below will become ->maketext_fuzzy instead
36 $lh->override_maketext(1);
37
38 # This prints "Exact match"
39 print $lh->maketext('0 camels were released.');
40
41 # "1 Kamel wurde freigegeben." -- quant() gets 1
42 print $lh->maketext('1 camel was released.');
43
44 # "2 Kamele wurden freigegeben." -- quant() gets 2
45 print $lh->maketext('2 camels were released.');
46
47 # "3 Kamele wurden freigegeben." -- parameters are ignored
48 print $lh->maketext('3 released.');
49
50 # "4 Kamele wurden freigegeben." -- normal usage
51 print $lh->maketext('[*,_1,camel was,camels were] released.', 4);
52
53 # "!Perl ist frei!" -- matches the broader one
54 # Note that the sequence ([_2] before [_1]) is preserved
55 print $lh->maketext('Perl released!');
56
58 This module is a subclass of "Locale::Maketext", with additional
59 support for localizing messages that already contains interpolated
60 variables.
61
62 This is most useful when the messages are returned by external sources
63 -- for example, to match "dir: command not found" against "[_1]:
64 command not found".
65
66 Of course, this module is also useful if you're simply too lazy to use
67 the
68
69 $lh->maketext("[quant,_1,file,files] deleted.", $count);
70
71 syntax, but wish to write
72
73 $lh->maketext_fuzzy("$count files deleted");
74
75 instead, and have the correct plural form figured out automatically.
76
77 If "maketext_fuzzy" seems too long to type for you, this module also
78 provides a "override_maketext" method to turn all "maketext" calls into
79 "maketext_fuzzy" calls.
80
82 $lh->maketext_fuzzy(key[, parameters...]);
83 That method takes exactly the same arguments as the "maketext" method
84 of "Locale::Maketext".
85
86 If key is found in lexicons, it is applied in the same way as
87 "maketext". Otherwise, it looks at all lexicon entries that could
88 possibly yield key, by turning "[...]" sequences into "(.*?)" and match
89 the resulting regular expression against key.
90
91 Once it finds all candidate entries, the longest one replaces the key
92 for the real "maketext" call. Variables matched by its bracket
93 sequences ($1, $2...) are placed before parameters; the order of
94 variables in the matched entry are correctly preserved.
95
96 For example, if the matched entry in %Lexicon is "Test [_1]", this
97 call:
98
99 $fh->maketext_fuzzy("Test string", "param");
100
101 is equivalent to this:
102
103 $fh->maketext("Test [_1]", "string", "param");
104
105 However, most of the time you won't need to supply parameters to a
106 "maketext_fuzzy" call, since all parameters are already interpolated
107 into the string.
108
109 $lh->override_maketext([flag]);
110 If flag is true, this accessor method turns "$lh->maketext" into an
111 alias for "$lh->maketext_fuzzy", so all consecutive "maketext" calls in
112 the $lh's packages are automatically fuzzy. A false flag restores the
113 original behaviour. If the flag is not specified, returns the current
114 status of override; the default is 0 (no overriding).
115
116 Note that this call only modifies the symbol table of the language
117 class that $lh belongs to, so other languages are not affected. If you
118 want to override all language handles in a certain application, try
119 this:
120
121 MyApp::L10N->override_maketext(1);
122
124 · The "longer is better" heuristic to determine the best match is
125 reasonably good, but could certainly be improved.
126
127 · Currently, "[quant,_1,file] deleted" won't match "3 files deleted";
128 you'll have to write "[quant,_1,file,files] deleted" instead, or
129 simply use "[_1] file deleted" as the lexicon key and put the
130 correct plural form handling into the corresponding value.
131
132 · When used in combination with "Locale::Maketext::Lexicon"'s "Tie"
133 backend, all keys would be iterated over each time a fuzzy match is
134 performed, and may cause serious speed penalty. Patches welcome.
135
137 Locale::Maketext, Locale::Maketext::Lexicon
138
140 This particular module was written to facilitate an auto-extraction
141 layer for Slashcode's Template Toolkit provider, based on
142 "HTML::Parser" and "Template::Parser". It would work like this:
143
144 Input | <B>from the [% story.dept %] dept.</B>
145 Output| <B>[%|loc( story.dept )%]from the [_1] dept.[%END%]</B>
146
147 Now, this layer suffers from the same linguistic problems as an
148 ordinary "Msgcat" or "Gettext" framework does -- what if we want to
149 make ordinates from "[% story.dept %]" (i.e. "from the 3rd dept."), or
150 expand the "dept." to "department" / "departments"?
151
152 The same problem occurred in RT's web interface, where it had to
153 localize messages returned by external modules, which may already
154 contain interpolated variables, e.g. "Successfully deleted 7 ticket(s)
155 in 'c:\temp'.".
156
157 Since I didn't have the time to refactor "DBI" and
158 "DBI::SearchBuilder", I devised a "loc_match" method to pre-process
159 their messages into one of the candidate strings, then applied the
160 matched string to "maketext".
161
162 Afterwards, I realized that instead of preparing a set of candidate
163 strings, I could actually use the original lexicon file (i.e. PO files
164 via "Locale::Maketext::Lexicon") to match against. This is how
165 "Locale::Maketext::Fuzzy" was born.
166
168 Audrey Tang <cpan@audreyt.org>
169
171 Copyright 2002, 2007 by Audrey Tang <cpan@audreyt.org>.
172
173 This software is released under the MIT license cited below.
174
175 The "MIT" License
176 Permission is hereby granted, free of charge, to any person obtaining a
177 copy of this software and associated documentation files (the
178 "Software"), to deal in the Software without restriction, including
179 without limitation the rights to use, copy, modify, merge, publish,
180 distribute, sublicense, and/or sell copies of the Software, and to
181 permit persons to whom the Software is furnished to do so, subject to
182 the following conditions:
183
184 The above copyright notice and this permission notice shall be included
185 in all copies or substantial portions of the Software.
186
187 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
188 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
189 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
190 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
191 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
192 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
193 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
194
195
196
197perl v5.12.0 2007-10-14 Locale::Maketext::Fuzzy(3)