Re: Localizing User Data
Re: Localizing User Data
- Subject: Re: Localizing User Data
- From: email@hidden
- Date: Sat, 15 Apr 2006 14:05:48 -0700
On 平成 18/04/15, at 6:39, Matteo Manferdini wrote:
Il giorno 15/apr/06, alle ore 15:26, Uli Kusterer ha scritto:
Who says you can't ? You can pass *any* string to
NSLocalizedString(), whether it's from your data dictionary or
elsewhere.
But that's not really an optimal solution. What kind of data is
this?
In many cases it makes sense to just have a different version of
the file containing this data for each localisation. Unless there
is a lot of non-localised data in the file that would be
duplicated. Another alternative is to make your file format itself
localisable, or to have placeholders and replace these with
localised data if it's e.g. a template document.
I'm just guessing here, you're not telling us enough to go by.
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
The data are food, with names to localize, and recipes, that also
have preparation instructions that need to be translated.
I was already thinking about different versions of files containing
data, but I wondered if there is a better method.
Thank you for your advice.
Easy. I've already done this.
Have a to-many relation from your data objects called "localizations."
Each localization object both inherits from its parent (so it has all
the same keys as your un-localized model object).
Also add a simple language variable that can be a string as used in
ISO 639 language codes. You can get a list of these codes using NSLocale
and bind a pop-up menu to present a list of languages to whoever is
setting the language of a localized recipe etc...
If you want to match the language of a localized item then override
each method that corresponds to a key (name,food,recipe) and check
if there is a localized version of the same object in "localizations"
by calling the current language and see if there is one with its
language string
that matches what you want, and return that string..
e.g (typed into mail)
- (NSString *)recipe
{
NSString *thisRecipesLanguage = [self valueForKey:@"language"];
NSString *currentUsersLanguage = [[NSLocale currentLocale]
objectForKey:@"NSLocaleLanguageCode"];
if ([thisRecipesLanguage isEqualToString:currentUsersLanguage])
{
NSString *tmpValue = nil;
[self willAccessValueForKey:@"recipe"];
tmpValue = [self primitiveValueForKey:@"recipe"];
[self didAccessValueForKey:@"recipe"];
return tmpValue;
}
else //Not the same so we fetch the recipe with the right language
if it exists
{
//the method localizationWithLanguageCode could call a fetch
request or use your own sorting to get the item from "localizations"
to-many relationship
NSManagedObject *localizedRecipe = [self
localizationWithLanguageCode:currentUsersLanguage];
if (localizedRecipe)
{
//You could have a default value in your bindings to present a
string like "Not yet localized" if this returns nil
return [localizedRecipe valueForKey:@"recipe"];
}
else
{
NSString *tmpValue = nil;
[self willAccessValueForKey:@"recipe"];
tmpValue = [self primitiveValueForKey:@"recipe"];
[self didAccessValueForKey:@"recipe"];
return tmpValue;
}
}
//default if totally empty
return NSLocalizedString(@"No Value Set",@"An Empty Value");
}
This behavior could also be made more general, and therefore easier
to maintain if making a large app.
Ciao.
Andre
email@hidden
Andre
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden