Skip to content

Add New Language Support

Harsh Bhakta edited this page Oct 28, 2017 · 2 revisions

To add new language support is really easy.

Let’s see. For example, here we want to add support for the Turkish language.

1. Fork and Clone the project

2. Prepare resource file

  • Open cloned project in Android Studio
  • Find “src/main/res/raw/a_new_lang_tamplate.xml”. It is a template with list of countries where correct translated names must be placed.
  • Make a copy of that file in the same folder and name it as “turkish.xml”. This must be the name of language.
  • Now in this turkish.xml, add correct translations where “todo”s are specified.
  • ccp_dialog_title is the title shown on selection list which should be a translation of “Select a country”.
  • ccp_dialog_search_hint_message is the search editText hint and should be a translation of “Search…”.
  • ccp_dialog_no_result_ack_message is the message shown when no country found for the query. It should be the translation of “Results not found”.
  • For all countries, replace “todo” with a translated country name.

3. Make entry in CountryCodePicker.java

  • Go to src/main/java/com/hbb20/CountryCodePicker.java

  • Find for “public enum Language “. This is the list of supported language in alphabetical order. Based on order, figure out the position for new language. For Turkish, it will go between Spanish and Ukrainian. So

     ARABIC("ar"),
     BENGALI("bn"),
     CHINESE_SIMPLIFIED("zh"),
     CHINESE_TRADITIONAL("zh"),
     ENGLISH("en"),
     FRENCH("fr"),
     GERMAN("de"),
     GUJARATI("gu"),
     HEBREW("iw"),
     HINDI("hi"),
     INDONESIA("in"),
     JAPANESE("ja"),
     KOREAN("ko"),
     PORTUGUESE("pt"),
     RUSSIAN("ru"),
     SPANISH("es"),
     UKRAINIAN("uk");
    

    will be

     ARABIC("ar"),
     BENGALI("bn"),
     CHINESE_SIMPLIFIED("zh"),
     CHINESE_TRADITIONAL("zh"),
     ENGLISH("en"),
     FRENCH("fr"),
     GERMAN("de"),
     GUJARATI("gu"),
     HEBREW("iw"),
     HINDI("hi"),
     INDONESIA("in"),
     JAPANESE("ja"),
     KOREAN("ko"),
     PORTUGUESE("pt"),
     RUSSIAN("ru"),
     SPANISH("es"),
     TURKISH("tr"),
     UKRAINIAN("uk");
    
  • Here the name must be UpperCase version of the file name (apart from .xml) given in step 2.B. In this case its “TURKISH” for turkish.xml.

  • 2 letter language code can be found at https://www.w3schools.com/tags/ref_language_codes.asp. For turkish language it is “tr”.

4. Add attribute entry.

  • Go to src/main/res/values/attrs.xml

  • Find <attr name="ccp_defaultLanguage" format=“enum">

  • Make language entry in alphabetical order.

  • Update enum values for all the language below the new language. So that entire list has correct value order as well. This is very important, as it is used as mapping with the list we updated earlier in CountryCodePIcker.java.

  • For example, earlier it was


     ...
     ...
     <enum name=“SPANISH” value=“15”>
     <enum name=“UKRAINIAN” value=“16”>
     ```

    
    
  • After inserting Turkish between them it, it should look like

     ...
     ...
     <enum name=“SPANISH” value=“15”>
     <enum name=“TURKISH” value=“16”>
     <enum name=“UKRAINIAN” value=“17”>
    

5. Testing

  • Finally we want to make sure that everything we have done so far is all good.
  • Now do “Menu > Build > Make project”
  • Open CountryCodePickerProject/app/src/main/res/layout/fragment_introduction.xml
  • There is one CountryCodePicker view. Set our default language as new language.
  • For this case, add app:ccp_defaultLanguage=“TURKISH" to the view.
  • Run the app on device
  • Open introduction page and tap on ccp view.

6. Create a pull request

  • If it is tested OK, please create a pull request. We will be happy to merge it.
Clone this wiki locally