.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Locale::Maketext::Fuzzy 3pm" .TH Locale::Maketext::Fuzzy 3pm "2018-03-24" "perl v5.26.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Locale::Maketext::Fuzzy \- Maketext from already interpolated strings .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& package MyApp::L10N; \& use base \*(AqLocale::Maketext::Fuzzy\*(Aq; # instead of Locale::Maketext \& \& package MyApp::L10N::de; \& use base \*(AqMyApp::L10N\*(Aq; \& our %Lexicon = ( \& # Exact match should always be preferred if possible \& "0 camels were released." \& => "Exact match", \& \& # Fuzzy match candidate \& "[quant,_1,camel was,camels were] released." \& => "[quant,_1,Kamel wurde,Kamele wurden] freigegeben.", \& \& # This could also match fuzzily, but is less preferred \& "[_2] released[_1]" \& => "[_1][_2] ist frei[_1]", \& ); \& \& package main; \& my $lh = MyApp::L10N\->get_handle(\*(Aqde\*(Aq); \& \& # All \->maketext calls below will become \->maketext_fuzzy instead \& $lh\->override_maketext(1); \& \& # This prints "Exact match" \& print $lh\->maketext(\*(Aq0 camels were released.\*(Aq); \& \& # "1 Kamel wurde freigegeben." \-\- quant() gets 1 \& print $lh\->maketext(\*(Aq1 camel was released.\*(Aq); \& \& # "2 Kamele wurden freigegeben." \-\- quant() gets 2 \& print $lh\->maketext(\*(Aq2 camels were released.\*(Aq); \& \& # "3 Kamele wurden freigegeben." \-\- parameters are ignored \& print $lh\->maketext(\*(Aq3 released.\*(Aq); \& \& # "4 Kamele wurden freigegeben." \-\- normal usage \& print $lh\->maketext(\*(Aq[*,_1,camel was,camels were] released.\*(Aq, 4); \& \& # "!Perl ist frei!" \-\- matches the broader one \& # Note that the sequence ([_2] before [_1]) is preserved \& print $lh\->maketext(\*(AqPerl released!\*(Aq); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module is a subclass of \f(CW\*(C`Locale::Maketext\*(C'\fR, with additional support for localizing messages that already contains interpolated variables. .PP This is most useful when the messages are returned by external sources \&\*(-- for example, to match \f(CW\*(C`dir: command not found\*(C'\fR against \&\f(CW\*(C`[_1]: command not found\*(C'\fR. .PP Of course, this module is also useful if you're simply too lazy to use the .PP .Vb 1 \& $lh\->maketext("[quant,_1,file,files] deleted.", $count); .Ve .PP syntax, but wish to write .PP .Vb 1 \& $lh\->maketext_fuzzy("$count files deleted"); .Ve .PP instead, and have the correct plural form figured out automatically. .PP If \f(CW\*(C`maketext_fuzzy\*(C'\fR seems too long to type for you, this module also provides a \f(CW\*(C`override_maketext\*(C'\fR method to turn \fIall\fR \f(CW\*(C`maketext\*(C'\fR calls into \f(CW\*(C`maketext_fuzzy\*(C'\fR calls. .SH "METHODS" .IX Header "METHODS" .ie n .SS "$lh\->maketext_fuzzy(\fIkey\fP[, \fIparameters...\fP]);" .el .SS "\f(CW$lh\fP\->maketext_fuzzy(\fIkey\fP[, \fIparameters...\fP]);" .IX Subsection "$lh->maketext_fuzzy(key[, parameters...]);" That method takes exactly the same arguments as the \f(CW\*(C`maketext\*(C'\fR method of \f(CW\*(C`Locale::Maketext\*(C'\fR. .PP If \fIkey\fR is found in lexicons, it is applied in the same way as \&\f(CW\*(C`maketext\*(C'\fR. Otherwise, it looks at all lexicon entries that could possibly yield \fIkey\fR, by turning \f(CW\*(C`[...]\*(C'\fR sequences into \f(CW\*(C`(.*?)\*(C'\fR and match the resulting regular expression against \fIkey\fR. .PP Once it finds all candidate entries, the longest one replaces the \&\fIkey\fR for the real \f(CW\*(C`maketext\*(C'\fR call. Variables matched by its bracket sequences (\f(CW$1\fR, \f(CW$2\fR...) are placed before \fIparameters\fR; the order of variables in the matched entry are correctly preserved. .PP For example, if the matched entry in \f(CW%Lexicon\fR is \f(CW\*(C`Test [_1]\*(C'\fR, this call: .PP .Vb 1 \& $fh\->maketext_fuzzy("Test string", "param"); .Ve .PP is equivalent to this: .PP .Vb 1 \& $fh\->maketext("Test [_1]", "string", "param"); .Ve .PP However, most of the time you won't need to supply \fIparameters\fR to a \f(CW\*(C`maketext_fuzzy\*(C'\fR call, since all parameters are already interpolated into the string. .ie n .SS "$lh\->override_maketext([\fIflag\fP]);" .el .SS "\f(CW$lh\fP\->override_maketext([\fIflag\fP]);" .IX Subsection "$lh->override_maketext([flag]);" If \fIflag\fR is true, this accessor method turns \f(CW\*(C`$lh\->maketext\*(C'\fR into an alias for \f(CW\*(C`$lh\->maketext_fuzzy\*(C'\fR, so all consecutive \&\f(CW\*(C`maketext\*(C'\fR calls in the \f(CW$lh\fR's packages are automatically fuzzy. A false \fIflag\fR restores the original behaviour. If the flag is not specified, returns the current status of override; the default is 0 (no overriding). .PP Note that this call only modifies the symbol table of the \fIlanguage class\fR that \f(CW$lh\fR belongs to, so other languages are not affected. If you want to override all language handles in a certain application, try this: .PP .Vb 1 \& MyApp::L10N\->override_maketext(1); .Ve .SH "CAVEATS" .IX Header "CAVEATS" .IP "\(bu" 4 The \*(L"longer is better\*(R" heuristic to determine the best match is reasonably good, but could certainly be improved. .IP "\(bu" 4 Currently, \f(CW"[quant,_1,file] deleted"\fR won't match \f(CW"3 files deleted"\fR; you'll have to write \f(CW"[quant,_1,file,files] deleted"\fR instead, or simply use \f(CW"[_1] file deleted"\fR as the lexicon key and put the correct plural form handling into the corresponding value. .IP "\(bu" 4 When used in combination with \f(CW\*(C`Locale::Maketext::Lexicon\*(C'\fR's \f(CW\*(C`Tie\*(C'\fR backend, all keys would be iterated over each time a fuzzy match is performed, and may cause serious speed penalty. Patches welcome. .SH "SEE ALSO" .IX Header "SEE ALSO" Locale::Maketext, Locale::Maketext::Lexicon .SH "HISTORY" .IX Header "HISTORY" This particular module was written to facilitate an \fIauto-extraction\fR layer for Slashcode's \fITemplate Toolkit\fR provider, based on \&\f(CW\*(C`HTML::Parser\*(C'\fR and \f(CW\*(C`Template::Parser\*(C'\fR. It would work like this: .PP .Vb 2 \& Input | from the [% story.dept %] dept. \& Output| [%|loc( story.dept )%]from the [_1] dept.[%END%] .Ve .PP Now, this layer suffers from the same linguistic problems as an ordinary \f(CW\*(C`Msgcat\*(C'\fR or \f(CW\*(C`Gettext\*(C'\fR framework does \*(-- what if we want to make ordinals from \f(CW\*(C`[% story.dept %]\*(C'\fR (i.e. \f(CW\*(C`from the 3rd dept.\*(C'\fR), or expand the \f(CW\*(C`dept.\*(C'\fR to \f(CW\*(C`department\*(C'\fR / \f(CW\*(C`departments\*(C'\fR? .PP The same problem occurred in \s-1RT\s0's web interface, where it had to localize messages returned by external modules, which may already contain interpolated variables, e.g. \f(CW"Successfully deleted 7 ticket(s) in \*(Aqc:\etemp\*(Aq."\fR. .PP Since I didn't have the time to refactor \f(CW\*(C`DBI\*(C'\fR and \f(CW\*(C`DBI::SearchBuilder\*(C'\fR, I devised a \f(CW\*(C`loc_match\*(C'\fR method to pre-process their messages into one of the \fIcandidate strings\fR, then applied the matched string to \f(CW\*(C`maketext\*(C'\fR. .PP Afterwards, I realized that instead of preparing a set of candidate strings, I could actually match against the original \fIlexicon file\fR (i.e. \s-1PO\s0 files via \f(CW\*(C`Locale::Maketext::Lexicon\*(C'\fR). This is how \&\f(CW\*(C`Locale::Maketext::Fuzzy\*(C'\fR was born. .SH "AUTHORS" .IX Header "AUTHORS" Audrey Tang .SH "CC0 1.0 Universal" .IX Header "CC0 1.0 Universal" To the extent possible under law, 唐鳳 has waived all copyright and related or neighboring rights to Locale-Maketext-Fuzzy. .PP This work is published from Taiwan. .PP .SH "POD ERRORS" .IX Header "POD ERRORS" Hey! \fBThe above document had some coding errors, which are explained below:\fR .IP "Around line 318:" 4 .IX Item "Around line 318:" Non-ASCII character seen before =encoding in '唐鳳'. Assuming \s-1UTF\-8\s0