Using Multiple Languages in Giggity app
Giggity app is used for conferences around the world. It becomes essential that it provides support for native languages as the users may not understand the terminologies written primarily in English from different countries. In this blog, I describe how to add a resource for another language in your app with the example of Giggity. I recently worked on the addition of French translation in the app. We look at the addition of German in the app.
You can specify resources tailored to the culture of the people who use your app. You can provide any resource type that is appropriate for the language and culture of your users. For example, the following screenshot shows an app displaying string and drawable resources in the device’s default (en_US) locale and the German (de_DE) locale.
It is good practice to use the Android resource framework to separate the localized aspects of your application as much as possible from the core Java functionality:
- You can put most or all of the contents of your application’s user interface into resource files, as described in this document and in Providing Resources.
- The behaviour of the user interface, on the other hand, is driven by your Java code. For example, if users input data that needs to be formatted or sorted differently depending on locale, then you would use Java to handle the data programmatically. This document does not cover how to localize your Java code.
Whenever the application runs in a locale for which you have not provided locale-specific text, Android will load the default strings from res/values/strings.xml. If this default file is absent, or if it is missing a string that your application needs, then your application will not run and will show an error. Here is an example of default strings in the app.
<!-- Menu --> <string name="settings">Settings</string> <string name="change_day">Change day</string> <string name="show_hidden">Show hidden items</string> <string name="timetable">Timetable</string> <string name="tracks">Tracks</string> <string name="now_next">Now and next</string> <string name="my_events">My events</string> <string name="search">Search</string>
An application can specify many res/<qualifiers>/ directories, each with different qualifiers. To create an alternative resource for a different locale, you use a qualifier that specifies a language or a language-region combination. (The name of a resource directory must conform to the naming scheme described in Providing Alternative Resources, or else it will not compile.) You can specify resources tailored to the culture of the people who use your app. You can provide any resource type that is appropriate for the language and culture of your users. For example, the following screenshot shows an app displaying string and drawable resources in the device’s default (en_US) locale and the German (de_DE) locale.
<!-- Menu --> <string name="settings">Einstellungen</string> <string name="change_day">Tag ändern</string> <string name="timetable">Zeitplan</string> <string name="tracks">Tracks</string> <string name="now_next">Jetzt und gleich</string> <string name="my_events">Meine Veranstaltungen</string> <string name="search">Suche</string>
Then you can use it in the app like this anywhere you need to use the string. This is an example of putting the options menu in the toolbar in Giggity app.
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(Menu.NONE, 1, 5, R.string.settings) .setShortcut('0', 's') .setIcon(R.drawable.ic_settings_white_24dp) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.add(Menu.NONE, 2, 7, R.string.add_dialog) .setShortcut('0', 'a') .setIcon(R.drawable.ic_add_white_24dp) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); return true; }
References:
- Google Android Developement Documentation – https://developer.android.com/guide/topics/resources/string-resource.html
- Google Android Developement Documentation – https://developer.android.com/guide/topics/resources/accessing-resources.html