在Android中,是否有理由将字符串资源用于不会翻译的字符串? [英] In Android, is there a reason to use string resources for strings that are not going to be translated?

查看:119
本文介绍了在Android中,是否有理由将字符串资源用于不会翻译的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用在Android代码中的java文件中定义的字符串有什么弊端.

I'm wondering what the drawbacks are for using strings that are defined in the java files in Android code.

我喜欢将普通的Java旧字符串用于那些不可见的字符串,例如我正在解析的XML文档中的名称或捆绑包的密钥.最好将所有这些内容保留在使用它们的java文件中,而不是将它们移到XML文件中并使代码更复杂.

I like to use plain old Java strings for things that are not visible strings like e.g. names in XML documents that I'm parsing, or keys for bundles. Seems to be a good idea to just keep all those things in the java file where they are used instead of moving them out into an XML file and making the code more complicated.

但是,我看到了许多Android代码示例,这些示例似乎将每个字符串都放入资源文件中.

Yet, I see many examples of Android code that seem to put every string into a resource file.

在Java文件中包含字符串有什么问题?人们不这样做的原因是什么?我一直在我的应用程序中执行此操作,到目前为止尚未看到任何问题.

What's the issue with having strings in java files? What are the reasons that people don't do it? I've been doing it in my apps and haven't seen any issues yet so far.

请注意,我知道XML文件对于需要翻译的内容意义重大.该问题适用于字符串保持不变的情况.

Note that I'm aware that XML files make a ton of sense for stuff that needs to be translated. This question is for cases where the strings stay the same.

让我尝试使这个问题更清楚:

Let me try to make this question clearer:

除了 之外,是否有任何原因:

Are there any reasons except:

  • 因为这是标准/最佳实践等-我的问题基本上是:为什么仅因为i8n才是最佳实践,还是有其他原因?
  • 因为它允许您使用资源框架进行翻译,与设备有关的字符串等.
  • 因为它允许您使用非ASCII字符.

推荐答案

这个问题的简单答案是将所有字符串都放入资源中的标准.同样有很多原因,如果您将字符串保留在xml/java文件中,则必须为单个字符串更新这些文件中的每个引用.

The simple answer to your question is its a standard to put all your string into resource. Also there are many reason that if you are keeping your string in xml/java file you have to update each and every reference in these file for a single string.

例如如果要在5个不同的文件中将确定"更改为确认",则必须在这5个文件中都进行更改,但是对于String资源,您只需要更新一个文件string.xml.

for eg. if You want to change "Ok" to "confirm" which are used in 5 different file you have to change in all those 5 files but for String resource you just have to update one file which string.xml.

修改

请在下面找到一些我们应该使用String.xml的原因

Please find below some of reasons we should use String.xml

1)将单个引用更新为多次出现.就像根据@treesAreEverywhere一样,可以使用公共静态String来完成,但是它将在应用程序启动时以及关闭应用程序之前占用内存.但是用String.xml编写的String会在使用时加载.

1) To update single reference to multiple occurrences. As according to the @treesAreEverywhere It can be done with public static String, but it will take memory on startup of application and till application is closed. But String written in String.xml will be loaded at time of use.

2)多语言支持.您可以创建多语言资源文件夹以支持您的多语言应用程序,因此操作系统会根据语言资源文件夹在运行时动态维护使用语言环境更改的语言.

2) Multiple language support. You can create multiple language resource folder to support your multiple language application so language changed using Locale will be dynamically maintained by OS at run time according to language resource folder.

3)请检查本地化文档,该文档可为您提供更多信息关于使用string.xml

3) Please check Localization document which provide you more information about using string.xml

4)字符串不会使您的应用程序代码混乱,使其清晰易维护.

4) Strings don’t clutter up your application code, leaving it clear and easy to maintain.

与其他任何语言一样,这是一种编码标准.但是,如果需要,您可以忽略它,并且可以在代码中使用公共静态字符串变量创建代码.使用string.xml不是强制性的,但是使用它是一种良好的编码习惯.良好的做法是,用带单引号的圆括号关闭if块,而不要照原样保留.

It's a kind of coding standard like any other language has. But you can ignore it if you want and can create your code with public static string variable in code. It is not compulsory to use string.xml but its a good coding practice to use it. Good practice like closing the if block with parenthesis containing single statement rather than leaving it as it is.

if(condition){ statement; }而不是if(condition) statement;

这篇关于在Android中,是否有理由将字符串资源用于不会翻译的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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