Android Studio error message I18n

混江龙づ霸主 提交于 2020-01-07 07:19:29

问题


I got an error message that says:

[I18N] Hardcoded string "Today - Sunny - 78/67", should use @string resource.

How can this error be resolved?

The line of code is: android:text="Today - Sunny - 78/67"


回答1:


i18n is an abbreviation for internationalization. Android Studio is just giving you a hint that the way you are setting the text won't allow you to easily translate it.

Instead you should put your string in a strings.xml file within the res/values folder:

<resources>
  <string name="my_string">Today - Sunny - 78/67</string>
</resources>

You then reference that string on your TextView like this:

android:text="@string/my_string"

This would allow you to make another file for spanish strings, res/values-es/strings.xml that you could add a translation to:

<resources>
  <string name="my_string">Hoy - Soleado - 78/67</string>
</resources>

And then that string will automatically get picked for your TextView if the user's locale language is Spanish.




回答2:


It's implying that you should not explicitly write your text in your XML. You should use the /Values/Strings.xml to hold your string values.

Take a look through the folders on the left side, for values. The file is already there. You just need to add your string to it.

For the record, this doesn't normally generate an error of the "I'm gonna crash now" variety. It's usually just a warning.




回答3:


you can put mouse on problems line and use android studio hot key - alt+enter. After that you can choose "Extract string resource"

Sory fo my bad english =)



来源:https://stackoverflow.com/questions/31997616/android-studio-error-message-i18n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!