简体   繁体   中英

Override NSManagedObject getter for localized string

I have a bunch of NSManagedObject entities in which I have a set of fields which needs to be translated properly.

I've read other posts about best practices for core data localization, however I was really hoping there was a method to centralize the management of the resolution of properly translated strings from keys related to entities.

So for example if I have an entity called Books with a field localizedDescription, instead of creating a relationship with a LocalizedBook which is an entity created just for the purpose of translation, I'd love to have all entities related to real world abstractions, and a unique entity, which could be also a strings file or a NSDictionary.

This way I'd love to create a subclass of NSManagedObject and a super class of Book in wich the getters for the fields, conditionally, could read the raw value or do some math and then return the translated value.

So what I am asking here is basically: how can I -with subclassing or categories- override the getter of every NSManagedObject field without writing the getter for every field or looping the fields? Is there a method I could override, like valueForKey:, without breaking anything?

For the sake of curiosity, I am more interested in this specific problem than to find the better way to solve the translation problem or the absolute best practice for translating core data values.

No you cant override getters without writing them. Both -valueForKey: and -primitiveValueForKey: should not be overridden.

UPD: May be overriding such methods, as NSObject's -methodForSelector: will do a trick.

当需要在应用程序中显示它们时,如何仅将所有值以一种语言保存在商店中,然后使用NSLocalizedString进行翻译呢?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM