如何保存从微调器中选择的值,如果设备的语言发生变化,该值也会发生变化?

发布于 2024-12-03 17:20:45 字数 1293 浏览 0 评论 0原文

所以我想这个是非常困难的。

我已经尝试了所有方法和我所有的知识,所以在搜索了所有网络和这个网站之后我不得不问这个问题。

事情是这样的:

我有一个旋转器,其值(英文)如下: 蜜蜂 鸟类 松鼠

在西班牙语中,值是:

abejas 帕哈罗斯 ardillas

构造进入微调器的值。

我确保它们是相同的,因为在运行时我从名为store.xml

<resources>
   <string name="bees">bees</string>
   <string name="birds">birds</string>
   <string name="squirrels">squirrels</string>
   <string name="other_species">other species</string>
</resources>

文件夹下

的文件并在 value-es store.xml

<resources>
   <string name="bees">abejas</string>
   <string name="birds">pajaros</string>
   <string name="squirrels">ardillas</string>
   <string name="other_species">otra especie</string>
</resources>

问题是我不应该保存由用户,因为当他们更改设备的语言时,保存的值将采用不同的语言。

所以我想到了一种获取用户选择的字符串值的唯一Id的方法,我使用res.getIdentifier来获取Id,然后获取字符串资源的名称。然后我会将资源的名称保存在数据库中,这样如果语言发生变化,我将能够根据名称找到该值。

现在,只要值与名称相同,res.getIdentifier 就可以工作,即:

  value "bees", name="bees" (works)
  value "bees", name="abejas" (don't work)
  value "other_species", name="other species" (don't work)

那么有没有一种方法可以从值获取 xml 中字符串的名称?

或者还有其他解决方法吗?

So this one is very difficult I guess.

I've been trying all means and all of my knowledge, so after searching all the web and this site I have to ask the question.

Here is the deal:

I have a spinner that has as values (in English) say:
bees
birds
squirrels

In spanish the values will be:

abejas
pajaros
ardillas

I make sure they are the same because at runtime I construct the values going into the spinner from a file called

store.xml

<resources>
   <string name="bees">bees</string>
   <string name="birds">birds</string>
   <string name="squirrels">squirrels</string>
   <string name="other_species">other species</string>
</resources>

and under the folder values-es

store.xml

<resources>
   <string name="bees">abejas</string>
   <string name="birds">pajaros</string>
   <string name="squirrels">ardillas</string>
   <string name="other_species">otra especie</string>
</resources>

The problem is that I shouldn't save the actual value selected by the user, because when they change the device's language, then the value saved it would be in a different language.

So I thought in a way of getting the unique Id of the string value selected by the user and I used res.getIdentifier to get the Id and then the name of the string resource. THEN I would save the name of the resource in the database, that way if the language changed I would be able to locate the value based on the name.

Now, res.getIdentifier worked, as long as the value was the same as the name i.e.:

  value "bees", name="bees" (works)
  value "bees", name="abejas" (don't work)
  value "other_species", name="other species" (don't work)

So is there a way that I can get to the name of a string in an xml, from the value?

Or is there another workaround this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心未许 2024-12-10 17:20:45

您可以使用 id 而不是字符串填充微调器,并使用自定义适配器来显示字符串。这样逻辑就与语言无关了。

You could fill your spinner with ids rather than strings, and use a custom adapter to display the strings. That way the logic is language independent.

无悔心 2024-12-10 17:20:45

使用另一个数组作为映射,例如:

English  Language A  
   0         2
   1         3
   2         0
   3         1

Use another array as a mapping for example:

English  Language A  
   0         2
   1         3
   2         0
   3         1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文