⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 locale::maketext::tpj13.3

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 3
📖 第 1 页 / 共 3 页
字号:
hard things possible\*(R", and see also the role it played in theMIDI::Simple language, discussed elsewhere in this issue [TPJ#13]..PPConsider our first stab at an entry in our \*(L"phrasebook of functions\*(R":.PP.Vb 8\&  sub I_found_X1_files_in_X2_directories {\&    my( $files, $dirs ) = @_[0,1];\&    $files = sprintf("%g %s", $files,\&      $files == 1 ? \*(Aqfichier\*(Aq : \*(Aqfichiers\*(Aq);\&    $dirs = sprintf("%g %s", $dirs,\&      $dirs == 1 ? "r\exE9pertoire" : "r\exE9pertoires");\&    return "J\*(Aqai trouv\exE9 $files dans $dirs.";\&  }.Ve.PPYou may sense that a lexicon (to use a non-committal catch-all term for acollection of things you know how to say, regardless of whether they'rephrases or words) consisting of functions \fIexpressed\fR as above wouldmake for rather long-winded and repetitive code \*(-- even if you wiselyrewrote this to have quantification (as we call adding a numberexpression to a noun phrase) be a function called like:.PP.Vb 6\&  sub I_found_X1_files_in_X2_directories {\&    my( $files, $dirs ) = @_[0,1];\&    $files = quant($files, "fichier");\&    $dirs =  quant($dirs,  "r\exE9pertoire");\&    return "J\*(Aqai trouv\exE9 $files dans $dirs.";\&  }.Ve.PPAnd you may also sense that you do not want to bother your translatorswith having to write Perl code \*(-- you'd much rather that they spendtheir \fIvery costly time\fR on just translation.  And this is to saynothing of the near impossibility of finding a commercial translatorwho would know even simple Perl..PPIn a first-hack implementation of Maketext, each language-module'slexicon looked like this:.PP.Vb 10\& %Lexicon = (\&   "I found %g files in %g directories"\&   => sub {\&      my( $files, $dirs ) = @_[0,1];\&      $files = quant($files, "fichier");\&      $dirs =  quant($dirs,  "r\exE9pertoire");\&      return "J\*(Aqai trouv\exE9 $files dans $dirs.";\&    },\&  ... and so on with other phrase => sub mappings ...\& );.Ve.PPbut I immediately went looking for some more concise way to basicallydenote the same phrase-function \*(-- a way that would also serve toconcisely denote \fImost\fR phrase-functions in the lexicon for \fImost\fRlanguages.  After much time and even some actual thought, I decided onthis system:.PP* Where a value in a \f(CW%Lexicon\fR hash is a contentful string instead ofan anonymous sub (or, conceivably, a coderef), it would be interpretedas a sort of shorthand expression of what the sub does.  When accessedfor the first time in a session, it is parsed, turned into Perl code,and then eval'd into an anonymous sub; then that sub replaces theoriginal string in that lexicon.  (That way, the work of parsing andevaling the shorthand form for a given phrase is done no more thanonce per session.).PP* Calls to \f(CW\*(C`maketext\*(C'\fR (as Maketext's main function is called) happenthru a \*(L"language session handle\*(R", notionally very much like an \s-1IO\s0handle, in that you open one at the start of the session, and use itfor \*(L"sending signals\*(R" to an object in order to have it return the textyou want..PPSo, this:.PP.Vb 2\&  $lang\->maketext("You have [quant,_1,piece] of new mail.",\&                 scalar(@messages));.Ve.PPbasically means this: look in the lexicon for \f(CW$lang\fR (which may inheritfrom any number of other lexicons), and find the function that wehappen to associate with the string \*(L"You have [quant,_1,piece] of newmail\*(R" (which is, and should be, a functioning \*(L"shorthand\*(R" for thisfunction in the native locale \*(-- English in this case).  If you findsuch a function, call it with \f(CW$lang\fR as its first parameter (as if itwere a method), and then a copy of scalar(@messages) as its second,and then return that value.  If that function was found, but was instring shorthand instead of being a fully specified function, parse itand make it into a function before calling it the first time..PP* The shorthand uses code in brackets to indicate method calls thatshould be performed.  A full explanation is not in order here, but afew examples will suffice:.PP.Vb 1\&  "You have [quant,_1,piece] of new mail.".Ve.PPThe above code is shorthand for, and will be interpreted as,this:.PP.Vb 8\&  sub {\&    my $handle = $_[0];\&    my(@params) = @_;\&    return join \*(Aq\*(Aq,\&      "You have ",\&      $handle\->quant($params[1], \*(Aqpiece\*(Aq),\&      "of new mail.";\&  }.Ve.PPwhere \*(L"quant\*(R" is the name of a method you're using to quantify thenoun \*(L"piece\*(R" with the number \f(CW$params\fR[0]..PPA string with no brackety calls, like this:.PP.Vb 1\&  "Your search expression was malformed.".Ve.PPis somewhat of a degerate case, and just gets turned into:.PP.Vb 1\&  sub { return "Your search expression was malformed." }.Ve.PPHowever, not everything you can write in Perl code can be written inthe above shorthand system \*(-- not by a long shot.  For example, considerthe Italian translator from the beginning of this article, who wantedthe Italian for \*(L"I didn't find any files\*(R" as a special case, insteadof \*(L"I found 0 files\*(R".  That couldn't be specified (at least not easilyor simply) in our shorthand system, and it would have to be writtenout in full, like this:.PP.Vb 10\&  sub {  # pretend the English strings are in Italian\&    my($handle, $files, $dirs) = @_[0,1,2];\&    return "I didn\*(Aqt find any files" unless $files;\&    return join \*(Aq\*(Aq,\&      "I found ",\&      $handle\->quant($files, \*(Aqfile\*(Aq),\&      " in ",\&      $handle\->quant($dirs,  \*(Aqdirectory\*(Aq),\&      ".";\&  }.Ve.PPNext to a lexicon full of shorthand code, that sort of sticks out like asore thumb \*(-- but this \fIis\fR a special case, after all; and at leastit's possible, if not as concise as usual..PPAs to how you'd implement the Russian example from the beginning ofthe article, well, There's More Than One Way To Do It, but it could besomething like this (using English words for Russian, just so you knowwhat's going on):.PP.Vb 1\&  "I [quant,_1,directory,accusative] scanned.".Ve.PPThis shifts the burden of complexity off to the quant method.  Thatmethod's parameters are: the numeric value it's going to use toquantify something; the Russian word it's going to quantify; and theparameter \*(L"accusative\*(R", which you're using to mean that thissentence's syntax wants a noun in the accusative case there, althoughthat quantification method may have to overrule, for grammaticalreasons you may recall from the beginning of this article..PPNow, the Russian quant method here is responsible not only forimplementing the strange logic necessary for figuring out how Russiannumber-phrases impose case and number on their noun-phrases, but alsofor inflecting the Russian word for \*(L"directory\*(R".  How that inflectionis to be carried out is no small issue, and among the solutions I'veseen, some (like variations on a simple lookup in a hash where allpossible forms are provided for all necessary words) arestraightforward but \fIcan\fR become cumbersome when you need to inflectmore than a few dozen words; and other solutions (like usingalgorithms to model the inflections, storing only root forms andirregularities) \fIcan\fR involve more overhead than is justifiable forall but the largest lexicons..PPMercifully, this design decision becomes crucial only in the hairiestof inflected languages, of which Russian is by no means the \fIworst\fR casescenario, but is worse than most.  Most languages have simplerinflection systems; for example, in English or Swahili, there aregenerally no more than two possible inflected forms for a given noun(\*(L"error/errors\*(R"; \*(L"kosa/makosa\*(R"), and therules for producing these forms are fairly simple \*(-- or at least,simple rules can be formulated that work for most words, and you canthen treat the exceptions as just \*(L"irregular\*(R", at least relative toyour ad hoc rules.  A simpler inflection system (simpler rules, fewerforms) means that design decisions are less crucial to maintainingsanity, whereas the same decisions could incuroverhead-versus-scalability problems in languages like Russian.  Itmay \fIalso\fR be likely that code (possibly in Perl, as withLingua::EN::Inflect, for English nouns) has alreadybeen written for the language in question, whether simple or complex..PPMoreover, a third possibility may even be simpler than anythingdiscussed above: \*(L"Just require that all possible (or at leastapplicable) forms be provided in the call to the given language's quantmethod, as in:\*(R".PP.Vb 1\&  "I found [quant,_1,file,files].".Ve.PPThat way, quant just has to chose which form it needs, without havingto look up or generate anything.  While possibly not optimal forRussian, this should work well for most other languages, wherequantification is not as complicated an operation..Sh "The Devil in the Details".IX Subsection "The Devil in the Details"There's plenty more to Maketext than described above \*(-- for example,there's the details of how language tags (\*(L"en-US\*(R", \*(L"i\-pwn\*(R", \*(L"fi\*(R",etc.) or locale IDs (\*(L"en_US\*(R") interact with actual module naming(\*(L"BogoQuery/Locale/en_us.pm\*(R"), and what magic can ensue; there's thedetails of how to record (and possibly negotiate) what characterencoding Maketext will return text in (\s-1UTF8\s0? Latin\-1? \s-1KOI8\s0?).  There'sthe interesting fact that Maketext is for localization, but nowhereactually has a "\f(CW\*(C`use locale;\*(C'\fR" anywhere in it.  For the curious,there's the somewhat frightening details of how I actuallyimplement something like data inheritance so that searches acrossmodules' \f(CW%Lexicon\fR hashes can parallel how Perl implements methodinheritance..PPAnd, most importantly, there's all the practical details of how toactually go about deriving from Maketext so you can use it for yourinterfaces, and the various tools and conventions for starting out andmaintaining individual language modules..PPThat is all covered in the documentation for Locale::Maketext and themodules that come with it, available in \s-1CPAN\s0.  After having read thisarticle, which covers the why's of Maketext, the documentation,which covers the how's of it, should be quite straightfoward..Sh "The Proof in the Pudding: Localizing Web Sites".IX Subsection "The Proof in the Pudding: Localizing Web Sites"Maketext and gettext have a notable difference: gettext is in C,accessible thru C library calls, whereas Maketext is in Perl, andreally can't work without a Perl interpreter (although I supposesomething like it could be written for C).  Accidents of history (andnot necessarily lucky ones) have made \*(C+ the most common language forthe implementation of applications like word processors, Web browsers,and even many in-house applications like custom query systems.  Currentconditions make it somewhat unlikely that the next one of any of thesekinds of applications will be written in Perl, albeit clearly more forreasons of custom and inertia than out of consideration of what is theright tool for the job..PPHowever, other accidents of history have made Perl a well-acceptedlanguage for design of server-side programs (generally in \s-1CGI\s0 form)for Web site interfaces.  Localization of static pages in Web sites istrivial, feasable either with simple language-negotiation features inservers like Apache, or with some kind of server-side inclusions oflanguage-appropriate text into layout templates.  However, I thinkthat the localization of Perl-based search systems (or other kinds ofdynamic content) in Web sites, be they public or access-restricted,is where Maketext will see the greatest use..PPI presume that it would be only the exceptional Web site that getslocalized for English \fIand\fR Chinese \fIand\fR Italian \fIand\fR Arabic\&\fIand\fR Russian, to recall the languages from the beginning of thisarticle \*(-- to say nothing of German, Spanish, French, Japanese,Finnish, and Hindi, to name a few languages that benefit from largenumbers of programmers or Web viewers or both..PPHowever, the ever-increasing internationalization of the Web (whethermeasured in terms of amount of content, of numbers of content writersor programmers, or of size of content audiences) makes it increasinglylikely that the interface to the average Web-based dynamic contentservice will be localized for two or maybe three languages.  It is myhope that Maketext will make that task as simple as possible, and willremove previous barriers to localization for languages dissimilar toEnglish..PP.Vb 1\& _\|_END_\|_.Ve.PPSean M. Burke (sburke@cpan.org) has a Master's in linguisticsfrom Northwestern University; he specializes in language technology.Jordan Lachler (lachler@unm.edu) is a PhD student in the Department ofLinguistics at the University of New Mexico; he specializes inmorphology and pedagogy of North American native languages..Sh "References".IX Subsection "References"Alvestrand, Harald Tveit.  1995.  \fI\s-1RFC\s0 1766: Tags for theIdentification of Languages.\fR\&\f(CW\*(C`ftp://ftp.isi.edu/in\-notes/rfc1766.txt\*(C'\fR[Now see \s-1RFC\s0 3066.].PPCallon, Ross, editor.  1996.  \fI\s-1RFC\s0 1925: The TwelveNetworking Truths.\fR\&\f(CW\*(C`ftp://ftp.isi.edu/in\-notes/rfc1925.txt\*(C'\fR.PPDrepper, Ulrich, Peter Miller,and Franc\*,ois Pinard.  1995\-2001.  \s-1GNU\s0\&\f(CW\*(C`gettext\*(C'\fR.  Available in \f(CW\*(C`ftp://prep.ai.mit.edu/pub/gnu/\*(C'\fR, withextensive docs in the distribution tarball.  [SinceI wrote this article in 1998, I now see that thegettext docs are now trying more to come to terms withplurality.  Whether useful conclusions have come from itis another question altogether. \*(-- \s-1SMB\s0, May 2001].PPForbes, Nevill.  1964.  \fIRussian Grammar.\fR  Third Edition, revisedby J. C. Dumbreck.  Oxford University Press.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -