Android pri (16)

Android开发入门

Lesson 3B(3)

Localization

  • In order for the system to switch out(转化) different translations for the text in our app, we need to isolate all the text from other logic in the app.
  • 所以呢,我们就将string写在value文件夹中的strings.xml文件中,这样就能做到text和logic进行分离了。
  • 我们只需要创建多个资源子目录的文件夹用来保存我们的language即可。
  • Markdown
  • 在xml中获取到strings.xml中的文件是很简单的,但是在Java文件中我们需要这样做:
  • Markdown
  • References

How to do the Localization?

  • 首先我们需要将xml文件中的hard code string保存到strings.xml文件中去,然后再进行引用好,这样就不会产生硬编码了。
  • 在进行转化的过程中,我们需要注意一点就是有时候字符串中包含不应被翻译为其他语言的文本。 例如说名字,价钱啊之类的,这些东西我们只需要让用户进行输入,我们保留原来的文本就好,那么这里我们需要用到一个新的东西。
  • 本地化检查清单
  • 要标记不应翻译的文本,请使用 < xliff:g > 占位符标记。
  • 在使用占位符标记的时候要注意在resource那边加上一堆头文件一样的东西。
  • 还有一点要注意,就是当android中有’(单引号)这样的符号存在的话,那你要在这之前加上\这个符号,要不然会有错。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="app_name">Just Java</string>
    <string name="name_text_field">Name</string>
    <string name="topping_text_field">Toppings</string>
    <string name="Whipped_cream">Whipped cream</string>
    <string name="Chocolate">Chocolate</string>
    <string name="Quantity">Quantity</string>
    <string name="Order_Summary">Order Summary</string>
    <string name="Order_button">Order</string>
    <!-- Change the hard code in the Java File. -->
    <string name="thank_you">Thank you!</string>
    <string name="whipped_cream_ask">Add whipped Cream? </string>
    <string name="true_value">true</string>
    <string name="false_value">false</string>
    <string name="chocolate_ask">Add Chocolate? </string>
    <!-- The use of xliff -->
    <string name="priceMessage_Name">
    Name: <xliff:g id="name" example="Cathy">%s</xliff:g>
    </string>
    <string name="quantity_Message">
    Quantity: <xliff:g id="quantity">%s</xliff:g>
    </string>
    <string name="total_Message">
    Total: $<xliff:g id="price">%s</xliff:g>
    </string>
    <!-- 还有一点要注意,就是当android中有'这样的符号存在的话,那你要在这之前加上\这个符号,要不然会有错 -->
    <string name="warn_decrement_message">You can\'t order less than 1 ration of coffee one time!</string>
    <string name="warn_increase_message">You can\'t order more than 100 rations of coffee one time!</string>
    </resources>
  • 要进行翻译的话,直接右键选择“Open Translations Editor”

  • Markdown
  • 中文的xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="app_name">Just Java</string>
    <string name="Chocolate">巧克力</string>
    <string name="Order_Summary">订单详情</string>
    <string name="Order_button">支付</string>
    <string name="Quantity">数量</string>
    <string name="Whipped_cream">奶油</string>
    <string name="topping_text_field">配料</string>
    <string name="name_text_field">姓名</string>
    <string name="chocolate_ask">需要加巧克力酱吗?</string>
    <string name="false_value">不用了。</string>
    <string name="priceMessage_Name">"姓名:<xliff:g id="name" example="Cathy">%s</xliff:g> "</string>
    <string name="thank_you">感谢您的惠顾!</string>
    <string name="quantity_Message">数量:<xliff:g id="quantity">%s</xliff:g></string>
    <string name="total_Message">"总价:¥<xliff:g id="price">%s</xliff:g> "</string>
    <string name="true_value">需要。</string>
    <string name="warn_decrement_message">您不能一次预定1杯以下的咖啡!</string>
    <string name="warn_increase_message">您不能一次性预定超过100杯咖啡!</string>
    <string name="whipped_cream_ask">需要加点奶油吗?</string>
    </resources>
  • 效果图

  • Markdown
  • Markdown

  • MainActivity.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    package com.example.hclmaster.justjava;
    /**
    * Add your package below. Package name can be found in the project's AndroidManifest.xml file.
    * This is the package name our example uses:
    * <p>
    * package com.example.android.justjava;
    */
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    /**
    * This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {
    private int quantity = 0;
    private boolean isCreamSelected = false;
    private boolean isChocolateSelected = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    /**
    * This method is called when the order button is clicked.
    */
    public void submitOrder(View view) {
    int price = calculatePrice();
    boolean hasWhippedCream = isCreamSelected;
    boolean hasChocolate = isChocolateSelected;
    // this is the string of the name in the EditText
    EditText mEdit = (EditText) findViewById(R.id.name_text_field);
    String mEditMessage = mEdit.getText().toString();
    String priceMessage = getString(R.string.priceMessage_Name, mEditMessage)+"\n";
    priceMessage += createOrderSummary(price, hasWhippedCream, hasChocolate);
    // Log.v("MainActivity", "----> "+priceMessage);
    // 将信息文本发送到电子邮件中去进行打开
    String[] email={"hellochenghong@gmail.com"};
    Intent intent=new Intent(Intent.ACTION_SEND);
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_EMAIL, email);
    intent.putExtra(Intent.EXTRA_SUBJECT, "JustJava From "+ mEditMessage);
    intent.putExtra(Intent.EXTRA_TEXT, priceMessage);
    // 如果手机中有这个应用才打开这个intent
    if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
    }
    }
    /**
    * Calculates the price of the order.
    *
    * @paramreturn Total Price
    */
    private int calculatePrice() {
    int price = 5;
    if(isCreamSelected) price += 1;
    if(isChocolateSelected) price += 2;
    return quantity * price;
    }
    /**
    * This method displays the given quantity value on the screen.
    */
    private void displayQuantity(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
    }
    /**
    * This method is called when the plus button is clicked.
    */
    public void increment(View view) {
    quantity++;
    if(quantity > 100){
    quantity = 100;
    Context context = getApplicationContext();
    CharSequence text = getString(R.string.warn_increase_message);
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    return ;
    }
    displayQuantity(quantity);
    }
    /**
    * This method is called when the minus button is clicked.
    */
    public void decrement(View view) {
    quantity--;
    if(quantity < 1){
    quantity = 1;
    Context context = getApplicationContext();
    CharSequence text = getString(R.string.warn_decrement_message);
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    return ;
    }
    displayQuantity(quantity);
    }
    /**
    * Create summary of the order.
    *
    * @param hasWhippedCream is whether or not the user wants whipped cream topping
    * @param hasChocolate is whether or not the user wants whipped cream topping
    * @param priceOfCup of the order
    * @return text summary
    */
    private String createOrderSummary(int priceOfCup, boolean hasWhippedCream, boolean hasChocolate) {
    String Message = getString(R.string.whipped_cream_ask);
    if(hasWhippedCream == true){
    Message += getString(R.string.true_value);
    } else{
    Message += getString(R.string.false_value);
    }
    Message += "\n" + getString(R.string.chocolate_ask);
    if(hasChocolate == true){
    Message += getString(R.string.true_value);;
    } else{
    Message += getString(R.string.false_value);
    }
    Message += "\n" + getString(R.string.quantity_Message, quantity+"") + "\n" +
    getString(R.string.total_Message, priceOfCup+"") + "\n" + getString(R.string.thank_you);
    return Message;
    }
    /**
    * This method is to judge whether the checkbox is selected.
    * @param view
    * @return
    */
    public void onCheckboxClicked(View view){
    // Is the view now checked?
    CheckBox whippedcreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream);
    isCreamSelected = whippedcreamCheckBox.isChecked();
    // Figure out if the user wants chocolate topping
    CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
    isChocolateSelected = chocolateCheckBox.isChecked();
    }
    }

整理的有点累~translator果然有点辛苦=_=

热评文章