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

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

问题描述

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

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.

有人有一些提示?

推荐答案

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

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

InternationalBook LocalizedBook 都是一对多关系的自定义托管对象(一本国际书对许多本地化书)

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

InternationalBook 的实现中,我添加了一个自定义访问器 title

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] 是返回用户语言和国家代码的类方法: en_US fr_FR code> NSLocale 有关详细信息。

[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.

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

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

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

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"];

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

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.

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

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天全站免登陆