如何在 Core Data 中表示本地化的字符串类型? [英] How to represent a localized string type in Core Data?

查看:19
本文介绍了如何在 Core Data 中表示本地化的字符串类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Core Data 的新手,并且在概念上正在努力解决其中的一些问题(例如,相对于我理解的 SQL).

I'm new to Core Data, and am struggling with some of it conceptually (relative to, say, SQL, which I understand).

为了简单起见,我正在尝试构建一个模型:

I'm trying to build a model which for the sake of simplicity looks like:

"Category" entity, which has a name, and a relationship to-many Products
"Product" entity, which has a name

我希望两个实体中的那些 names(字符串)存储本地化的变体.这意味着另一个加入.有少量可能的本地化.我知道我可以将每个本地化作为一个单独的属性(name_en"、name_de"等),但这不能扩展,我想了解实现这一点的正确"方式.

I want those names (string) in both entities to store localized variants. That implies another join. There are a small number of possible localizations. I know that I could put each localization as an individual attribute ("name_en", "name_de", etc), but that doesn't scale, and I want to understand the "right" way of accomplishing this.

我的直觉告诉我我还想要两个实体,一个 Localizations(它只包含一组可能的本地化)和某种 LocalizedString 一个,它与 Localization 相关.但是 Xcode 警告我没有设置反向关系等.

My gut tells me I want two more entities here, a Localizations one (which simply contains the set of possible localizations) and some sort of LocalizedString one, which related to the Localization. But Xcode warns me about not having Inverse relationships set up, etc.

深入了解 Core Data 模型设计的人可以帮助新手理解如何思考这个问题吗?

Can someone who groks Core Data model design deeply please help out a newbie understand how to think through this problem?

(我的下一个问题将是构建奇怪的多枢轴用户界面,让您可以为每个可用的本地化设置名称,但这将是另一组调查.:) )

(My next problem will be building the weirdly multi-pivoted UI that lets you set the name for each localization that's available, but that will be another set of investigation. :) )

推荐答案

我不知道我是否有资格成为了解核心数据的人,但我过去使用过这样的东西:

I don't know if I qualify as somebody who groks core data but I've used something like this in the past:

name是英文名.因此,我不必在英语设备(涵盖了我的应用 70% 的设备)中搜索关系.
Something 子类中有一个名为 localizedName 的 getter,它返回当前使用的语言代码的名称或字符串.

name in Something is the english name. So I don't have to search the relationship in english language devices (which covers 70% of the devices for my app).
And there is a getter called localizedName in the Something subclass that either returns name or the string for the currently used language code.

当我这样做时,我想我可以将本地化的字符串保存在 name 属性中,因为语言通常不会在开发人员不使用的设备上经常更改.但最终我决定不这样做,因为那不是性能问题.

When I did this I thought that I could save the localized string in the name property because languages usually don't change often on devices that are not used by developers. But finally I decided against this because that wasn't a performance problem.

我认为可以用来解决不存在的性能问题的另一个变体:

Another variant I thought I could use to fight against non-existing performance problems:

currentLanguageString 基本上指向当前语言的本地化字符串.每次语言更改时它都会更改(在所有情况下,在 99.9% 的情况下会发生一次,即应用程序首次启动时).

currentLanguageString basically points to the localized string of the current language. It is changed everytime the language changes (in 99.9% of all cases this happens one time, when the app is first launched).

如果您在 Something 实体中有多个字符串,我会将 LocalizedString 设为一个抽象父类,该子类专门为您要本地化的字符串创建.
您必须使用这种奇怪的构造,因为您无法创建与多个实体的关系.

if you have more than one string in the Something entity I would make LocalizedString an abstract parent class of subclasses specifically created for the string you want to localize.
You have to use such strange constructs because you can't create a relationship to more than one entity.

或者,如果您知道自己在做什么,请忽略无反向关系"警告

Or if you know what you are doing, ignore the "no inverse relationship" warning

但如果你这样做,你必须确保你永远不会删除仍在使用的 LocalizedString 对象.您不想以不一致的存储结束.

but if you go this way you have to make sure that you never delete a LocalizedString object that is still in use. You don't want to end with an inconsistent storage.

这篇关于如何在 Core Data 中表示本地化的字符串类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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