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.02 of Locale::Maketext::Fuzzy.
10
12 package MyApp::L10N;
13 use base 'Locale::Maketext::Fuzzy'; # instead of Locale::Maketext
14
15 package MyApp::L10N::de;
16 use base 'MyApp::L10N';
17 our %Lexicon = (
18 # Exact match should always be preferred if possible
19 "0 camels were released."
20 => "Exact match",
21
22 # Fuzzy match candidate
23 "[quant,_1,camel was,camels were] released."
24 => "[quant,_1,Kamel wurde,Kamele wurden] freigegeben.",
25
26 # This could also match fuzzily, but is less preferred
27 "[_2] released[_1]"
28 => "[_1][_2] ist frei[_1]",
29 );
30
31 package main;
32 my $lh = MyApp::L10N->get_handle('de');
33
34 # All ->maketext calls below will become ->maketext_fuzzy instead
35 $lh->override_maketext(1);
36
37 # This prints "Exact match"
38 print $lh->maketext('0 camels were released.');
39
40 # "1 Kamel wurde freigegeben." -- quant() gets 1
41 print $lh->maketext('1 camel was released.');
42
43 # "2 Kamele wurden freigegeben." -- quant() gets 2
44 print $lh->maketext('2 camels were released.');
45
46 # "3 Kamele wurden freigegeben." -- parameters are ignored
47 print $lh->maketext('3 released.');
48
49 # "4 Kamele wurden freigegeben." -- normal usage
50 print $lh->maketext('[*,_1,camel was,camels were] released.', 4);
51
52 # "!Perl ist frei!" -- matches the broader one
53 # Note that the sequence ([_2] before [_1]) is preserved
54 print $lh->maketext('Perl released!');
55
57 This module is a subclass of "Locale::Maketext", with additional sup‐
58 port for localizing messages that already contains interpolated vari‐
59 ables. This is most useful when the messages are returned by external
60 modules -- for example, to match "dir: command not found" against
61 "[_1]: command not found".
62
63 Of course, this module is also useful if you're simply too lazy to use
64 the
65
66 $lh->maketext("[quant,_1,file,files] deleted.", $count);
67
68 syntax, but wish to write
69
70 $lh->maketext_fuzzy("$count files deleted");
71
72 instead, and have the correct plural form figured out automatically.
73
74 If "maketext_fuzzy" seems too long to type for you, this module also
75 provides a "override_maketext" method to turn all "maketext" calls into
76 "maketext_fuzzy" calls.
77
79 $lh->maketext_fuzzy(key[, parameters...]);
80
81 That method takes exactly the same arguments as the "maketext" method
82 of "Locale::Maketext".
83
84 If key is found in lexicons, it is applied in the same way as "make‐
85 text". Otherwise, it looks at all lexicon entries that could possibly
86 yield key, by turning "[...]" sequences into "(.*?)" and match the
87 resulting regular expression against key.
88
89 Once it finds all candidate entries, the longest one replaces the key
90 for the real "maketext" call. Variables matched by its bracket
91 sequences ($1, $2...) are placed before parameters; the order of vari‐
92 ables in the matched entry are correctly preserved.
93
94 For example, if the matched entry in %Lexicon is "Test [_1]", this
95 call:
96
97 $fh->maketext_fuzzy("Test string", "param");
98
99 is equivalent to this:
100
101 $fh->maketext("Test [_1]", "string", "param");
102
103 However, most of the time you won't need to supply parameters to a
104 "maketext_fuzzy" call, since all parameters are already interpolated
105 into the string.
106
107 $lh->override_maketext([flag]);
108
109 If flag is true, this accessor method turns "$lh->maketext" into an
110 alias for "$lh->maketext_fuzzy", so all consecutive "maketext" calls in
111 the $lh's packages are automatically fuzzy. A false flag restores the
112 original behaviour. If the flag is not specified, returns the current
113 status of override; the default is 0 (no overriding).
114
115 Note that this call only modifies the symbol table of the language
116 class that $lh belongs to, so other languages are not affected. If you
117 want to override all language handles in a certain application, try
118 this:
119
120 MyApp::L10N->override_maketext(1);
121
123 · The "longer is better" heuristic to determine the best match is
124 reasonably good, but could certainly be improved.
125
126 · Currently, "[quant,_1,file] deleted" won't match "3 files deleted";
127 you'll have to write "[quant,_1,file,files] deleted" instead, or
128 simply use "[_1] file deleted" as the lexicon key and put the cor‐
129 rect plural form handling into the corresponding value.
130
131 · When used in combination with "Locale::Maketext::Lexicon"'s "Tie"
132 backend, all keys would be iterated over each time a fuzzy match is
133 performed, and may cause serious speed penalty. Patches welcome.
134
136 Locale::Maketext, Locale::Maketext::Lexicon
137
139 This particular module was written to facilitate an auto-extraction
140 layer for Slashcode's Template Toolkit provider, based on
141 "HTML::Parser" and "Template::Parser". It would work like this:
142
143 Input ⎪ <B>from the [% story.dept %] dept.</B>
144 Output⎪ <B>[%⎪loc( story.dept )%]from the [_1] dept.[%END%]</B>
145
146 Now, this layer suffers from the same linguistic problems as an ordi‐
147 nary "Msgcat" or "Gettext" framework does -- what if we want to make
148 ordinates from "[% story.dept %]" (i.e. "from the 3rd dept."), or
149 expand the "dept." to "department" / "departments"?
150
151 The same problem occurred in RT's web interface, where it had to local‐
152 ize messages returned by external modules, which may already contain
153 interpolated variables, e.g. ""Successfully deleted 7 ticket(s) in
154 'c:\temp'."".
155
156 Since I didn't have the time to refactor "DBI" and "DBI::Search‐
157 Builder", I devised a "loc_match" method to pre-process their messages
158 into one of the candidate strings, then applied the matched string to
159 "maketext".
160
161 Afterwards, I realized that instead of preparing a set of candidate
162 strings, I could actually use the original lexicon file (i.e. PO files
163 via "Locale::Maketext::Lexicon") to match against. This is how
164 "Locale::Maketext::Fuzzy" was born.
165
167 Autrijus Tang <autrijus@autrijus.org>
168
170 Copyright 2002 by Autrijus Tang <autrijus@autrijus.org>.
171
172 This program is free software; you can redistribute it and/or modify it
173 under the same terms as Perl itself.
174
175 See <http://www.perl.com/perl/misc/Artistic.html>
176
177
178
179perl v5.8.8 2002-10-01 Locale::Maketext::Fuzzy(3)