Core Data中多语言数据的良好做法 [英] Good practices for multiple language data in Core Data

查看:202
本文介绍了Core Data中多语言数据的良好做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个多语言coredata数据库在我的iphone应用程序。我可以为每种语言创建不同的数据库,但我希望在iphone sdk存在一个自动的方式来管理数据在不同的语言核心数据,如资源和字符串。



有人有一些提示?

解决方案

我已经做了类似于Shortseller的,但没有使用类别。



alt文字http://i40.tinypic.com/x3ryp.png p>

InternationalBook LocalizedBook 都是自定义管理对象



在执行 InternationalBook 时,为 title 添加了自定义访问器:

   - (NSString * )title {
[self willAccessValueForKey:@title];
NSString * locTitle = nil;
NSPredicate * predicate = [NSPredicate predicateWithFormat:@locale ==%@,[DataManager localeString]];
NSSet * localizedSet = [self.localizedBook filteredSetUsingPredicate:predicate];
if([localizedSet count]> 0){
locTitle = [[localizedSet valueForKey:@localizedTitle] anyObject];
}
[self didAccessValueForKey:@title];
return locTitle;
}

[DataManager localeString] 是一个类方法,返回用户的语言和国家代码: en_US fr_FR code> NSLocale 。



请参阅核心数据编程中的自定义属性和一对一关系存取器方法指导 willAccessValueForKey: didAccessValueForKey:的解释。



当填充数据时,我获取一个表示用户当前语言环境( [DataManager localeString] )的字符串,并将其与本地化书籍标题存储在一个新的 LocalizedBook 对象。每个 LocalizedBook 实例添加到 NSMutableSet ,表示一对多关系。

  NSMutableSet * bookLocalizations = [internationalBook mutableSetValueForKey:@localizedBook]; //国际书是InternationalBook的一个实例
//设置locale和localizedTitle的值
LocalizedBook * localizedBook =(LocalizedBook *)[NSEntityDescription insertnNewObjectEntityForName:@LocalizedBookinManagedObjectContext:self.bookMOC];
localizedBook.locale = [DataManager localeString];
localizedBook.localizedTitle = theLocalizedTitle; //假设已定义LococalTitle。
[bookLocalizations addObject:localizedBook];
[bookLocalizations setValue:localizedBook forKey:@localizedBook];

由于本地化标题存储在 LocalizedBook managed对象,你可以使 title 属性暂时,但如果你这样做,你不能使用 title 在谓词中。



这种方法的好处是,对多数关系的实现对任何消费者都是透明的。您只需请求 internationalBook.title ,自定义访问器会根据用户在场景后的语言环境返回相应的值。


i need a multilingual coredata db in my iphone app. I could create different database for each language but i hope that in iphone sdk exist an automatically way to manage data in different language core data like for resources and string.

Someone have some hints?

解决方案

I've done something similar to Shortseller, but without the use of categories.

alt text http://i40.tinypic.com/x3ryp.png

InternationalBook and LocalizedBook are both custom managed objects with a one-to-many relationship (one international book to many localised books).

In the implementation of InternationalBook, I've added a custom accessor for title:

- (NSString *)title {
    [self willAccessValueForKey:@"title"];
    NSString *locTitle = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"locale==%@", [DataManager localeString]];
    NSSet *localizedSet = [self.localizedBook filteredSetUsingPredicate:predicate];
    if ([localizedSet count] > 0) {
        locTitle = [[localizedSet valueForKey:@"localizedTitle"] anyObject];
    }
    [self didAccessValueForKey:@"title"];
    return locTitle;
}

[DataManager localeString] is a class method which returns the user's language and country code: en_US, fr_FR, etc. See documentation on NSLocale for details.

See the "Custom Attribute and To-One Relationship Accessor Methods" section of the Core Data Programming Guide for an explanation of willAccessValueForKey: and didAccessValueForKey:.

When populating the data, I grab a string representing the user's current locale ([DataManager localeString]), and store that along the with localised book title in a new LocalizedBook object. Each LocalizedBook instance is added to an NSMutableSet, which represents the one-to-many relationship.

NSMutableSet *bookLocalizations = [internationalBook mutableSetValueForKey:@"localizedBook"]; // internationalBook is an instance of InternationalBook
// set the values for locale and localizedTitle
LocalizedBook *localizedBook = (LocalizedBook *)[NSEntityDescription insertnNewObjectEntityForName:@"LocalizedBook" inManagedObjectContext:self.bookMOC];
localizedBook.locale = [DataManager localeString];
localizedBook.localizedTitle = theLocalizedTitle; // assume theLocalizedTitle has been defined.
[bookLocalizations addObject:localizedBook];
[bookLocalizations setValue:localizedBook forKey:@"localizedBook"];

Since the localised titles are being stored in the LocalizedBook managed object, you can make the title attribute a transient, but if you do that you can't use title in a predicate.

The nice thing about this approach is that the implementation of the to-many relationship is transparent to any consumers. You simply request internationalBook.title and the custom accessor returns the appropriate value based on the user's locale behind the scenes.

这篇关于Core Data中多语言数据的良好做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆