I am already well aware of how easy it is to make a multilanguage app with Android by using the string.xml code. That part was nice and easy.
我已经很清楚使用string.xml代码在Android上制作多语言应用程序是多么容易。那部分很好很容易。
The issue is I have a lot of dynamic texts in my app that are made with my Java code. This is the part where I am can't find how to also make multilingual. The way I am picturing a solution is having some kind of condition in which I verify what language it is in, and change the string. For example:
问题是我的应用程序中有很多动态文本是用我的Java代码编写的。这是我无法找到如何制作多语言的部分。我想象一个解决方案的方式是在某种条件下我会验证它所使用的语言,并更改字符串。例如:
if(language.equals(English))
textView1.setText("Hi! This is in English!");
if(language.equals(Portuguese))
textView1.setText("Oi! Isso é em Português!");
Is it possible for me to do something like this? If so, how can I do it?
我可以这样做吗?如果是这样,我该怎么办?
Thanks!
谢谢!
3 个解决方案
#1
5
Extract those strings into the strings.xml
file too and it will choose the value from the correct file depending on the language. There is no reason to have those strings hardcoded in your java.
将这些字符串解压缩到strings.xml文件中,它将根据语言从正确的文件中选择值。没有理由在java中硬编码这些字符串。
String hello = getResources().getString(R.string.hello);
Will have the right language if its available, since at runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device. For instance if you have res/values-ru/strings.xml
that contains res/values/strings.xml
and the device is in russian, it will take the value (if it's available) from the first one.
如果可用,将具有正确的语言,因为在运行时,Android系统根据当前为用户设备设置的语言环境使用适当的字符串资源集。例如,如果你有res / values-ru / strings.xml包含res / values / strings.xml并且设备是俄语的,它将从第一个获取值(如果它可用)。
#2
2
Based on your example, it sounds like you don't actually understand how strings.xml works, because that seems to be the answer you're looking for. Your entire example can be replaced with one line:
根据您的示例,听起来您实际上并不了解strings.xml的工作原理,因为这似乎是您正在寻找的答案。您的整个示例可以替换为一行:
textView1.setText(R.string.hello);
Then you create different versions of strings.xml, all of them containing exactly the same IDs but different string values. If English is your default, then the English version would be named /res/values/strings.xml. The Portuguese file would be named /res/values-pt/strings.xml. The system would automatically use the strings from the correct file for the device locale.
然后创建strings.xml的不同版本,所有这些版本都包含完全相同的ID但不同的字符串值。如果英语是您的默认值,则英文版本将命名为/res/values/strings.xml。葡萄牙文件名为/res/values-pt/strings.xml。系统会自动使用正确文件中的字符串作为设备区域设置。
#3
1
After a few hundreds texts to translate and include in the code, you'll see that it is much simpler to use strings in resources.
在几百个要翻译并包含在代码中的文本之后,您会发现在资源中使用字符串要简单得多。
- Your java code never has to worry about adding a new language, just create a new
values-..
folder - 您的Java代码永远不必担心添加新语言,只需创建一个新的values- ..文件夹
- You can have separate persons, including non-developers, working on translations at the same time as you work on the code
- 您可以让单独的人员(包括非开发人员)在您处理代码的同时进行翻译
- You never have to worry about a missing translation, as lint will warn you, and the default language fallback will guaranty there will be no crash
- 您永远不必担心缺少翻译,因为lint会警告您,默认语言后备将保证不会发生崩溃
- That's the only way to put localized texts in XML layout without setting them explicitly in java, which saves you tons of useless, error prone, spaghetti code.
- 这是将本地化文本放在XML布局中而不在Java中明确设置它们的唯一方法,这可以节省大量无用的,容易出错的意大利面条代码。
- It is the standard and recommended way to do so, meaning a/ you'll find support should you need it and b/ any android developer joining your team will be in a familiar setting
- 这是标准和推荐的方式,这意味着你需要它/你会找到支持和b /任何加入你的团队的Android开发人员将在熟悉的环境中
#1
5
Extract those strings into the strings.xml
file too and it will choose the value from the correct file depending on the language. There is no reason to have those strings hardcoded in your java.
将这些字符串解压缩到strings.xml文件中,它将根据语言从正确的文件中选择值。没有理由在java中硬编码这些字符串。
String hello = getResources().getString(R.string.hello);
Will have the right language if its available, since at runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device. For instance if you have res/values-ru/strings.xml
that contains res/values/strings.xml
and the device is in russian, it will take the value (if it's available) from the first one.
如果可用,将具有正确的语言,因为在运行时,Android系统根据当前为用户设备设置的语言环境使用适当的字符串资源集。例如,如果你有res / values-ru / strings.xml包含res / values / strings.xml并且设备是俄语的,它将从第一个获取值(如果它可用)。
#2
2
Based on your example, it sounds like you don't actually understand how strings.xml works, because that seems to be the answer you're looking for. Your entire example can be replaced with one line:
根据您的示例,听起来您实际上并不了解strings.xml的工作原理,因为这似乎是您正在寻找的答案。您的整个示例可以替换为一行:
textView1.setText(R.string.hello);
Then you create different versions of strings.xml, all of them containing exactly the same IDs but different string values. If English is your default, then the English version would be named /res/values/strings.xml. The Portuguese file would be named /res/values-pt/strings.xml. The system would automatically use the strings from the correct file for the device locale.
然后创建strings.xml的不同版本,所有这些版本都包含完全相同的ID但不同的字符串值。如果英语是您的默认值,则英文版本将命名为/res/values/strings.xml。葡萄牙文件名为/res/values-pt/strings.xml。系统会自动使用正确文件中的字符串作为设备区域设置。
#3
1
After a few hundreds texts to translate and include in the code, you'll see that it is much simpler to use strings in resources.
在几百个要翻译并包含在代码中的文本之后,您会发现在资源中使用字符串要简单得多。
- Your java code never has to worry about adding a new language, just create a new
values-..
folder - 您的Java代码永远不必担心添加新语言,只需创建一个新的values- ..文件夹
- You can have separate persons, including non-developers, working on translations at the same time as you work on the code
- 您可以让单独的人员(包括非开发人员)在您处理代码的同时进行翻译
- You never have to worry about a missing translation, as lint will warn you, and the default language fallback will guaranty there will be no crash
- 您永远不必担心缺少翻译,因为lint会警告您,默认语言后备将保证不会发生崩溃
- That's the only way to put localized texts in XML layout without setting them explicitly in java, which saves you tons of useless, error prone, spaghetti code.
- 这是将本地化文本放在XML布局中而不在Java中明确设置它们的唯一方法,这可以节省大量无用的,容易出错的意大利面条代码。
- It is the standard and recommended way to do so, meaning a/ you'll find support should you need it and b/ any android developer joining your team will be in a familiar setting
- 这是标准和推荐的方式,这意味着你需要它/你会找到支持和b /任何加入你的团队的Android开发人员将在熟悉的环境中