📄 locale::maketext.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...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" '''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 turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "Locale::Maketext 3".TH Locale::Maketext 3 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" 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 \- framework for localization.SH "SYNOPSIS".IX Header "SYNOPSIS".Vb 9\& package MyProgram;\& use strict;\& use MyProgram::L10N;\& # ...which inherits from Locale::Maketext\& my $lh = MyProgram::L10N\->get_handle() || die "What language?";\& ...\& # And then any messages your program emits, like:\& warn $lh\->maketext( "Can\*(Aqt open file [_1]: [_2]\en", $f, $! );\& ....Ve.SH "DESCRIPTION".IX Header "DESCRIPTION"It is a common feature of applications (whether run directly,or via the Web) for them to be \*(L"localized\*(R" \*(-- i.e., for themto a present an English interface to an English-speaker, a Germaninterface to a German-speaker, and so on for all languages it'sprogrammed with. Locale::Maketextis a framework for software localization; it provides you with thetools for organizing and accessing the bits of text and text-processingcode that you need for producing localized applications..PPIn order to make sense of Maketext and how all itscomponents fit together, you should probablygo read Locale::Maketext::TPJ13, and\&\fIthen\fR read the following documentation..PPYou may also want to read over the source for \f(CW\*(C`File::Findgrep\*(C'\fRand its constituent modules \*(-- they are a complete (if small)example application that uses Maketext..SH "QUICK OVERVIEW".IX Header "QUICK OVERVIEW"The basic design of Locale::Maketext is object-oriented, andLocale::Maketext is an abstract base class, from which youderive a \*(L"project class\*(R".The project class (with a name like \*(L"TkBocciBall::Localize\*(R",which you then use in your module) is in turn the base classfor all the \*(L"language classes\*(R" for your project(with names \*(L"TkBocciBall::Localize::it\*(R", \&\*(L"TkBocciBall::Localize::en\*(R",\&\*(L"TkBocciBall::Localize::fr\*(R", etc.)..PPA language class isa class containing a lexicon of phrases as class data,and possibly also some methods that are of use in interpretingphrases in the lexicon, or otherwise dealing with text in thatlanguage..PPAn object belonging to a language class is called a \*(L"languagehandle\*(R"; it's typically a flyweight object..PPThe normal course of action is to call:.PP.Vb 6\& use TkBocciBall::Localize; # the localization project class\& $lh = TkBocciBall::Localize\->get_handle();\& # Depending on the user\*(Aqs locale, etc., this will\& # make a language handle from among the classes available,\& # and any defaults that you declare.\& die "Couldn\*(Aqt make a language handle??" unless $lh;.Ve.PPFrom then on, you use the \f(CW\*(C`maketext\*(C'\fR function to accessentries in whatever lexicon(s) belong to the language handleyou got. So, this:.PP.Vb 1\& print $lh\->maketext("You won!"), "\en";.Ve.PP\&...emits the right text for this language. If the objectin \f(CW$lh\fR belongs to class \*(L"TkBocciBall::Localize::fr\*(R" and\&\f(CW%TkBocciBall::Localize::fr::Lexicon\fR contains \f(CW\*(C`("You won!"=> "Tu as gagne\*'!")\*(C'\fR, then the abovecode happily tells the user \*(L"Tu as gagne\*'!\*(R"..SH "METHODS".IX Header "METHODS"Locale::Maketext offers a variety of methods, which fallinto three categories:.IP "\(bu" 4Methods to do with constructing language handles..IP "\(bu" 4\&\f(CW\*(C`maketext\*(C'\fR and other methods to do with accessing \f(CW%Lexicon\fR datafor a given language handle..IP "\(bu" 4Methods that you may find it handy to use, from routines ofyours that you put in \f(CW%Lexicon\fR entries..PPThese are covered in the following section..Sh "Construction Methods".IX Subsection "Construction Methods"These are to do with constructing a language handle:.IP "\(bu" 4\&\f(CW$lh\fR = YourProjClass\->get_handle( ...langtags... ) || die \*(L"lg-handle?\*(R";.SpThis tries loading classes based on the language-tags you give (like\&\f(CW\*(C`("en\-US", "sk", "kon", "es\-MX", "ja", "i\-klingon")\*(C'\fR, and for the first classthat succeeds, returns YourProjClass::\fIlanguage\fR\->\fInew()\fR..SpIf it runs thru the entire given list of language-tags, and finds no classesfor those exact terms, it then tries \*(L"superordinate\*(R" language classes.So if no \*(L"en-US\*(R" class (i.e., YourProjClass::en_us)was found, nor classes for anything else in that list, we then tryits superordinate, \*(L"en\*(R" (i.e., YourProjClass::en), and so on thru the other language-tags in the given list: \*(L"es\*(R".(The other language-tags in our example list: happen to have no superordinates.).SpIf none of those language-tags leads to loadable classes, we thentry classes derived from YourProjClass\->\fIfallback_languages()\fR andthen if nothing comes of that, we use classes named byYourProjClass\->\fIfallback_language_classes()\fR. Then in the (probablyquite unlikely) event that that fails, we just return undef..IP "\(bu" 4\&\f(CW$lh\fR = YourProjClass\->get_handle\fB()\fR || die \*(L"lg-handle?\*(R";.SpWhen \f(CW\*(C`get_handle\*(C'\fR is called with an empty parameter list, magic happens:.SpIf \f(CW\*(C`get_handle\*(C'\fR senses that it's running in program that wasinvoked as a \s-1CGI\s0, then it tries to get language-tags out of theenvironment variable \*(L"\s-1HTTP_ACCEPT_LANGUAGE\s0\*(R", and it pretends thatthose were the languages passed as parameters to \f(CW\*(C`get_handle\*(C'\fR..SpOtherwise (i.e., if not a \s-1CGI\s0), this tries various OS-specific waysto get the language-tags for the current locale/language, and thenpretends that those were the value(s) passed to \f(CW\*(C`get_handle\*(C'\fR..SpCurrently this OS-specific stuff consists of looking in the environmentvariables \*(L"\s-1LANG\s0\*(R" and \*(L"\s-1LANGUAGE\s0\*(R"; and on MSWin machines (where thosevariables are typically unused), this also tries usingthe module Win32::Locale to get a language-tag for whatever language/localeis currently selected in the \*(L"Regional Settings\*(R" (or \*(L"International\*(R"?)Control Panel. I welcome furthersuggestions for making this do the Right Thing under other operatingsystems that support localization..SpIf you're using localization in an application that keeps a configurationfile, you might consider something like this in your project class:.Sp.Vb 10\& sub get_handle_via_config {\& my $class = $_[0];\& my $chosen_language = $Config_settings{\*(Aqlanguage\*(Aq};\& my $lh;\& if($chosen_language) {\& $lh = $class\->get_handle($chosen_language)\& || die "No language handle for \e"$chosen_language\e" or the like";\& } else {\& # Config file missing, maybe?\& $lh = $class\->get_handle()\& || die "Can\*(Aqt get a language handle";\& }\& return $lh;\& }.Ve.IP "\(bu" 4\&\f(CW$lh\fR = YourProjClass::langname\->\fInew()\fR;.SpThis constructs a language handle. You usually \fBdon't\fR call thisdirectly, but instead let \f(CW\*(C`get_handle\*(C'\fR find a language class to \f(CW\*(C`use\*(C'\fRand to then call \->new on..IP "\(bu" 4\&\f(CW$lh\fR\->\fIinit()\fR;.SpThis is called by \->new to initialize newly-constructed language handles.If you define an init method in your class, remember that it's usuallyconsidered a good idea to call \f(CW$lh\fR\->SUPER::init in it (presumably at thebeginning), so that all classes get a chance to initialize a new objecthowever they see fit..IP "\(bu" 4YourProjClass\->\fIfallback_languages()\fR.Sp\&\f(CW\*(C`get_handle\*(C'\fR appends the return value of this to the end ofwhatever list of languages you pass \f(CW\*(C`get_handle\*(C'\fR. Unlessyou override this method, your project classwill inherit Locale::Maketext's \f(CW\*(C`fallback_languages\*(C'\fR, whichcurrently returns \f(CW\*(C`(\*(Aqi\-default\*(Aq, \*(Aqen\*(Aq, \*(Aqen\-US\*(Aq)\*(C'\fR.(\*(L"i\-default\*(R" is defined in \s-1RFC\s0 2277)..SpThis method (by having it return the nameof a language-tag that has an existing language class)can be used for making sure that\&\f(CW\*(C`get_handle\*(C'\fR will always manage to construct a languagehandle (assuming your language classes are in an appropriate\&\f(CW@INC\fR directory). Or you can use the next method:.IP "\(bu" 4YourProjClass\->\fIfallback_language_classes()\fR.Sp\&\f(CW\*(C`get_handle\*(C'\fR appends the return value of this to the endof the list of classes it will try using. Unlessyou override this method, your project classwill inherit Locale::Maketext's \f(CW\*(C`fallback_language_classes\*(C'\fR,which currently returns an empty list, \f(CW\*(C`()\*(C'\fR.By setting this to some value (namely, the name of a loadablelanguage class), you can be sure that\&\f(CW\*(C`get_handle\*(C'\fR will always manage to construct a languagehandle..ie n .Sh "The ""maketext"" Method".el .Sh "The ``maketext'' Method".IX Subsection "The maketext Method"This is the most important method in Locale::Maketext:.PP.Vb 1\& $text = $lh\->maketext(I<key>, ...parameters for this phrase...);.Ve.PPThis looks in the \f(CW%Lexicon\fR of the language handle\&\f(CW$lh\fR and all its superclasses, lookingfor an entry whose key is the string \fIkey\fR. Assuming suchan entry is found, various things then happen, depending on thevalue found:.PPIf the value is a scalarref, the scalar is dereferenced and returned(and any parameters are ignored)..PPIf the value is a coderef, we return &$value($lh, ...parameters...)..PPIf the value is a string that \fIdoesn't\fR look like it's in Bracket Notation,we return it (after replacing it with a scalarref, in its \f(CW%Lexicon\fR)..PPIf the value \fIdoes\fR look like it's in Bracket Notation, then we compileit into a sub, replace the string in the \f(CW%Lexicon\fR with the new coderef,and then we return &$new_sub($lh, ...parameters...).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -