What I used when I was writing multi-lingual support.

Source: Internet
Author: User
Tags collator comparable

What I used when I was writing multi-lingual support.
絮叨絮叨:好久不来写了,竟然支持markdown 了。 我也是在项目里的wiki 里干刚接触了一些, 来这里也试试。然后悲催的发现,mac 电脑在markdown下直接上传图片有bug @2015-08-19 20:28:13。一会试一下链接版的吧。我们的37度手环一不小心要卖到国外去了,自然要支持多国家多语言啦。 等卖到阿拉伯世界的时候,我会再补充RTL(Right To Left)相关的内容。

This article deals with the implementation of the Android client only, which is not described in the Server backend section. This paper mainly introduces the use of text and pictures in multi-languages, and how to modify the drop-down menu spinner, and also introduces the common shell commands in the process, awk command, efficient work, efficient life.

Android development itself has a good support for multiple languages, especially in the new version of Android Studio, which is also directly added to the multilingual editor. Dealing with multiple languages is an absolute artifact.
Support for official website [supporting Multi language][2]

Character translation

Of course, use the Multi-language editor in artifact Studio
Figure 1 Editor Open location

Figure 2 Editor interface

Figure 3 Directory Structure

Picture processing with text

The same as the principle of text, respectively, to establish the corresponding language picture folder
For example, a folder that corresponds to drawable-xhdpi different languages/regions is
drawable-ko-xhdpi
drawable-zh-xhdpi
Figure 4 Picture Multi-language directory

True multi-country function--support multi-country SMS verification code to prepare more than 1 countries area code

Country code [Country CODE][1]

Fetch is a big table, more than 200 lines. Organize your saved markdown files for easy viewing and editing later
The general copy is a tab \ t division, I use a vim to replace, the number of spaces and \ t all replaced | , the specific VIM operation command is as follows:

#在vim 中多次执行,把所有的\t 换成 多个空格:%s/\t/  |/g#然后把多个空格 换成|:%s/   */|/g

This forms the following format

#Country Number tableCountries and Regions|国家或地区|时差|电话代码|国际域名缩写------------------------|-----------|-----------|-------|-----Angola|安哥拉|-7|244|AOAfghanistan|阿富汗|0|93|AFAlbania|阿尔巴尼亚|-7|355|AL
Time
countries and regions Country or regionDifference Phone Code International Domain abbreviation
Angola Angola -7 244 AO
Afghanistan Afghanistan 0 93 Af
Albania Albania -7 355 AL
Prepare 2 country code for the String-array

Stored as String-array in each language/region resource folder, where awk is used to obtain the country name and the corresponding country code, respectively. Specific shell commands are as follows

$ cat countrynumber.  md | awk-f '  |  {print  $2   "|"  $4   "</item>" } ' > cn.txt$ cat countrynumber.md | Awk-f '  |  {print  $1   "|"  $4   "</item>" } ' > En.txt   
<string-array name="country_code">    <item>安哥拉|244</item>    <item>阿富汗|93</item>    <item>阿尔巴尼亚|355</item></string-array><string-array name="country_code">    <item>Angola|244</item>    <item>Afghanistan|93</item>    <item>Albania|355</item></string-array>
Java code parsing, and sorting

Since it is used | Split, then the code is parsed and sorted before the show

     PublicStatic arraylist<country> Getcountrycode (Context ctx) {arraylist<country> retlist =NewArraylist<> ();String[]Array= Ctx.getresources (). Getstringarray (R.Array. Country_code); for(StringItem:Array) {String[] data = Item.Split("\\|"); Country Country =NewCountry (); Country.Name = data[0]; Country.code = data[1];        Retlist.add (country);        } collections.sort (Retlist);    return retlist; }

Sorting requires class country to implement the sort interface comparable
When the system language is used in Chinese, the collator can be automatically sorted by the pinyin of the Chinese characters.

 Public  class country implements comparable<country> {     PublicString name; PublicString Code; @Override PublicString toString () {StringBuffer buffer =NewStringBuffer ();returnBuffer.append (name). Append (", "). Append (Code). toString (); } @Override Public intCompareTo (Country another) {if(another = =NULL) {return-1; } Collator cmp = Collator.getinstance (Locale.getdefault ());returnCmp.compare ( This. Name, Another.name); }}
Custom Spinner drop-down select country code

Let's take a look at the implementation effect, two styles of spinner
Figure Spinner

Nothing high-tech, is to let spinner according to their own meaning to show. Two kinds are used in different places. Flexible use is not what the siege lion to do.
The interface modification of spinner is mainly two parts:
One is not expanded state, in GetView () modified, you can change the triangle picture, control the display location, etc.;
The other is the expanded state-modified in Getdropdownview () to control what is displayed after the expansion.

The realization of Countryspinneradapter

 Public classCountryspinneradapter extends Arrayadapter<country> {PrivateContext context;PrivateArraylist<country> mcountrylist;PrivateSpinner Mspinner;PrivateBoolean Misfamilyspinner; Public Countryspinneradapter(context context, Spinner Spinner, Arraylist<country> countrylist, Boolean Isfamilyspinner) {Super (context, R.layout.country_spinner_item, countrylist); This. Context = Context;        Mcountrylist = countrylist; Mspinner = spinner; This. Misfamilyspinner = Isfamilyspinner; } Public Countryspinneradapter(context context, Spinner Spinner, arraylist<country> countrylist) { This(context, spinner, Countrylist,false); } @Override PublicViewGetdropdownview(intPosition, View Convertview, ViewGroup parent) {if(Convertview = =NULL) {Convertview = View.inflate (GetContext (), Misfamilyspinner? R.layout.country_spinner_layout_family:r.layout.country_spinner_layout,NULL); } Country Country = Mcountrylist.Get(position);        TextView name = (TextView) Convertview.findviewbyid (r.id.country_name);        TextView code = (TextView) Convertview.findviewbyid (R.id.country_code);        Name.settext (Country.Name); Code.settext ("+"+ Country.code);if(mspinner.getselecteditemposition () = = position) {convertview.setselected (true); }Else{convertview.setselected (false); }returnConvertview; } @Override PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {if(Convertview = =NULL) {Convertview = View.inflate (GetContext (), Misfamilyspinner? R.layout.country_spinner_item_family:r.layout.country_spinner_item,NULL); } Country Country = (country) mspinner.getselecteditem ();if(Misfamilyspinner)            {TextView code = (TextView) Convertview.findviewbyid (R.id.country_code); Code.settext ("+"+ Country.code); }Else{TextView name = (TextView) Convertview.findviewbyid (r.id.country_name);            TextView code = (TextView) Convertview.findviewbyid (R.id.country_code);            Name.settext (Country.Name); Code.settext ("+"+ Country.code); }if(mspinner.getselecteditemposition () = = position) {convertview.setselected (true); }Else{convertview.setselected (false); }returnConvertview; }}

Use the layout separately to control:

Layout/country_spinner_item.xml

A horizontal layout contains
Two TextView display name and code respectively
A ImageView, ImageView used to show the drop-down triangle

<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Wrap_content"  Android:gravity="center_vertical"android:background="@drawable/family_spinner_bg "android:orientation="horizontal ">                        <TextView        Android:id="@+id/country_name"        Android:layout_marginleft="8DP"        Android:layout_width="0DP"        Android:layout_height="Wrap_content"        Android:layout_weight="1"        Android:singleline="true"        Android:text="@string/family_dialog_phone_number"        Android:textcolor="@color/family_dialog_message"        android:textsize="18SP"/>    <TextViewandroid:id="@+id/country_code"android:layout_marginright= "8DP" android:layout_width="Wrap_content"android:layout_height="Wrap_content"  Android:textcolor= "@color/family_dialog_message"android:textsize=" 18sp " />                                                    <ImageViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:src="@drawable/country_spinner_arrow" />                        </linearlayout>
Layout/country_spinner_layout.xml

A horizontal layout contains
Two TextView display name and code respectively

<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Match_parent"android:layout_height="25DP"Android:background="@drawable/family_spinner_back"android:gravity="Center_vertical"> <textview android:id="@+id/country_name"Android:layout_width="0DP"android:layout_height="Wrap_content"android:layout_marginleft="8DP"android:layout_weight="1"Android:duplicateparentstate="false"Android:ellipsize="End"Android:singleline="true"android:text="@string/family_dialog_phone_number"Android:textcolor="@color/family_spinner_text"Android:textsize="18SP"/> <textview android:id="@+id/country_code"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_marginright="10DP"Android:duplicateparentstate="false"android:text="@string/family_dialog_phone_number"Android:textcolor="@color/family_spinner_text"Android:textsize="18SP"/></linearlayout>
能看到这里,说明也是缘分,广告一下,有缘人看过来:这里上有70后靠谱大叔CXO,下有90后萝莉鲜肉,前有各种博士搞定算法硬件,后有脸皮厚猥琐男拿下销售。现在人手紧缺,安卓开发,IOS 开发,UI/UX,产品经理都需要人,活真的要干不过来了。希望自我驱动力强,能够平衡风险收益的同学欢迎来勾搭。鄙人37度手环安卓开发,扣扣:84365252。

[1]: Https://countrycode.org/country code
[2]: http://developer.android.com/training/basics/supporting-devices/languages.html support Multi languages

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

What I use when I write multi-lingual support

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.