标签:des android style blog http color os io 使用
使用 Android 的人越来越多,每天都在增加。因此,当你想把你的应用成功地全球化时,通常的方法就是使应用程序本地化。
虽然本地化,你可以考虑使用依靠区域和国家适当的文字,音频,货币,数字和图形。但是,本文只涉及本地化的字符串,例如支持多国语言(multiple languages)。Localizing with Resources 说明了,当本地化应用程序时,应该考虑的其他事情。
本文我们创建一个支持 French、Deutsch(German)、Hindi 和 Japanese 语言的 app。
默认情况下,Android 把英语作为主要语言,并从 res?values?strings.xml 中加载字符串资源。当你想添加支持其他语言时,你需要创建相应的文件夹(通过追加一个连字符和 ISO 语言代码)。例如,如果你想添加支持法语,你应该创建一个名为“values-fr”的文件夹,并保持 strings.xml 中的所有字符串都翻译成法文。
简单来说,本地化的原理如下:
因此,默认的 stings.xml 文件应包含应用程序使用的所有字符串的值,这是强制性的。其他明智的应用程序将崩溃,并强制关闭的错误。Other wise the app will crash with Force Close error.
可以按如下方式:
当你想要 app 支持多语言时,最好采用下面方式定义字符串。总是在 strings.xml 声明字符串。
<string name="note_email">Enter your email address</string>
当在布局 XML 文件中引用时,使用 @strings 注解:
<TextView ... android:text="@string/note_email" />
在后台,可以使用 R.string:
emailNote.setText(R.string.note_email);
但不能:
Never hard code the string in xml or in java code which make the translation difficult.
<TextView ... android:text="Enter your email address" />
以及
emailNote.setText("Enter your email address");
下面创建一个项目,演示让android 支持多语言。
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="bg_gradient_start">#b21331</color>
<color name="bg_gradient_end">#820d2a</color>
<color name="bg_button_login">#380813</color>
</resources>
bg_button_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="@color/bg_button_login" >
</solid>
<!-- If you want to add some padding -->
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<!-- Here is the corner radius -->
<corners
android:radius="6dp" >
</corners>
</shape>
bg_form_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="@color/white" >
</solid>
<!-- If you want to add some padding -->
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<!-- Here is the corner radius -->
<corners
android:radius="6dp" >
</corners>
</shape>
bg_gradient.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:gradientRadius="750"
android:endColor="@color/bg_gradient_end"
android:startColor="@color/bg_gradient_start"
android:type="radial" />
</shape>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Multi Language App</string>
<string name="action_settings">Settings</string>
<string name="welcome">Welcome!</string>
<string name="email">Email Address</string>
<string name="password">Password</string>
<string name="login">Login</string>
<string name="signup">Don\‘t have account? Sign Up</string>
</resources>
现在,把字符串翻译成相关的语言,并放到相应的 strings.xml 文件中。
另外,针对我的真机,支持三种语言:中文,英文和韩语,因此,再增加一个韩语。翻译是用 google 翻译的,对不对我也不知道。
values-de/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">Willkommen!</string>
<string name="email">Email Addresse</string>
<string name="password">passowrd</string>
<string name="login">Login</string>
<string name="signup">müssen nicht angemeldet? Anmeldung</string>
</resources>
values-fr/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">accueil</string>
<string name="email">adresse e-mail</string>
<string name="password">mot de passe</string>
<string name="login">connexion</string>
<string name="signup">Ne pas avoir un compte? signer</string>
</resources>
values-hi/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">???????</string>
<string name="email">???? ???</string>
<string name="password">???????</string>
<string name="login">?????</string>
<string name="signup">???? ???? ??? ???? ?? ????</string>
</resources>
values-ja/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">歓迎</string>
<string name="email">電子メールアドレス</string>
<string name="password">パスワード</string>
<string name="login">ログイン</string>
<string name="signup">アカウントをお持ちでない場合は?サインアップ</string>
</resources>
values-ko/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">??</string>
<string name="email">???</string>
<string name="password">??</string>
<string name="login">???</string>
<string name="signup">??</string>
</resources>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="@string/welcome"
android:textColor="@color/white"
android:textSize="45dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_form_rounded"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@null"
android:hint="@string/email"
android:padding="5dp"
android:singleLine="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="@string/password"
android:inputType="textPassword"
android:padding="5dp" />
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="@drawable/bg_button_rounded"
android:text="@string/login"
android:textColor="@color/white" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:gravity="center_horizontal"
android:text="@string/signup"
android:textColor="@color/white" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().hide();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
图 1 Android 多语言——英文
1. 在设备上进入“设置 ? 语言和输入”
2. 选择你设备支持的语言。
Below table give you ISO languages codes for all the languages that android supports.
Language |
Locale |
values/strings.xml |
German | de | values-de/strings.xml |
Chinese | zh | values-zh/strings.xml |
Czech | cs | values-cs/strings.xml |
Dutch | nl | values-nl/strings.xml |
French | fr | values-fr/strings.xml |
Italian | it | values-it/strings.xml |
Japanese | ja | values-ja/strings.xml |
Korean | ko | values-ko/strings.xml |
Polish | pl | values-pl/strings.xml |
Russian | ru | values-ru/strings.xml |
Spanish | es | values-es/strings.xml |
Arabic | ar | values-ar/strings.xml |
Bulgarian | bg | values-bg/strings.xml |
Catalan | ca | values-ca/strings.xml |
Croatian | hr | values-hr/strings.xml |
Danish | da | values-da/strings.xml |
Finnish | fi | values-fi/strings.xml |
Greek | el | values-el/strings.xml |
Hebrew | iw | values-iw/strings.xml |
Hindi | hi | values-hi/strings.xml |
Hungarian | hu | values-hu/strings.xml |
Indonesian | in | values-in/strings.xml |
Latvian | lv | values-lv/strings.xml |
Lithuanian | lt | values-lt/strings.xml |
Norwegian | nb | values-nb/strings.xml |
Portuguese | pt | values-pt/strings.xml |
Romanian | ro | values-ro/strings.xml |
Serbian | sr | values-sr/strings.xml |
Slovak | sk | values-sk/strings.xml |
Slovenian | sl | values-sl/strings.xml |
Swedish | sv | values-sv/strings.xml |
Tagalog | tl | values-tl/strings.xml |
Thai | th | values-th/strings.xml |
Turkish | tr | values-tr/strings.xml |
Ukrainian | uk | values-uk/strings.xml |
Vietnamese | vi | values-vi/strings.xml |
Source: http://bit.ly/1qYfHDL
Right now I used Google Translate service to translate the strings into other languages. But if you want more accurate and meaningful translation always go for professional services like Professional translations through Google Play
Finally Localization Checklist gives you list of things to be verified before the app goes live when localization considered.
Android Building Multi-Language Supported App
标签:des android style blog http color os io 使用
原文地址:http://www.cnblogs.com/liuning8023/p/3962380.html