In this article we are going to build multi-language supported app. We are going to support Gujarati language strings in our android application.
How String localisation works:
By default android considers English as primary language and loads the string from res->values->strings.xml. When you want to support another language, you need to create a values folder by appending an Hyphen and the ISO language code. You can check list of ISO codes from Wikipedia.
Different resources for different language:
English strings (default locale), /values/strings.xml
:
<resources> <string name="hello_world">Hello World!</string> </resources>
Gujarati strings (gu locale), /values-es/strings.xml
:
<resources> <string name="hello_world">હેલો વર્લ્ડ!</string> </resources>
User String resource in our app:
In source code to get any resource use this syntax : R..
Example:
// Get a string resource from your app'sResources
String hello =getResources()
.getString(R.string.hello_world); // Or supply a string resource to a method that requires a string TextView textView = new TextView(this); textView.setText(R.string.hello_world);
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:text="@string/hello_world"/>
Check gujarati language supported on device:
It is important to know that device which runs our app is able to support Gujarati language. to check that use this function:
public static boolean isSupported(Context context, String text) { final int WIDTH_PX = 200; final int HEIGHT_PX = 80; int w = WIDTH_PX, h = HEIGHT_PX; Resources resources = context.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap.Config conf = Bitmap.Config.ARGB_8888; Bitmap bitmap = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap Bitmap orig = bitmap.copy(conf, false); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.rgb(0, 0, 0)); paint.setTextSize((int) (14 * scale)); // draw text to the Canvas center Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); int x = (bitmap.getWidth() - bounds.width()) / 2; int y = (bitmap.getHeight() + bounds.height()) / 2; canvas.drawText(text, x, y, paint); boolean res = !orig.sameAs(bitmap); orig.recycle(); bitmap.recycle(); return res; }
Now to change language just use this function :
Call this method in activity: (I have spinner and on spinner’s item selected method I have wrote this)
switch (position) { case 0: Utils.changeLanguage(this, "en"); startActivity(new Intent(this, ProfileActivity.class)); finish(); break; case 1: if (isSupported(this, "વિષય")) { Utils.changeLanguage(this, "gu"); startActivity(new Intent(this, ProfileActivity.class)); finish(); } break; }
Utils.java contains this :
public static void changeLanguage(Context context, String lang) { Locale myLocale = new Locale(lang); Resources res = context.getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.setLocale(myLocale); res.updateConfiguration(conf, dm); }
Now our app is ready with Gujarati language support. You can add other language also, check iso code list and make resources for that.
Share this content: