Re: NSLocalizedString and plural forms
Re: NSLocalizedString and plural forms
- Subject: Re: NSLocalizedString and plural forms
- From: Ricky Sharp <email@hidden>
- Date: Thu, 18 Oct 2007 17:10:36 -0500
On Oct 18, 2007, at 4:46 PM, Andrey Subbotin wrote:
Hello all,
I've been looking eagerly through the Internationalization
Programming Topics at Apple's site and am kind of puzzled. The
problem I try to solve is providing a Russian language localization
for my project and I'm unable to find a way to properly localize
plural forms.
Say, we have the following three cases:
- 1 file has just been copied.
- 2 files have just been copied.
- 5 files have just been copied.
That is, in the English language there're only 2 forms. If all the
languages followed this pattern, then the code to localize it could
look like this:
if (x == 1) {
str = NSLocalizedString(@"%d file has just been copied.", nil);
} else {
str = NSLocalizedString(@"%d files have just been copied.", nil);
}
Then... in Russian there'd be more plural forms. The above three
lines would be:
- 1 fail skopirovalsya.
- 2 faila skopirovalis'.
- 5 failov skopirovalos'.
The cases like this make it impossible to simply use the above two
NSLocalizedString keys to retrieve the proper translation.
The gettext library can handle these things well by providing a way
to specify the number of plural forms in its dictionary files
(Please see the following page where it all is described in greater
details: http://www.gnu.org/software/libc/manual/html_node/Advanced-
gettext-functions.html ). But even if I can use the gettext library
in Objective-C apps, gettext is no way Cocoa.
The question is: How do I properly localize plural forms using the
Cocoa APIs? Is it at all possible?
I'd gladly appreciate any hints, etc. Thanks in advance.
You could take the list of all languages you're supporting (let's
assume for the sake of this e-mail as just English and Russian) and
first localize for the language that needs the most unique terms
(Russian in this case).
Your code would then need to work with that most specific case:
if (x == 1) str = NSLocalizedString("KEY_1",...);
else if (x == 2) str = NSLocalizedString("KEY_2",...);
else str = NSLocalizedString("KEY_3",...);
In your localized .strings for Russian, you'd then have:
"KEY_1" = "%d fail";
"KEY_2" = "%d faila";
"KEY_2" = "%d failov";
In your English .strings, you'd then have some redundancy:
"KEY_1" = "%d file"
"KEY_2" = "%d files";
"KEY_3" = "%d files";
I know that I would lean towards this approach if I ever localized my
product (which also currently has quite a bit of code like yours to
handle singular/plural cases).
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden