Android Tutorial LayoutView: Automatic translation of layout views

Introduction

Internationalizing your app is nowadays the #1 task. Localized content if always preferred. LayoutView makes translating your layout views automatic and hassle free.

This tutorial is available in the latest XtraViews package and as standalone APK


How to setup the translation procedure

The only thing you have to do actually, is to include the appropriate resource files in your project.
LayoutView will automatically translate any views whose layout names are found in the strings.xml resource file.

upload_2014-7-22_7-18-8.png
upload_2014-7-22_7-18-23.png



Actually there is nothing to do. Any layout that is loaded with LayoutView, will be auto translated
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim Layout As LayoutView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    Layout.LoadLayout("Layout1") 'Here is where the automatic translation happens
End Sub


And this is how the strings.xml looks like:
B4X:
<resources>
    <string name="button1"><![CDATA[label<b><font color=red>ONE</font></b>]]></string>
    <string name="button2">labelTWO</string>
    <string name="button3">labelTHREE</string>
    <string name="button4">labelFOUR</string>
    <string name="BtnChangeButtons">translated: change labels</string>
</resources>

Currently auto-translated views: Button, Label, CheckBox, RadioButton

Please note that you can use HTML formatting surrounded by a CDATA (see button1 node).

But, if there are cases where you must manually use the translation feature, you can call:
B4X:
Activity.Title = Layout.GetTranslation("activity_title")
Activity.Title = Layout.GetTranslationDefault("activity_title", "title_if_translation_not_found")


How to create the translation resource files

Put the default texts in a file with the following location and name:

project-root\objects\res\values\strings.xml (mark as read only)

The text strings in strings.xml should use the default language, which is the language that you expect most of your application's users to speak.

Example:

Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French, and most of the text in your application (everything except the application's title) to Japanese. In this case, you could create three alternative strings.xml files, each stored in a locale-specific resource directory:

project-root\objects\res\values\strings.xml
contains english text for all the strings that the application uses, including text for a string named title.

project-root\objects\res\values-fr\strings.xml
contains french text for all the strings, including title.

project-root\objects\res\values-ja\strings.xml
contains japanese text for all the strings except title.

--
Related tutorials:

That's all for now folks! :D
 
Last edited:

BaGRoS

Active Member
Licensed User
Longtime User
I start using
B4X:
Layout.LoadLayout("Moja")
without translations, but
B4X:
Layout.LoadLayout("Moja")
Layout.Button("btnExit").text = Layout.GetTranslation("btnExit")
give me tranlated button.
Maybe You know why?
 
Top