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

查看:148
本文介绍了如何在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

我想要两个实体中的 name s(string)来存储本地化的变体。这意味着另一个连接。有少量可能的本地化。我知道我可以把每个本地化作为一个单独的属性(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一个,本土化。但是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?

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

(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:

名称是英文名称。所以我不必在英语设备中搜索关系(这涵盖了我的应用程序的70%的设备)。

并且有一个名为 localizedName Something 子类中返回名称或当前使用的语言代码的字符串。

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