Exceptional coding style

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 15 13:29:27 PST 2013


On Tue, Jan 15, 2013 at 09:23:22PM +0100, FG wrote:
> On 2013-01-15 17:00, H. S. Teoh wrote:
> >reading the format string alone says nothing about what meaning is
> >intended. What is found in a what? Also, it doesn't let you vary the
> >word order correctly. Nor does it let you perform inflection
> >correctly.
> >
> >For true ease of translation, you need:
> >
> >1) Logical naming of format arguments, e.g.:
> >
> >	"%{filename} is found in %{dirname}"
> 
> 
> I agree that named arguments are best -- then you don't require extra
> comments.  As for argument positioning, it already can be specified,
> for example "%1$s".

Yes, but argument positioning is easy to mix up when the format string
has a lot of arguments. Named arguments are the best. Self-documenting,
and also avoids careless mistakes (like mistyping %1$s when you mean
%2$s).


> >2) Some way to indicate the function of each word so that inflection
> >can be cone correctly, e.g.:
> >
> >	"%{filename.nominative} is found in %{dirname.locative}"
> >
> >or something like that, in languages where words change form
> >depending on function. There will, of course, need to be requisite
> >mechanisms for interpreting these word function identifiers.
> 
> Filename and dirname don't look like good examples - they don't
> change.  Well, this is a problem much deeper than providing simple
> translation, so let's drop this for now.

Yeah filename and dirname are not good examples.  A better example (for
Indo-European languages at least) is with verbs:

	writeln("The cat %s the dog.", verb);

In some languages, the form of "cat" and "dog" may need to change
depending on what the verb is. But then these cases are probably too
complex to capture in a format string in the first place, so perhaps the
point is moot.


> >3) Some way to indicate multiplicity:
> >
> >	"1 file was found" vs. "2 files were found"
> 
> This is already taken care of in every gettext system I know.
> The code would look something like this:
> 
> writefln(ngettext("%1$d file found in %2$s", "%1$d files found in %2$s", 2),
>     2, "folder X");
> 
> where ngettext takes 3 arguments: eng_singular, eng_plural, count.

What if there are two (or more) items in the string that have counts?


T

-- 
"Uhh, I'm still not here." -- KD, while "away" on ICQ.


More information about the Digitalmars-d mailing list