简体   繁体   English

翻译Android Studios中Java文件中的字符串值

[英]Translating String values that are in a Java File in Android Studios

I'm making a simple app for fun to show random facts every time a button is pressed. 我正在制作一个简单的应用程序,以便在每次按下按钮时显示随机事实。 I'm looking into making translations for my app, but I am not sure how to translate these sentences. 我正在考虑为我的应用程序翻译,但我不知道如何翻译这些句子。 My facts are all located in a Java Class. 我的事实都位于Java类中。

Here is my Facts Java class: 这是我的Facts Java类:

String facts[] = { 
            "In some cultures' telling of Snow White, the dwarves are thieves.",
            "Sea otters hold each other's paws while sleeping so they don't drift apart.",
            "The longest attack of hiccups ever lasted 68 years.", };

I've never translated sentences that came from a Java Class before, so I don't understand how to do this. 我以前从未翻译过来自Java类的句子,所以我不明白如何做到这一点。 Am I supposed to copy these values and put them in the strings.xml folder, or am I not on the right track? 我应该复制这些值并将它们放在strings.xml文件夹中,还是我不在正确的轨道上?

Yes, you should place them in an xml file. 是的,您应该将它们放在xml文件中。

You can use a StringArray 您可以使用StringArray

You can load them from the resources. 您可以从资源加载它们。

String[] facts = getContext().getResources().getStringArray(R.arrays.random_facts);

You can localize the string resources to get different translations. 您可以本地化字符串资源以获得不同的翻译。

You should use a ResourceBundle . 您应该使用ResourceBundle

Resource bundles contain locale-specific objects. 资源包包含特定于语言环境的对象。 When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale. 当您的程序需要特定于语言环境的资源(例如String ,您的程序可以从适合当前用户的语言环境的资源包中加载它。 In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles. 通过这种方式,您可以编写程序代码,该程序代码在很大程度上独立于用户的语言环境,隔离资源包中大多数(如果不是全部)特定于语言环境的信息。

This allows you to write programs that can: 这允许您编写可以:

  • be easily localized, or translated, into different languages 易于本地化或翻译成不同的语言
  • handle multiple locales at once 一次处理多个语言环境
  • be easily modified later to support even more locales 稍后可以轻松修改以支持更多语言环境

Eg use a PropertyResourceBundle and put the text in a set of property files, one file per language. 例如,使用PropertyResourceBundle并将文本放在一组属性文件中,每种语言一个文件。

Facts_en.properties : Facts_en.properties

f1=In some cultures' telling of Snow White, the dwarves are thieves.
f2=Sea otters hold each other's paws while sleeping so they don't drift apart.
f3=The longest attack of hiccups ever lasted 68 years.

Facts_da.properties : Facts_da.properties

f1=I nogle kulturers fortællinger om Snehvide, er dværgene tyve.
f2=Havoddere holder hinandens poter, mens de sover, så de ikke glider fra hinanden.
f3=Den længste angreb af hikke nogensinde varede 68 år.

You then call getBundle() to get the facts: 然后调用getBundle()来获取事实:

ResourceBundle bundle = ResourceBundle.getBundle("Facts", Locale.ENGLISH);
String fact1 = bundle.getString("f1");

Read The Java™ Tutorials - Lesson: Isolating Locale-Specific Data to learn more. 阅读Java™教程 - 课程:隔离特定区域设置的数据以了解更多信息。

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

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