简体   繁体   English

android中的非本地化字符串

[英]Non-localized strings in android

How do I avoid (some) of my string resources getting localized? 如何避免(某些)我的字符串资源被本地化?

Sometimes I need the same string multiple places. 有时我需要多个相同的字符串。 For instance when defining a preference I need the preference key both in my preferences.xml and in my java code in order to get the preference value. 例如,在定义首选项时,我需要在preferences.xml和我的java代码中使用首选项键以获取首选项值。 Either I can manually define the key twice (which I do now) but I feel that my list of keys is getting large and error prone. 要么我可以手动定义键两次(我现在这样做),但我觉得我的键列表变得越来越大并且容易出错。

I could also define the key in my strins.xml but then I fear it will get translated (sooner or later (by mistake?)). 我也可以在我的strins.xml中定义密钥,但后来我担心它会被翻译(迟早(错误?))。 I fear this could cause problems to have the same preference value store multiple times with different keys. 我担心这可能导致问题是使用不同的密钥多次存储相同的优先值。 What if the user changes language and all the settings got reset? 如果用户更改语言并重置了所有设置,该怎么办?

Is there a way to define a string - accessible both in code and xml - which will not get translated as a part of the localization process? 有没有办法定义一个字符串 - 可以在代码和xml中访问 - 它们不会作为本地化过程的一部分进行翻译?

In general, if you provide the string only in your default strings.xml it will appear the same on all locales. 通常,如果仅在默认strings.xml中提供字符串,则它在所有语言环境中显示相同。 In addition you can use the "translatable" attribute which will tell Lint that this string is not designed to be translated, and will serve for documentation purposes. 此外,您可以使用“可翻译”属性,该属性将告诉Lint此字符串不是为了翻译而设计的,并且将用于文档目的。

Just use translatable=false . 只需使用translatable=false And I suggest putting your preferences keys to separate resource file as it would be easier to manage. 我建议将您的首选项键放在单独的资源文件中,因为它更容易管理。

<string name="Key" translatable="false">my key</string>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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