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

📄 locale::maketext::tpj13.3

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 3
📖 第 1 页 / 共 3 页
字号:
test of \f(CW\*(C`($directory == 1)\*(C'\fR no longer does the job.  And it meansthat where English's grammatical category of number necessitatesonly the two permutations of the first sentence based on \*(L"directory[singular]\*(R" and \*(L"directories [plural]\*(R", Arabic has three \*(-- and,worse, in the second sentence (\*(L"Your query matched \f(CW%g\fR file in \f(CW%g\fRdirectory.\*(R"), where English has four, Arabic has nine.  You sensean unwelcome, exponential trend taking shape..PPYour Italian translator emails you back and says that \*(L"I searched 0directories\*(R" (a possible English output of your program) is stilted,and if you think that's fine English, that's your problem, but that\&\fIjust will not do\fR in the language of Dante.  He insists that where\&\f(CW$directory_count\fR is 0, your program should produce the Italian textfor "I \fIdidn't\fR scan \fIany\fR directories.\*(L".  And ditto for \*(R"I didn'tmatch any files in any directories\*(L", although he says the last partabout \*(R"in any directories" should probably just be left off..PPYou wonder how you'll get gettext to handle this; to accomodate theways Arabic, Chinese, and Italian deal with numbers in just these fewvery simple phrases, you need to write code that will ask gettext fordifferent queries depending on whether the numerical values inquestion are 1, 2, more than 2, or in some cases 0, and you still haven'tfigured out the problem with the different word order in Chinese..PPThen your Russian translator calls on the phone, to \fIpersonally\fR tellyou the bad news about how really unpleasant your life is about tobecome:.PPRussian, like German or Latin, is an inflectional language; that is, nounsand adjectives have to take endings that depend on their case(i.e., nominative, accusative, genitive, etc...) \*(-- which is roughly a matter ofwhat role they have in syntax of the sentence \*(--as well as on the grammatical gender (i.e., masculine, feminine, neuter)and number (i.e., singular or plural) of the noun, as well as on thedeclension class of the noun.  But unlike with most other inflected languages,putting a number-phrase (like \*(L"ten\*(R" or \*(L"forty-three\*(R", or their Arabicnumeral equivalents) in front of noun in Russian can change the case andnumber that noun is, and therefore the endings you have to put on it..PPHe elaborates:  In \*(L"I scanned \f(CW%g\fR directories\*(R", you'd \fIexpect\fR\&\*(L"directories\*(R" to be in the accusative case (since it is the directobject in the sentence) and the plural number,except where \f(CW$directory_count\fR is 1, then you'd expect the singular, ofcourse.  Just like Latin or German.  \fIBut!\fR  Where \f(CW$directory_count\fR %10 is 1 (\*(L"%\*(R" for modulo, remember), assuming \f(CW$directory\fR count is aninteger, and except where \f(CW$directory_count\fR % 100 is 11, \*(L"directories\*(R"is forced to become grammatically singular, which means it gets theending for the accusative singular...  You begin to visualize the codeit'd take to test for the problem so far, \fIand still work for Chineseand Arabic and Italian\fR, and how many gettext items that'd take, buthe keeps going...  But where \f(CW$directory_count\fR % 10 is 2, 3, or 4(except where \f(CW$directory_count\fR % 100 is 12, 13, or 14), the word for\&\*(L"directories\*(R" is forced to be genitive singular \*(-- which means anotherending... The room begins to spin around you, slowly at first...  Butwith \fIall other\fR integer values, since \*(L"directory\*(R" is an inanimatenoun, when preceded by a number and in the nominative or accusativecases (as it is here, just your luck!), it does stay plural, but it isforced into the genitive case \*(-- yet another ending...  Andyou never hear him get to the part about how you're going to run intosimilar (but maybe subtly different) problems with other Slaviclanguages like Polish, because the floor comes up to meet you, and youfade into unconsciousness..PPThe above cautionary tale relates how an attempt at localization canlead from programmer consternation, to program obfuscation, to a needfor sedation.  But careful evaluation shows that your choice of toolsmerely needed further consideration..Sh "The Linguistic View".IX Subsection "The Linguistic View".RS 4\&\*(L"It is more complicated than you think.\*(R".Sp\&\-\- The Eighth Networking Truth, from \s-1RFC\s0 1925.RE.PPThe field of Linguistics has expended a great deal of effort over thepast century trying to find grammatical patterns which hold acrosslanguages; it's been a constant processof people making generalizations that should apply to all languages,only to find out that, all too often, these generalizations fail \*(--sometimes failing for just a few languages, sometimes whole classes oflanguages, and sometimes nearly every language in the world exceptEnglish.  Broad statistical trends are evident in what the \*(L"averagelanguage\*(R" is like as far as what its rules can look like, must looklike, and cannot look like.  But the \*(L"average language\*(R" is just asunreal a concept as the \*(L"average person\*(R" \*(-- it runs up against thefact no language (or person) is, in fact, average.  The wisdom of pastexperience leads us to believe that any given language can do whateverit wants, in any order, with appeal to any kind of grammaticalcategories wants \*(-- case, number, tense, real or metaphoriccharacteristics of the things that words refer to, arbitrary orpredictable classifications of words based on what endings or prefixesthey can take, degree or means of certainty about the truth ofstatements expressed, and so on, ad infinitum..PPMercifully, most localization tasks are a matter of finding ways totranslate whole phrases, generally sentences, where the context isrelatively set, and where the only variation in content is \fIusually\fRin a number being expressed \*(-- as in the example sentences above.Translating specific, fully-formed sentences is, in practice, fairlyfoolproof \*(-- which is good, because that's what's in the phrasebooksthat so many tourists rely on.  Now, a given phrase (whether in aphrasebook or in a gettext lexicon) in one language \fImight\fR have agreater or lesser applicability than that phrase's translation intoanother language \*(-- for example, strictly speaking, in Arabic, the\&\*(L"your\*(R" in \*(L"Your query matched...\*(R" would take a different formdepending on whether the user is male or female; so the Arabictranslation \*(L"your[feminine] query\*(R" is applicable in fewer cases thanthe corresponding English phrase, which doesn't distinguish the user'sgender.  (In practice, it's not feasable to have a program know theuser's gender, so the masculine \*(L"you\*(R" in Arabic is usually used, bydefault.).PPBut in general, such surprises are rare when entire sentences arebeing translated, especially when the functional context is restrictedto that of a computer interacting with a user either to convey a factor to prompt for a piece of information.  So, for purposes oflocalization, translation by phrase (generally by sentence) is both thesimplest and the least problematic..Sh "Breaking gettext".IX Subsection "Breaking gettext".RS 4\&\*(L"It Has To Work.\*(R".Sp\&\-\- First Networking Truth, \s-1RFC\s0 1925.RE.PPConsider that sentences in a tourist phrasebook are of two types: oneslike \*(L"How do I get to the marketplace?\*(R" that don't have any blanks tofill in, and ones like \*(L"How much do these _\|_\|_ cost?\*(R", where there'sone or more blanks to fill in (and these are usually linked to alist of words that you can put in that blank: \*(L"fish\*(R", \*(L"potatoes\*(R",\&\*(L"tomatoes\*(R", etc.)  The ones with no blanks are no problem, but thefill-in-the-blank ones may not be really straightforward. If it's aSwahili phrasebook, for example, the authors probably didn't bother totell you the complicated ways that the verb \*(L"cost\*(R" changes itsinflectional prefix depending on the noun you're putting in the blank.The trader in the marketplace will still understand what you're saying ifyou say \*(L"how much do these potatoes cost?\*(R" with the wronginflectional prefix on \*(L"cost\*(R".  After all, \fIyou\fR can't speak proper Swahili,\&\fIyou're\fR just a tourist.  But while tourists can be stupid, computersare supposed to be smart; the computer should be able to fill in theblank, and still have the results be grammatical..PPIn other words, a phrasebook entry takes some values as parameters(the things that you fill in the blank or blanks), and provides a valuebased on these parameters, where the way you get that final value fromthe given values can, properly speaking, involve an arbitrarilycomplex series of operations.  (In the case of Chinese, it'd be not atall complex, at least in cases like the examples at the beginning ofthis article; whereas in the case of Russian it'd be a rather complexseries of operations.  And in some languages, thecomplexity could be spread around differently: while the act ofputting a number-expression in front of a noun phrase might not becomplex by itself, it may change how you have to, for example, inflecta verb elsewhere in the sentence.  This is what in syntax is called\&\*(L"long-distance dependencies\*(R".).PPThis talk of parameters and arbitrary complexity is just another wayto say that an entry in a phrasebook is what in a programming languagewould be called a \*(L"function\*(R".  Just so you don't miss it, this is thecrux of this article: \fIA phrase is a function; a phrasebook is abunch of functions.\fR.PPThe reason that using gettext runs into walls (as in the abovesecond-person horror story) is that you're trying to use a string (orworse, a choice among a bunch of strings) to do what you really need afunction for \*(-- which is futile.  Preforming (s)printf interpolationon the strings which you get back from gettext does allow you to do \fIsome\fRcommon things passably well... sometimes... sort of; but, to paraphrasewhat some people say about \f(CW\*(C`csh\*(C'\fR script programming, \*(L"it fools youinto thinking you can use it for real things, but you can't, and youdon't discover this until you've already spent too much time trying,and by then it's too late.\*(R".Sh "Replacing gettext".IX Subsection "Replacing gettext"So, what needs to replace gettext is a system that supports lexiconsof functions instead of lexicons of strings.  An entry in a lexiconfrom such a system should \fInot\fR look like this:.PP.Vb 1\&  "J\*(Aqai trouv\exE9 %g fichiers dans %g r\exE9pertoires".Ve.PP[\exE9 is e\-acute in Latin\-1.  Some pod renderers wouldscream if I used the actual character here. \*(-- \s-1SB\s0].PPbut instead like this, bearing in mind that this is just a first stab:.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.PPNow, there's no particularly obvious way to store anything but stringsin a gettext lexicon; so it looks like we just have to start over andmake something better, from scratch.  I call my shot at agettext-replacement system \*(L"Maketext\*(R", or, in \s-1CPAN\s0 terms,Locale::Maketext..PPWhen designing Maketext, I chose to plan its main features in terms of\&\*(L"buzzword compliance\*(R".  And here are the buzzwords:.Sh "Buzzwords: Abstraction and Encapsulation".IX Subsection "Buzzwords: Abstraction and Encapsulation"The complexity of the language you're trying to output a phrase in isentirely abstracted inside (and encapsulated within) the Maketext modulefor that interface.  When you call:.PP.Vb 2\&  print $lang\->maketext("You have [quant,_1,piece] of new mail.",\&                       scalar(@messages));.Ve.PPyou don't know (and in fact can't easily find out) whether this willinvolve lots of figuring, as in Russian (if \f(CW$lang\fR is a handle to theRussian module), or relatively little, as in Chinese.  That kind ofabstraction and encapsulation may encourage other pleasant buzzwordslike modularization and stratification, depending on what designdecisions you make..Sh "Buzzword: Isomorphism".IX Subsection "Buzzword: Isomorphism"\&\*(L"Isomorphism\*(R" means \*(L"having the same structure or form\*(R"; in discussionsof program design, the word takes on the special, specific meaning thatyour implementation of a solution to a problem \fIhas the samestructure\fR as, say, an informal verbal description of the solution, ormaybe of the problem itself.  Isomorphism is, all things considered,a good thing \*(-- it's what problem-solving (and solution-implementing)should look like..PPWhat's wrong the with gettext-using code like this....PP.Vb 9\&  printf( $file_count == 1 ?\&    ( $directory_count == 1 ?\&     "Your query matched %g file in %g directory." :\&     "Your query matched %g file in %g directories." ) :\&    ( $directory_count == 1 ?\&     "Your query matched %g files in %g directory." :\&     "Your query matched %g files in %g directories." ),\&   $file_count, $directory_count,\&  );.Ve.PPis first off that it's not well abstracted \*(-- these ways of testingfor grammatical number (as in the expressions like \f(CW\*(C`foo == 1 ?singular_form : plural_form\*(C'\fR) should be abstracted to each languagemodule, since how you get grammatical number is language-specific..PPBut second off, it's not isomorphic \*(-- the \*(L"solution\*(R" (i.e., thephrasebook entries) for Chinese maps from these four English phrases tothe one Chinese phrase that fits for all of them.  In other words, theinformal solution would be \*(L"The way to say what you want in Chinese iswith the one phrase 'For your question, in Y directories you wouldfind X files'\*(R" \*(-- and so the implemented solution should be,isomorphically, just a straightforward way to spit out that onephrase, with numerals properly interpolated.  It shouldn't have to mapfrom the complexity of other languages to the simplicity of this one..Sh "Buzzword: Inheritance".IX Subsection "Buzzword: Inheritance"There's a great deal of reuse possible for sharing of phrases betweenmodules for related dialects, or for sharing of auxiliary functionsbetween related languages.  (By \*(L"auxiliary functions\*(R", I meanfunctions that don't produce phrase-text, but which, say, return ananswer to \*(L"does this number require a plural noun after it?\*(R".  Suchauxiliary functions would be used in the internal logic of functionsthat actually do produce phrase-text.).PPIn the case of sharing phrases, consider that you have an interfacealready localized for American English (probably by having beenwritten with that as the native locale, but that's incidental).Localizing it for \s-1UK\s0 English should, in practical terms, be just amatter of running it past a British person with the instructions toindicate what few phrases would benefit from a change in spelling orpossibly minor rewording.  In that case, you should be able to put inthe \s-1UK\s0 English localization module \fIonly\fR those phrases that areUK-specific, and for all the rest, \fIinherit\fR from the AmericanEnglish module.  (And I expect this same situation would apply withBrazilian and Continental Portugese, possbily with some \fIvery\fRclosely related languages like Czech and Slovak, and possibly with theslightly different \*(L"versions\*(R" of written Mandarin Chinese, as I hear exist inTaiwan and mainland China.).PPAs to sharing of auxiliary functions, consider the problem of Russiannumbers from the beginning of this article; obviously, you'd want towrite only once the hairy code that, given a numeric value, wouldreturn some specification of which case and number a given quanitifiednoun should use.  But suppose that you discover, while localizing aninterface for, say, Ukranian (a Slavic language related to Russian,spoken by several million people, many of whom would be relieved tofind that your Web site's or software's interface is available intheir language), that the rules in Ukranian are the same as in Russianfor quantification, and probably for many other grammatical functions.While there may well be no phrases in common between Russian andUkranian, you could still choose to have the Ukranian module inheritfrom the Russian module, just for the sake of inheriting all thevarious grammatical methods.  Or, probably better organizationally,you could move those functions to a module called \f(CW\*(C`_E_Slavic\*(C'\fR orsomething, which Russian and Ukranian could inherit useful functionsfrom, but which would (presumably) provide no lexicon..Sh "Buzzword: Concision".IX Subsection "Buzzword: Concision"Okay, concision isn't a buzzword.  But it should be, so I decree thatas a new buzzword, \*(L"concision\*(R" means that simple common things shouldbe expressible in very few lines (or maybe even just a few characters)of code \*(-- call it a special case of \*(L"making simple things easy and

⌨️ 快捷键说明

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