dslinux/user/perl/lib/Locale/Maketext ChangeLog Guts.pm GutsLoader.pm README TPJ13.pod

cayenne dslinux_cayenne at user.in-berlin.de
Tue Dec 5 05:27:13 CET 2006


Update of /cvsroot/dslinux/dslinux/user/perl/lib/Locale/Maketext
In directory antilope:/tmp/cvs-serv7729/lib/Locale/Maketext

Added Files:
	ChangeLog Guts.pm GutsLoader.pm README TPJ13.pod 
Log Message:
Adding fresh perl source to HEAD to branch from

--- NEW FILE: TPJ13.pod ---

# This document contains text in Perl "POD" format.
# Use a POD viewer like perldoc or perlman to render it.

# This corrects some typoes in the previous release.

=head1 NAME

Locale::Maketext::TPJ13 -- article about software localization

=head1 SYNOPSIS

  # This an article, not a module.

=head1 DESCRIPTION

The following article by Sean M. Burke and Jordan Lachler
first appeared in I<The Perl
Journal> #13 and is copyright 1999 The Perl Journal. It appears
courtesy of Jon Orwant and The Perl Journal.  This document may be
distributed under the same terms as Perl itself.

=head1 Localization and Perl: gettext breaks, Maketext fixes

by Sean M. Burke and Jordan Lachler

This article points out cases where gettext (a common system for
localizing software interfaces -- i.e., making them work in the user's
language of choice) fails because of basic differences between human
languages.  This article then describes Maketext, a new system capable
of correctly treating these differences.

=head2 A Localization Horror Story: It Could Happen To You

=over

"There are a number of languages spoken by human beings in this
world."

-- Harald Tveit Alvestrand, in RFC 1766, "Tags for the
Identification of Languages"

=back

Imagine that your task for the day is to localize a piece of software
-- and luckily for you, the only output the program emits is two
messages, like this:

  I scanned 12 directories.

  Your query matched 10 files in 4 directories.

So how hard could that be?  You look at the code that
produces the first item, and it reads:

  printf("I scanned %g directories.",
         $directory_count);

You think about that, and realize that it doesn't even work right for
English, as it can produce this output:

  I scanned 1 directories.

So you rewrite it to read:

  printf("I scanned %g %s.",
         $directory_count,
         $directory_count == 1 ?
           "directory" : "directories",
  );

...which does the Right Thing.  (In case you don't recall, "%g" is for
locale-specific number interpolation, and "%s" is for string
interpolation.)

But you still have to localize it for all the languages you're
producing this software for, so you pull Locale::gettext off of CPAN
so you can access the C<gettext> C functions you've heard are standard
for localization tasks.

And you write:

  printf(gettext("I scanned %g %s."),
         $dir_scan_count,
         $dir_scan_count == 1 ?
           gettext("directory") : gettext("directories"),
  );

But you then read in the gettext manual (Drepper, Miller, and Pinard 1995)
that this is not a good idea, since how a single word like "directory"
or "directories" is translated may depend on context -- and this is
true, since in a case language like German or Russian, you'd may need
these words with a different case ending in the first instance (where the
word is the object of a verb) than in the second instance, which you haven't even
gotten to yet (where the word is the object of a preposition, "in %g
directories") -- assuming these keep the same syntax when translated
into those languages.

So, on the advice of the gettext manual, you rewrite:

  printf( $dir_scan_count == 1 ?
           gettext("I scanned %g directory.") :
           gettext("I scanned %g directories."),
         $dir_scan_count );

So, you email your various translators (the boss decides that the
languages du jour are Chinese, Arabic, Russian, and Italian, so you
have one translator for each), asking for translations for "I scanned
%g directory." and "I scanned %g directories.".  When they reply,
you'll put that in the lexicons for gettext to use when it localizes
your software, so that when the user is running under the "zh"
(Chinese) locale, gettext("I scanned %g directory.") will return the
appropriate Chinese text, with a "%g" in there where printf can then
interpolate $dir_scan.

Your Chinese translator emails right back -- he says both of these
phrases translate to the same thing in Chinese, because, in linguistic
jargon, Chinese "doesn't have number as a grammatical category" --
whereas English does.  That is, English has grammatical rules that
refer to "number", i.e., whether something is grammatically singular
or plural; and one of these rules is the one that forces nouns to take
a plural suffix (generally "s") when in a plural context, as they are when
they follow a number other than "one" (including, oddly enough, "zero").
Chinese has no such rules, and so has just the one phrase where English
has two.  But, no problem, you can have this one Chinese phrase appear
as the translation for the two English phrases in the "zh" gettext
lexicon for your program.

Emboldened by this, you dive into the second phrase that your software
needs to output: "Your query matched 10 files in 4 directories.".  You notice
that if you want to treat phrases as indivisible, as the gettext
manual wisely advises, you need four cases now, instead of two, to
cover the permutations of singular and plural on the two items,
$dir_count and $file_count.  So you try this:

  printf( $file_count == 1 ?
    ( $directory_count == 1 ?
     gettext("Your query matched %g file in %g directory.") :
     gettext("Your query matched %g file in %g directories.") ) :
    ( $directory_count == 1 ?
     gettext("Your query matched %g files in %g directory.") :
     gettext("Your query matched %g files in %g directories.") ),
   $file_count, $directory_count,
  );

(The case of "1 file in 2 [or more] directories" could, I suppose,
occur in the case of symlinking or something of the sort.)

It occurs to you that this is not the prettiest code you've ever
written, but this seems the way to go.  You mail off to the
translators asking for translations for these four cases.  The
Chinese guy replies with the one phrase that these all translate to in
Chinese, and that phrase has two "%g"s in it, as it should -- but
there's a problem.  He translates it word-for-word back: "In %g
directories contains %g files match your query."  The %g
slots are in an order reverse to what they are in English.  You wonder
how you'll get gettext to handle that.

But you put it aside for the moment, and optimistically hope that the
other translators won't have this problem, and that their languages
will be better behaved -- i.e., that they will be just like English.

But the Arabic translator is the next to write back.  First off, your
code for "I scanned %g directory." or "I scanned %g directories."
assumes there's only singular or plural.  But, to use linguistic
jargon again, Arabic has grammatical number, like English (but unlike
Chinese), but it's a three-term category: singular, dual, and plural.
In other words, the way you say "directory" depends on whether there's
one directory, or I<two> of them, or I<more than two> of them.  Your
test of C<($directory == 1)> no longer does the job.  And it means
that where English's grammatical category of number necessitates
only the two permutations of the first sentence based on "directory
[singular]" and "directories [plural]", Arabic has three -- and,
worse, in the second sentence ("Your query matched %g file in %g
directory."), where English has four, Arabic has nine.  You sense
an unwelcome, exponential trend taking shape.

Your Italian translator emails you back and says that "I searched 0
directories" (a possible English output of your program) is stilted,
and if you think that's fine English, that's your problem, but that
I<just will not do> in the language of Dante.  He insists that where
$directory_count is 0, your program should produce the Italian text
for "I I<didn't> scan I<any> directories.".  And ditto for "I didn't
match any files in any directories", although he says the last part
about "in any directories" should probably just be left off.

You wonder how you'll get gettext to handle this; to accomodate the
ways Arabic, Chinese, and Italian deal with numbers in just these few
very simple phrases, you need to write code that will ask gettext for
different queries depending on whether the numerical values in
question are 1, 2, more than 2, or in some cases 0, and you still haven't
figured out the problem with the different word order in Chinese.

Then your Russian translator calls on the phone, to I<personally> tell
you the bad news about how really unpleasant your life is about to
become:

Russian, like German or Latin, is an inflectional language; that is, nouns
and adjectives have to take endings that depend on their case
(i.e., nominative, accusative, genitive, etc...) -- which is roughly a matter of
what 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 the
declension class of the noun.  But unlike with most other inflected languages,
putting a number-phrase (like "ten" or "forty-three", or their Arabic
numeral equivalents) in front of noun in Russian can change the case and
number that noun is, and therefore the endings you have to put on it.

He elaborates:  In "I scanned %g directories", you'd I<expect>
"directories" to be in the accusative case (since it is the direct
object in the sentnce) and the plural number,
except where $directory_count is 1, then you'd expect the singular, of
course.  Just like Latin or German.  I<But!>  Where $directory_count %
10 is 1 ("%" for modulo, remember), assuming $directory count is an
integer, and except where $directory_count % 100 is 11, "directories"
is forced to become grammatically singular, which means it gets the
ending for the accusative singular...  You begin to visualize the code
it'd take to test for the problem so far, I<and still work for Chinese
and Arabic and Italian>, and how many gettext items that'd take, but
he keeps going...  But where $directory_count % 10 is 2, 3, or 4
(except where $directory_count % 100 is 12, 13, or 14), the word for
"directories" is forced to be genitive singular -- which means another
ending... The room begins to spin around you, slowly at first...  But
with I<all other> integer values, since "directory" is an inanimate
noun, when preceded by a number and in the nominative or accusative
cases (as it is here, just your luck!), it does stay plural, but it is
forced into the genitive case -- yet another ending...  And
you never hear him get to the part about how you're going to run into
similar (but maybe subtly different) problems with other Slavic
languages like Polish, because the floor comes up to meet you, and you
fade into unconsciousness.


The above cautionary tale relates how an attempt at localization can
lead from programmer consternation, to program obfuscation, to a need
for sedation.  But careful evaluation shows that your choice of tools
merely needed further consideration.

=head2 The Linguistic View

=over

"It is more complicated than you think." 

-- The Eighth Networking Truth, from RFC 1925

=back

The field of Linguistics has expended a great deal of effort over the
past century trying to find grammatical patterns which hold across
languages; it's been a constant process
of 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 of
languages, and sometimes nearly every language in the world except
English.  Broad statistical trends are evident in what the "average
language" is like as far as what its rules can look like, must look
like, and cannot look like.  But the "average language" is just as
unreal a concept as the "average person" -- it runs up against the
fact no language (or person) is, in fact, average.  The wisdom of past
experience leads us to believe that any given language can do whatever
it wants, in any order, with appeal to any kind of grammatical
categories wants -- case, number, tense, real or metaphoric
characteristics of the things that words refer to, arbitrary or
predictable classifications of words based on what endings or prefixes
they can take, degree or means of certainty about the truth of
statements expressed, and so on, ad infinitum.

Mercifully, most localization tasks are a matter of finding ways to
translate whole phrases, generally sentences, where the context is
relatively set, and where the only variation in content is I<usually>
in a number being expressed -- as in the example sentences above.
Translating specific, fully-formed sentences is, in practice, fairly
foolproof -- which is good, because that's what's in the phrasebooks
that so many tourists rely on.  Now, a given phrase (whether in a
phrasebook or in a gettext lexicon) in one language I<might> have a
greater or lesser applicability than that phrase's translation into
another language -- for example, strictly speaking, in Arabic, the
"your" in "Your query matched..." would take a different form
depending on whether the user is male or female; so the Arabic
translation "your[feminine] query" is applicable in fewer cases than
the corresponding English phrase, which doesn't distinguish the user's
gender.  (In practice, it's not feasable to have a program know the
user's gender, so the masculine "you" in Arabic is usually used, by
default.)

But in general, such surprises are rare when entire sentences are
being translated, especially when the functional context is restricted
to that of a computer interacting with a user either to convey a fact
or to prompt for a piece of information.  So, for purposes of
localization, translation by phrase (generally by sentence) is both the
simplest and the least problematic.

=head2 Breaking gettext

=over

"It Has To Work."

-- First Networking Truth, RFC 1925

=back

Consider that sentences in a tourist phrasebook are of two types: ones
like "How do I get to the marketplace?" that don't have any blanks to
fill in, and ones like "How much do these ___ cost?", where there's
one or more blanks to fill in (and these are usually linked to a
list of words that you can put in that blank: "fish", "potatoes",
"tomatoes", etc.)  The ones with no blanks are no problem, but the
fill-in-the-blank ones may not be really straightforward. If it's a
Swahili phrasebook, for example, the authors probably didn't bother to
tell you the complicated ways that the verb "cost" changes its
inflectional prefix depending on the noun you're putting in the blank.
The trader in the marketplace will still understand what you're saying if
you say "how much do these potatoes cost?" with the wrong
inflectional prefix on "cost".  After all, I<you> can't speak proper Swahili,
I<you're> just a tourist.  But while tourists can be stupid, computers
are supposed to be smart; the computer should be able to fill in the
blank, and still have the results be grammatical.

In other words, a phrasebook entry takes some values as parameters
(the things that you fill in the blank or blanks), and provides a value
based on these parameters, where the way you get that final value from
the given values can, properly speaking, involve an arbitrarily
complex series of operations.  (In the case of Chinese, it'd be not at
all complex, at least in cases like the examples at the beginning of
this article; whereas in the case of Russian it'd be a rather complex
series of operations.  And in some languages, the
complexity could be spread around differently: while the act of
putting a number-expression in front of a noun phrase might not be
complex by itself, it may change how you have to, for example, inflect
a verb elsewhere in the sentence.  This is what in syntax is called
"long-distance dependencies".)

This talk of parameters and arbitrary complexity is just another way
to say that an entry in a phrasebook is what in a programming language
would be called a "function".  Just so you don't miss it, this is the
crux of this article: I<A phrase is a function; a phrasebook is a
bunch of functions.>

The reason that using gettext runs into walls (as in the above
second-person horror story) is that you're trying to use a string (or
worse, a choice among a bunch of strings) to do what you really need a
function for -- which is futile.  Preforming (s)printf interpolation
on the strings which you get back from gettext does allow you to do I<some>
common things passably well... sometimes... sort of; but, to paraphrase
what some people say about C<csh> script programming, "it fools you
into thinking you can use it for real things, but you can't, and you
don't discover this until you've already spent too much time trying,
and by then it's too late."

=head2 Replacing gettext

So, what needs to replace gettext is a system that supports lexicons
of functions instead of lexicons of strings.  An entry in a lexicon
from such a system should I<not> look like this:

  "J'ai trouv\xE9 %g fichiers dans %g r\xE9pertoires"

[\xE9 is e-acute in Latin-1.  Some pod renderers would
scream if I used the actual character here. -- SB]

but instead like this, bearing in mind that this is just a first stab:

  sub I_found_X1_files_in_X2_directories {
    my( $files, $dirs ) = @_[0,1];
    $files = sprintf("%g %s", $files,
      $files == 1 ? 'fichier' : 'fichiers');
    $dirs = sprintf("%g %s", $dirs,
      $dirs == 1 ? "r\xE9pertoire" : "r\xE9pertoires");
    return "J'ai trouv\xE9 $files dans $dirs.";
  }

Now, there's no particularly obvious way to store anything but strings
in a gettext lexicon; so it looks like we just have to start over and
make something better, from scratch.  I call my shot at a
gettext-replacement system "Maketext", or, in CPAN terms,
Locale::Maketext.

When designing Maketext, I chose to plan its main features in terms of
"buzzword compliance".  And here are the buzzwords:

=head2 Buzzwords: Abstraction and Encapsulation

The complexity of the language you're trying to output a phrase in is
entirely abstracted inside (and encapsulated within) the Maketext module
for that interface.  When you call:

  print $lang->maketext("You have [quant,_1,piece] of new mail.",
                       scalar(@messages));

you don't know (and in fact can't easily find out) whether this will
involve lots of figuring, as in Russian (if $lang is a handle to the
Russian module), or relatively little, as in Chinese.  That kind of
abstraction and encapsulation may encourage other pleasant buzzwords
like modularization and stratification, depending on what design
decisions you make.

=head2 Buzzword: Isomorphism

"Isomorphism" means "having the same structure or form"; in discussions
of program design, the word takes on the special, specific meaning that
your implementation of a solution to a problem I<has the same
structure> as, say, an informal verbal description of the solution, or
maybe of the problem itself.  Isomorphism is, all things considered,
a good thing -- it's what problem-solving (and solution-implementing)
should look like.

What's wrong the with gettext-using code like this...

  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,
  );

is first off that it's not well abstracted -- these ways of testing
for grammatical number (as in the expressions like C<foo == 1 ?
singular_form : plural_form>) should be abstracted to each language
module, since how you get grammatical number is language-specific.

But second off, it's not isomorphic -- the "solution" (i.e., the
phrasebook entries) for Chinese maps from these four English phrases to
the one Chinese phrase that fits for all of them.  In other words, the
informal solution would be "The way to say what you want in Chinese is
with the one phrase 'For your question, in Y directories you would
find X files'" -- and so the implemented solution should be,
isomorphically, just a straightforward way to spit out that one
phrase, with numerals properly interpolated.  It shouldn't have to map
from the complexity of other languages to the simplicity of this one.

=head2 Buzzword: Inheritance

There's a great deal of reuse possible for sharing of phrases between
modules for related dialects, or for sharing of auxiliary functions
between related languages.  (By "auxiliary functions", I mean
functions that don't produce phrase-text, but which, say, return an
answer to "does this number require a plural noun after it?".  Such
auxiliary functions would be used in the internal logic of functions
that actually do produce phrase-text.)

In the case of sharing phrases, consider that you have an interface
already localized for American English (probably by having been
written with that as the native locale, but that's incidental).
Localizing it for UK English should, in practical terms, be just a
matter of running it past a British person with the instructions to
indicate what few phrases would benefit from a change in spelling or
possibly minor rewording.  In that case, you should be able to put in
the UK English localization module I<only> those phrases that are
UK-specific, and for all the rest, I<inherit> from the American
English module.  (And I expect this same situation would apply with
Brazilian and Continental Portugese, possbily with some I<very>
closely related languages like Czech and Slovak, and possibly with the
slightly different "versions" of written Mandarin Chinese, as I hear exist in
Taiwan and mainland China.)

As to sharing of auxiliary functions, consider the problem of Russian
numbers from the beginning of this article; obviously, you'd want to
write only once the hairy code that, given a numeric value, would
return some specification of which case and number a given quanitified
noun should use.  But suppose that you discover, while localizing an
interface for, say, Ukranian (a Slavic language related to Russian,
spoken by several million people, many of whom would be relieved to
find that your Web site's or software's interface is available in
their language), that the rules in Ukranian are the same as in Russian
for quantification, and probably for many other grammatical functions.
While there may well be no phrases in common between Russian and
Ukranian, you could still choose to have the Ukranian module inherit
from the Russian module, just for the sake of inheriting all the
various grammatical methods.  Or, probably better organizationally,
you could move those functions to a module called C<_E_Slavic> or
something, which Russian and Ukranian could inherit useful functions
from, but which would (presumably) provide no lexicon.

=head2 Buzzword: Concision

Okay, concision isn't a buzzword.  But it should be, so I decree that
as a new buzzword, "concision" means that simple common things should
be expressible in very few lines (or maybe even just a few characters)
of code -- call it a special case of "making simple things easy and
hard things possible", and see also the role it played in the
MIDI::Simple language, discussed elsewhere in this issue [TPJ#13].

Consider our first stab at an entry in our "phrasebook of functions":

  sub I_found_X1_files_in_X2_directories {
    my( $files, $dirs ) = @_[0,1];
    $files = sprintf("%g %s", $files,
      $files == 1 ? 'fichier' : 'fichiers');
    $dirs = sprintf("%g %s", $dirs,
      $dirs == 1 ? "r\xE9pertoire" : "r\xE9pertoires");
    return "J'ai trouv\xE9 $files dans $dirs.";
  }

You may sense that a lexicon (to use a non-committal catch-all term for a
collection of things you know how to say, regardless of whether they're
phrases or words) consisting of functions I<expressed> as above would
make for rather long-winded and repetitive code -- even if you wisely
rewrote this to have quantification (as we call adding a number
expression to a noun phrase) be a function called like:

  sub I_found_X1_files_in_X2_directories {
    my( $files, $dirs ) = @_[0,1];
    $files = quant($files, "fichier");
    $dirs =  quant($dirs,  "r\xE9pertoire");
    return "J'ai trouv\xE9 $files dans $dirs.";
  }

And you may also sense that you do not want to bother your translators
with having to write Perl code -- you'd much rather that they spend
their I<very costly time> on just translation.  And this is to say
nothing of the near impossibility of finding a commercial translator
who would know even simple Perl.

In a first-hack implementation of Maketext, each language-module's
lexicon looked like this:

 %Lexicon = (
   "I found %g files in %g directories"
   => sub {
      my( $files, $dirs ) = @_[0,1];
      $files = quant($files, "fichier");
      $dirs =  quant($dirs,  "r\xE9pertoire");
      return "J'ai trouv\xE9 $files dans $dirs.";
    },
  ... and so on with other phrase => sub mappings ...
 );

but I immediately went looking for some more concise way to basically
denote the same phrase-function -- a way that would also serve to
concisely denote I<most> phrase-functions in the lexicon for I<most>
languages.  After much time and even some actual thought, I decided on
this system:

* Where a value in a %Lexicon hash is a contentful string instead of
an anonymous sub (or, conceivably, a coderef), it would be interpreted
as a sort of shorthand expression of what the sub does.  When accessed
for 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 the
original string in that lexicon.  (That way, the work of parsing and
evaling the shorthand form for a given phrase is done no more than
once per session.)

* Calls to C<maketext> (as Maketext's main function is called) happen
thru a "language session handle", notionally very much like an IO
handle, in that you open one at the start of the session, and use it
for "sending signals" to an object in order to have it return the text
you want.

So, this:

  $lang->maketext("You have [quant,_1,piece] of new mail.",
                 scalar(@messages));

basically means this: look in the lexicon for $lang (which may inherit
from any number of other lexicons), and find the function that we
happen to associate with the string "You have [quant,_1,piece] of new
mail" (which is, and should be, a functioning "shorthand" for this
function in the native locale -- English in this case).  If you find
such a function, call it with $lang as its first parameter (as if it
were a method), and then a copy of scalar(@messages) as its second,
and then return that value.  If that function was found, but was in
string shorthand instead of being a fully specified function, parse it
and make it into a function before calling it the first time.

* The shorthand uses code in brackets to indicate method calls that
should be performed.  A full explanation is not in order here, but a
few examples will suffice:

  "You have [quant,_1,piece] of new mail."

The above code is shorthand for, and will be interpreted as,
this:

  sub {
    my $handle = $_[0];
    my(@params) = @_;
    return join '',
      "You have ",
      $handle->quant($params[1], 'piece'),
      "of new mail.";
  }

where "quant" is the name of a method you're using to quantify the
noun "piece" with the number $params[0].

A string with no brackety calls, like this:

  "Your search expression was malformed."

is somewhat of a degerate case, and just gets turned into:

  sub { return "Your search expression was malformed." }

However, not everything you can write in Perl code can be written in
the above shorthand system -- not by a long shot.  For example, consider
the Italian translator from the beginning of this article, who wanted
the Italian for "I didn't find any files" as a special case, instead
of "I found 0 files".  That couldn't be specified (at least not easily
or simply) in our shorthand system, and it would have to be written
out in full, like this:

  sub {  # pretend the English strings are in Italian
    my($handle, $files, $dirs) = @_[0,1,2];
    return "I didn't find any files" unless $files;
    return join '',
      "I found ",
      $handle->quant($files, 'file'),
      " in ",
      $handle->quant($dirs,  'directory'),
      ".";
  }

Next to a lexicon full of shorthand code, that sort of sticks out like a
sore thumb -- but this I<is> a special case, after all; and at least
it's possible, if not as concise as usual.

As to how you'd implement the Russian example from the beginning of
the article, well, There's More Than One Way To Do It, but it could be
something like this (using English words for Russian, just so you know
what's going on):

  "I [quant,_1,directory,accusative] scanned."

This shifts the burden of complexity off to the quant method.  That
method's parameters are: the numeric value it's going to use to
quantify something; the Russian word it's going to quantify; and the
parameter "accusative", which you're using to mean that this
sentence's syntax wants a noun in the accusative case there, although
that quantification method may have to overrule, for grammatical
reasons you may recall from the beginning of this article.

Now, the Russian quant method here is responsible not only for
implementing the strange logic necessary for figuring out how Russian
number-phrases impose case and number on their noun-phrases, but also
for inflecting the Russian word for "directory".  How that inflection
is to be carried out is no small issue, and among the solutions I've
seen, some (like variations on a simple lookup in a hash where all
possible forms are provided for all necessary words) are
straightforward but I<can> become cumbersome when you need to inflect
more than a few dozen words; and other solutions (like using
algorithms to model the inflections, storing only root forms and
irregularities) I<can> involve more overhead than is justifiable for
all but the largest lexicons.

Mercifully, this design decision becomes crucial only in the hairiest
of inflected languages, of which Russian is by no means the I<worst> case
scenario, but is worse than most.  Most languages have simpler
inflection systems; for example, in English or Swahili, there are
generally no more than two possible inflected forms for a given noun
("error/errors"; "kosa/makosa"), and the
rules for producing these forms are fairly simple -- or at least,
simple rules can be formulated that work for most words, and you can
then treat the exceptions as just "irregular", at least relative to
your ad hoc rules.  A simpler inflection system (simpler rules, fewer
forms) means that design decisions are less crucial to maintaining
sanity, whereas the same decisions could incur
overhead-versus-scalability problems in languages like Russian.  It
may I<also> be likely that code (possibly in Perl, as with
Lingua::EN::Inflect, for English nouns) has already
been written for the language in question, whether simple or complex.

Moreover, a third possibility may even be simpler than anything
discussed above: "Just require that all possible (or at least
applicable) forms be provided in the call to the given language's quant
method, as in:"

  "I found [quant,_1,file,files]."

That way, quant just has to chose which form it needs, without having
to look up or generate anything.  While possibly not optimal for
Russian, this should work well for most other languages, where
quantification is not as complicated an operation.

=head2 The Devil in the Details

There's plenty more to Maketext than described above -- for example,
there's the details of how language tags ("en-US", "i-pwn", "fi",
etc.) or locale IDs ("en_US") interact with actual module naming
("BogoQuery/Locale/en_us.pm"), and what magic can ensue; there's the
details of how to record (and possibly negotiate) what character
encoding Maketext will return text in (UTF8? Latin-1? KOI8?).  There's
the interesting fact that Maketext is for localization, but nowhere
actually has a "C<use locale;>" anywhere in it.  For the curious,
there's the somewhat frightening details of how I actually
implement something like data inheritance so that searches across
modules' %Lexicon hashes can parallel how Perl implements method
inheritance.

And, most importantly, there's all the practical details of how to
actually go about deriving from Maketext so you can use it for your
interfaces, and the various tools and conventions for starting out and
maintaining individual language modules.

That is all covered in the documentation for Locale::Maketext and the
modules that come with it, available in CPAN.  After having read this
article, which covers the why's of Maketext, the documentation,
which covers the how's of it, should be quite straightfoward.

=head2 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, and
really can't work without a Perl interpreter (although I suppose
something like it could be written for C).  Accidents of history (and
not necessarily lucky ones) have made C++ the most common language for
the implementation of applications like word processors, Web browsers,
and even many in-house applications like custom query systems.  Current
conditions make it somewhat unlikely that the next one of any of these
kinds of applications will be written in Perl, albeit clearly more for
reasons of custom and inertia than out of consideration of what is the
right tool for the job.

However, other accidents of history have made Perl a well-accepted
language for design of server-side programs (generally in CGI form)
for Web site interfaces.  Localization of static pages in Web sites is
trivial, feasable either with simple language-negotiation features in
servers like Apache, or with some kind of server-side inclusions of
language-appropriate text into layout templates.  However, I think
that the localization of Perl-based search systems (or other kinds of
dynamic content) in Web sites, be they public or access-restricted,
is where Maketext will see the greatest use.

I presume that it would be only the exceptional Web site that gets
localized for English I<and> Chinese I<and> Italian I<and> Arabic
I<and> Russian, to recall the languages from the beginning of this
article -- to say nothing of German, Spanish, French, Japanese,
Finnish, and Hindi, to name a few languages that benefit from large
numbers of programmers or Web viewers or both.

However, the ever-increasing internationalization of the Web (whether
measured in terms of amount of content, of numbers of content writers
or programmers, or of size of content audiences) makes it increasingly
likely that the interface to the average Web-based dynamic content
service will be localized for two or maybe three languages.  It is my
hope that Maketext will make that task as simple as possible, and will
remove previous barriers to localization for languages dissimilar to
English.

 __END__

Sean M. Burke (sburkeE<64>cpan.org) has a Master's in linguistics
from Northwestern University; he specializes in language technology.
Jordan Lachler (lachlerE<64>unm.edu) is a PhD student in the Department of
Linguistics at the University of New Mexico; he specializes in
morphology and pedagogy of North American native languages.

=head2 References

Alvestrand, Harald Tveit.  1995.  I<RFC 1766: Tags for the
Identification of Languages.>
C<ftp://ftp.isi.edu/in-notes/rfc1766.txt>
[Now see RFC 3066.]

Callon, Ross, editor.  1996.  I<RFC 1925: The Twelve
Networking Truths.>
C<ftp://ftp.isi.edu/in-notes/rfc1925.txt>

Drepper, Ulrich, Peter Miller,
and FranE<ccedil>ois Pinard.  1995-2001.  GNU
C<gettext>.  Available in C<ftp://prep.ai.mit.edu/pub/gnu/>, with
extensive docs in the distribution tarball.  [Since
I wrote this article in 1998, I now see that the
gettext docs are now trying more to come to terms with
plurality.  Whether useful conclusions have come from it
is another question altogether. -- SMB, May 2001]

Forbes, Nevill.  1964.  I<Russian Grammar.>  Third Edition, revised
by J. C. Dumbreck.  Oxford University Press.

=cut

#End


--- NEW FILE: README ---
README for Locale::Maketext
                                        Time-stamp: "2004-03-30 16:02:27 AST"

			   Locale::Maketext

Locale::Maketext is a base class providing a framework for
localization and inheritance-based lexicons, as described in my
article in The Perl Journal #13 (a corrected version of which appears
in this dist).


PREREQUISITES

This suite requires Perl 5.  It also requires a recent version
of I18N::LangTags.  MSWin users should also get Win32::Locale.
File::Findgrep is also useful example code, as is the rather
larger Apache::MP3 source (even if you don't run Apache).


INSTALLATION

You install Locale::Maketext, as you would install any Perl module
distribution, by running these commands:

   perl Makefile.PL
   make
   make test
   make install

If you want to install a private copy of Maketext in your home directory,
then you should try to produce the initial Makefile with something
like this command:

  perl Makefile.PL LIB=~/perl

See perldoc perlmodinstall for more information.


DOCUMENTATION

See the pod in Locale::Maketext and Locale::Maketext::TPJ13,
and see also File::Findgrep.


SUPPORT

Questions, bug reports, useful code bits, and suggestions for
Worms should be sent to me at sburke at cpan.org


AVAILABILITY

The latest version of Locale::Maketext is available from the
Comprehensive Perl Archive Network (CPAN).  Visit
<http://www.perl.com/CPAN/> to find a CPAN site near you.


COPYRIGHT

Copyright 1999-2004, Sean M. Burke <sburke at cpan.org>, all rights
reserved.  This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.


AUTHOR

Sean M. Burke <sburke at cpan.org>

--- NEW FILE: ChangeLog ---
Revision history for Perl suite Locale::Maketext
                                        Time-stamp: "2004-03-30 21:38:07 AST"

2004-03-30  Sean M. Burke  sburke at cpan.org
	* Release 1.09:

	* Moved the language-preference-detecting code into new module
	I18N::LangTags::Detect.
	
	Thanks to Autrijus Tang for catching some errors in the dist!


	
2004-01-19  Sean M. Burke  sburke at cpan.org
	
	* Release 1.08:
	
	* Corrected a one-line code bug in v1.07 that accidentally demoted
	all en-* tags in cases of lexicon-groups that had an en.pm but no
	en_*.pm.  Thanks to Robert Spier for spotting this.  Test added.
	So don't use v1.07!

	* Autrijus found some typoes in the TPJ article.  Fixed.1
	
2004-01-11  Sean M. Burke  sburke at cpan.org
	
	* Release 1.07:  Now uses a new and different rule for implicating
	superordinate language tags in accept-language lists.  Previously,
	superordinates were just tacked onto the, so "en-US, ja", turned
	into "en-US, ja, en".  However, this turned out to be suboptimal
	for many users of RT, a popular system using Maketext.  The new
	rule is that a tag implicates superordinate forms right after it,
	unless those tags are explicitly stated elsewhere in the
	accept-languages list.  So "en-US ja" becomes "en-US en ja".  If
	you want "en" to be really lower, you have to actually state it
	there: "en-US ja en" is left as-is.

	The 04super.t and 05super.t tests in t/ have many many examples of
	this, including some strange corner cases.

	(In implementing this change, I also refactored some code in
	Maketext.pm, for hopefully improved readability.  However,
	the above is the only actual change in behavior.)
	
2003-06-21  Sean M. Burke  sburke at cpan.org
	* Release 1.06:  Now has "use utf8" to make the things work
	happily.  Some fancy footwork is required to make this work under
	pre-utf8 perl versions.
	
2003-04-18  Sean M. Burke  sburke at cpan.org
	* Release 1.05:  Different Makefile.PL, same .pm code.
	
	Jesse Vincent, Hugo van der Sanden, and Jarkko Hietaniemi
	encourage me to add this to the makefile:
	 ($] < 5.008) ? () : ( INSTALLDIRS => 'perl'),
	so that when you install this on a recent version of perl (5.8 or
	later), the installation will overwrite the Maketext.pm in your
	core library directory.  Email me if this produces trouble for any
	of you folks out there, okay?
	
2003-04-02  Sean M. Burke  sburke at cpan.org
	* Release 1.04: Implementing proper HTTP "tag;q=rank" parsing for
	get_handle.  This should make all the difference for users/victims
	of the current version of Safari, which uses that syntax as well
	as inserts random languages with low q numbers.
	Thanks to Jesse Vincent and the whole RT junta for finding this.

	* Added more tests, now in t/

	* Lots of typo fixes to Maketext.pm.  Thanks to Evan A. Zacks for
	patient help in finding them all.	
	
2001-06-21  Sean M. Burke  sburke at cpan.org
	* Release 1.03: basically cosmetic tweaks to the docs and the
	test.pl.
	
2001-06-20  Sean M. Burke  sburke at cpan.org
	* Release 1.02:  EBCDIC-compatability changes courtesy of Peter
	Prymmer.  Added [*,...] as alias for [quant,...] and [#,...] as an
	alias for [numf,...].  Added some more things to test.pl
	
2001-05-25  Sean M. Burke  sburke at cpan.org
	* Release 1.01:  total rewrite.  Docs are massive now.
	Including TPJ13 article now.
	
2000-05-14  Sean M. Burke  sburke at cpan.org

	* Release 0.18:  only change, regrettably, is a better makefile,
	and it my email address has changed.

1999-03-15  Sean M. Burke  sburke at netadventure.net

	* Release 0.17:  Public alpha release
	Underdocumented.

--- NEW FILE: GutsLoader.pm ---

package Locale::Maketext::GutsLoader;
use strict;
sub zorp { return scalar @_ }

BEGIN {
  $Locale::Maketext::GutsLoader::GUTSPATH = __FILE__;
  *Locale::Maketext::DEBUG = sub () {0}
   unless defined &Locale::Maketext::DEBUG;
}

#
# This whole drama is so that we can load the utf8'd code
# in Locale::Maketext::Guts, but if that fails, snip the
# utf8 and then try THAT.
#

$Locale::Maketext::GUTSPATH = '';
Locale::Maketext::DEBUG and print "Requiring Locale::Maketext::Guts...\n";
eval 'require Locale::Maketext::Guts';

if($@) {
  my $path = $Locale::Maketext::GUTSPATH;

  die "Can't load Locale::Maketext::Guts\nAborting" unless $path;
  
  die "No readable file $Locale::Maketext::GutsLoader::GUTSPATH\nAborting"
   unless -e $path and -f _ and -r _;

  open(IN, $path) or die "Can't read-open $path\nAborting";
  
  my $source;
  { local $/;  $source = <IN>; }
  close(IN);
  unless( $source =~ s/\b(use utf8)/# $1/ ) {
    Locale::Maketext::DEBUG and
     print "I didn't see 'use utf8' in $path\n";
  }
  eval $source;
  die "Can't compile $path\n...The error I got was:\n$@\nAborting" if $@;
  Locale::Maketext::DEBUG and print "Non-utf8'd Locale::Maketext::Guts fine\n";
} else {
  Locale::Maketext::DEBUG and print "Loaded Locale::Maketext::Guts fine\n";
}

1;


--- NEW FILE: Guts.pm ---

package Locale::Maketext::Guts;
BEGIN { *zorp = sub { return scalar @_ } unless defined &zorp; }
 # Just so we're nice and define SOMETHING in "our" package.

package Locale::Maketext;
use strict;
use vars qw($USE_LITERALS $GUTSPATH);

BEGIN {
  $GUTSPATH = __FILE__;
  *DEBUG = sub () {0} unless defined &DEBUG;
}

use utf8;

sub _compile {
  # This big scary routine compiles an entry.
  # It returns either a coderef if there's brackety bits in this, or
  #  otherwise a ref to a scalar.
  
  my $target = ref($_[0]) || $_[0];
  
  my(@code);
  my(@c) = (''); # "chunks" -- scratch.
  my $call_count = 0;
  my $big_pile = '';
  {
    my $in_group = 0; # start out outside a group
    my($m, @params); # scratch
    
    while($_[1] =~  # Iterate over chunks.
     m<\G(
       [^\~\[\]]+  # non-~[] stuff
       |
       ~.       # ~[, ~], ~~, ~other
       |
       \[          # [ presumably opening a group
       |
       \]          # ] presumably closing a group
       |
       ~           # terminal ~ ?
       |
       $
     )>xgs
    ) {
      print "  \"$1\"\n" if DEBUG > 2;

      if($1 eq '[' or $1 eq '') {       # "[" or end
        # Whether this is "[" or end, force processing of any
        #  preceding literal.
        if($in_group) {
          if($1 eq '') {
            $target->_die_pointing($_[1], "Unterminated bracket group");
          } else {
            $target->_die_pointing($_[1], "You can't nest bracket groups");
          }
        } else {
          if($1 eq '') {
            print "   [end-string]\n" if DEBUG > 2;
          } else {
            $in_group = 1;
          }
          die "How come \@c is empty?? in <$_[1]>" unless @c; # sanity
          if(length $c[-1]) {
            # Now actually processing the preceding literal
            $big_pile .= $c[-1];
            if($USE_LITERALS and (
              (ord('A') == 65)
               ? $c[-1] !~ m<[^\x20-\x7E]>s
                  # ASCII very safe chars
               : $c[-1] !~ m/[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~\x07]/s
                  # EBCDIC very safe chars
            )) {
              # normal case -- all very safe chars
              $c[-1] =~ s/'/\\'/g;
              push @code, q{ '} . $c[-1] . "',\n";
              $c[-1] = ''; # reuse this slot
            } else {
              push @code, ' $c[' . $#c . "],\n";
              push @c, ''; # new chunk
            }
          }
           # else just ignore the empty string.
        }

      } elsif($1 eq ']') {  # "]"
        # close group -- go back in-band
        if($in_group) {
          $in_group = 0;
          
          print "   --Closing group [$c[-1]]\n" if DEBUG > 2;
          
          # And now process the group...
          
          if(!length($c[-1]) or $c[-1] =~ m/^\s+$/s) {
            DEBUG > 2 and print "   -- (Ignoring)\n";
            $c[-1] = ''; # reset out chink
            next;
          }
          
           #$c[-1] =~ s/^\s+//s;
           #$c[-1] =~ s/\s+$//s;
          ($m, at params) = split(",", $c[-1], -1);  # was /\s*,\s*/
          
          # A bit of a hack -- we've turned "~,"'s into DELs, so turn
          #  'em into real commas here.
          if (ord('A') == 65) { # ASCII, etc
            foreach($m, @params) { tr/\x7F/,/ } 
          } else {              # EBCDIC (1047, 0037, POSIX-BC)
            # Thanks to Peter Prymmer for the EBCDIC handling
            foreach($m, @params) { tr/\x07/,/ } 
          }
          
          # Special-case handling of some method names:
          if($m eq '_*' or $m =~ m<^_(-?\d+)$>s) {
            # Treat [_1,...] as [,_1,...], etc.
            unshift @params, $m;
            $m = '';
          } elsif($m eq '*') {
            $m = 'quant'; # "*" for "times": "4 cars" is 4 times "cars"
          } elsif($m eq '#') {
            $m = 'numf';  # "#" for "number": [#,_1] for "the number _1"
          }

          # Most common case: a simple, legal-looking method name
          if($m eq '') {
            # 0-length method name means to just interpolate:
            push @code, ' (';
          } elsif($m =~ m<^\w+(?:\:\:\w+)*$>s
            and $m !~ m<(?:^|\:)\d>s
             # exclude starting a (sub)package or symbol with a digit 
          ) {
            # Yes, it even supports the demented (and undocumented?)
            #  $obj->Foo::bar(...) syntax.
            $target->_die_pointing(
              $_[1], "Can't (yet?) use \"SUPER::\" in a bracket-group method",
              2 + length($c[-1])
            )
             if $m =~ m/^SUPER::/s;
              # Because for SUPER:: to work, we'd have to compile this into
              #  the right package, and that seems just not worth the bother,
              #  unless someone convinces me otherwise.
            
            push @code, ' $_[0]->' . $m . '(';
          } else {
            # TODO: implement something?  or just too icky to consider?
            $target->_die_pointing(
             $_[1],
             "Can't use \"$m\" as a method name in bracket group",
             2 + length($c[-1])
            );
          }
          
          pop @c; # we don't need that chunk anymore
          ++$call_count;
          
          foreach my $p (@params) {
            if($p eq '_*') {
              # Meaning: all parameters except $_[0]
              $code[-1] .= ' @_[1 .. $#_], ';
               # and yes, that does the right thing for all @_ < 3
            } elsif($p =~ m<^_(-?\d+)$>s) {
              # _3 meaning $_[3]
              $code[-1] .= '$_[' . (0 + $1) . '], ';
            } elsif($USE_LITERALS and (
              (ord('A') == 65)
               ? $p !~ m<[^\x20-\x7E]>s
                  # ASCII very safe chars
               : $p !~ m/[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~\x07]/s
                  # EBCDIC very safe chars            
            )) {
              # Normal case: a literal containing only safe characters
              $p =~ s/'/\\'/g;
              $code[-1] .= q{'} . $p . q{', };
            } else {
              # Stow it on the chunk-stack, and just refer to that.
              push @c, $p;
              push @code, ' $c[' . $#c . "], ";
            }
          }
          $code[-1] .= "),\n";

          push @c, '';
        } else {
          $target->_die_pointing($_[1], "Unbalanced ']'");
        }
        
      } elsif(substr($1,0,1) ne '~') {
        # it's stuff not containing "~" or "[" or "]"
        # i.e., a literal blob
        $c[-1] .= $1;
        
      } elsif($1 eq '~~') { # "~~"
        $c[-1] .= '~';
        
      } elsif($1 eq '~[') { # "~["
        $c[-1] .= '[';
        
      } elsif($1 eq '~]') { # "~]"
        $c[-1] .= ']';

      } elsif($1 eq '~,') { # "~,"
        if($in_group) {
          # This is a hack, based on the assumption that no-one will actually
          # want a DEL inside a bracket group.  Let's hope that's it's true.
          if (ord('A') == 65) { # ASCII etc
            $c[-1] .= "\x7F";
          } else {              # EBCDIC (cp 1047, 0037, POSIX-BC)
            $c[-1] .= "\x07";
          }
        } else {
          $c[-1] .= '~,';
        }
        
      } elsif($1 eq '~') { # possible only at string-end, it seems.
        $c[-1] .= '~';
        
      } else {
        # It's a "~X" where X is not a special character.
        # Consider it a literal ~ and X.
        $c[-1] .= $1;
      }
    }
  }

  if($call_count) {
    undef $big_pile; # Well, nevermind that.
  } else {
    # It's all literals!  Ahwell, that can happen.
    # So don't bother with the eval.  Return a SCALAR reference.
    return \$big_pile;
  }

  die "Last chunk isn't null??" if @c and length $c[-1]; # sanity
  print scalar(@c), " chunks under closure\n" if DEBUG;
  if(@code == 0) { # not possible?
    print "Empty code\n" if DEBUG;
    return \'';
  } elsif(@code > 1) { # most cases, presumably!
    unshift @code, "join '',\n";
  }
  unshift @code, "use strict; sub {\n";
  push @code, "}\n";

  print @code if DEBUG;
  my $sub = eval(join '', @code);
  die "$@ while evalling" . join('', @code) if $@; # Should be impossible.
  return $sub;
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub _die_pointing {
  # This is used by _compile to throw a fatal error
  my $target = shift; # class name
  # ...leaving $_[0] the error-causing text, and $_[1] the error message
  
  my $i = index($_[0], "\n");

  my $pointy;
  my $pos = pos($_[0]) - (defined($_[2]) ? $_[2] : 0) - 1;
  if($pos < 1) {
    $pointy = "^=== near there\n";
  } else { # we need to space over
    my $first_tab = index($_[0], "\t");
    if($pos > 2 and ( -1 == $first_tab  or  $first_tab > pos($_[0]))) {
      # No tabs, or the first tab is harmlessly after where we will point to,
      # AND we're far enough from the margin that we can draw a proper arrow.
      $pointy = ('=' x $pos) . "^ near there\n";
    } else {
      # tabs screw everything up!
      $pointy = substr($_[0],0,$pos);
      $pointy =~ tr/\t //cd;
       # make everything into whitespace, but preseving tabs
      $pointy .= "^=== near there\n";
    }
  }
  
  my $errmsg = "$_[1], in\:\n$_[0]";
  
  if($i == -1) {
    # No newline.
    $errmsg .= "\n" . $pointy;
  } elsif($i == (length($_[0]) - 1)  ) {
    # Already has a newline at end.
    $errmsg .= $pointy;
  } else {
    # don't bother with the pointy bit, I guess.
  }
  Carp::croak( "$errmsg via $target, as used" );
}

1;





More information about the dslinux-commit mailing list