B4A Library [Class] Translation

Hi!

As i got so many help from this forum, i thought that I have to give something back :)
This is a small class that will help you to translate you app. It makes use of the strings.xml file located in your res/values folder.

You can use the strings.xml file with services like getlocalisation.com
This class needs Reflection library!


B4X:
'USAGE:
'------
'You need at least one "strings.xml" - file in the Folder "objects" - "res" - "values"
'Remember to to set this file to read only!!

'To add another language, create a new folder in the "res" directory, named "values-" and the 2 letter abbreviation of the language
'You can find the 2letter abbreviations here: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
'Example: "values-de" for german. In this directory, place the localized "strings.xml" - File (Full path: objects/res/values-de/strings.xml"
'Tipp: Services like http://www.getlocalization.com/ can use this xml - files :)

'Sample Layout of the "strings.xml" file:
'<?xml version="1.0" encoding=\"utf-8\"?>
'<resources>
'    <String name="app_name">ahaTranslate</String>
'</resources>

'TIPP:
'Do you want a localized AppName? Go to the Manifest-editor and replace:
'    SetApplicationAttribute(android:Label, "$LABEL$")
'with
'    SetApplicationAttribute(android:label, "@string/app_name")
'Of course, you have to define the app_name in strings.xml as shown above in the Sample Layout

'Info about Translation: http://developer.android.com/guide/topics/resources/localization.html
'http://developer.android.com/guide/topics/resources/string-resource.html
Documentation:
(done with warwound's B4A library and module reference generator.)
ahaTranslate
  • AutoTranslateActivity (Activity As Activity)
    Translates a whole activity. The Tag information of the controls is used. The tag corresponds to the name in the strings.xml file
    Supported: label, button, checkbox, edittext, radiobutton, togglebutton, spinner
    ATTN: ToggleButtons have to states. ON and OFF. If you use "togglebuttontext" as key in the Tag-Property, you must have 2 Strings in the strings.xml file:
    togglebuttontextON and togglebuttontextOFF!
  • GetDeviceDateFormatSettings As String
    Gets the phone Date Settings' returns "MM/dd/yyyy" on error
  • GetPhoneCurrencySymbol As String
    Gets the phone currency Symbol. If it's not found, $ is returned
  • GetPhoneLanguage As String
    Gets the phone language and returns the 2-Letter Code (en for english, de for German..)
    If language cannnot be determined, 'en' is returned
    Returned string is lowercase
  • GetResourceBitmap (Key As String) As Bitmap
    Loads the drawable from the ressources directory (res/drawable) and returnes a bitmap. Use res/drawable-de etc for localisation
  • GetResourceString (Key As String) As String
    Gets the translation for a specific key
    If no localized String is found, the appropiate String from "values/strings.xml" is is used.
    If the Key doesn't exists, it returns "No translation found!"
  • GetResourceStringArray (Key As String))
    Gets an StringArray
    If no localized StringArray is found, the appropiate StringArray from "values/strings.xml" is is used.
    If the Key doesn't exists, it returns "No translation found!"
  • GetResourceStringPlural (Key As String, Count As Int) As String
    Gets localized Plurals. You have to pass the Count. For example:
    count = 1: Returns day
    count =>2: Returns days..
    See String Resources | Android Developers => Quantity Strings for more information
  • GetResourceStringWithParams (Key As String, Params As List) As String
    Gets the translation for a specific key with the use od Parameters.
    string.xml: <string name="Hello">Hi {1} {2}!</string>
    B4A: GetResourceStringWithParams("Hello",Array As String("World",":)")
    => Returnes: Hi World :)
    If no localized String is found, the appropiate String from "values/strings.xml" is is used.
    If the Key doesn't exists, it returns "No translation found!"
In the attached file, there are 2 example activities and the class .

Have fun, I hope you like it :)

-----
Version 1.2
Added: New GetResourceDrawable (Key As String) As Object
Loads the drawable from the ressources directory (res/drawable) . Use res/drawable-de etc for localisation
(Works for 9patch images as well)
Version 1.3
Added: GetAndroidDrawable
 

Attachments

  • ahaTranslateLibrary.zip
    7 KB · Views: 339
  • ahaTranslate.zip
    18.1 KB · Views: 334
Last edited:

GeeKay

Member
Licensed User
Longtime User
Old version?

Hi RiverRaid!

It sounds good. But it seems to be an old version you attached. Subs like AutoTranslateActivity, GetResourceStringPlural and others aren't in the code. :(
Could you check it?
Thx
 

RiverRaid

Active Member
Licensed User
Longtime User
I'm sorry :sign0148: you are right...

Now attached the Version 1.2 which has a new sub GetResourceDrawable.

Hope you like it :sign0162:
 

Attachments

  • ahaTranslate.zip
    18.1 KB · Views: 208
  • ahaTranslateLibrary.zip
    6.9 KB · Views: 215

Theera

Well-Known Member
Licensed User
Longtime User
I'm sorry :sign0148: you are right...

Now attached the Version 1.2 which has a new sub GetResourceDrawable.

Hope you like it :sign0162:

Hi ReverRaid,

After I have test,I have receive error message. Please help me to correct.
 

Attachments

  • error1.png
    error1.png
    22.8 KB · Views: 239

Theera

Well-Known Member
Licensed User
Longtime User
Hi!

Please try the new Version 1.3 attached in the top post :)

Hi RiverRaid,
I have tried coding,but I confuse how to coding,please guide me. I would like this widget to translate English to Thai.
 

Attachments

  • TestTranslate.zip
    6.1 KB · Views: 208

RiverRaid

Active Member
Licensed User
Longtime User
It seems that...

... you are mixing something up ;)

This class does not translate any strings automaticaly, it helps you to translate your App into different languages...

Short Tutorial:

Lets say, you want to release your app in 3 languages:
1) English (thats the "main" language, if there is no localized app in the language of the User, this language is default used
2) German
3) French

You have to create 3 xml-Files and set them to read-only:
1) strings.xml files in the folder /res/values
That is the main language File (in this case English). If the user has a language selected in the phone which is not supported by your app until now, this language is used.
You can open this file with a text-editor ore notepad++ and copy following information into it:
B4X:
<?xml version="1.0" encoding=\"utf-8\"?>
<resources>
    <String name="Button1">This is Button1</String>
</resources>

2) strings.xml files in the folder /res/values-de
B4X:
<?xml version="1.0" encoding=\"utf-8\"?>
<resources>
    <String name="Button1">Das ist Button1</String>
</resources>

2) strings.xml files in the folder /res/values-fr
B4X:
<?xml version="1.0" encoding=\"utf-8\"?>
<resources>
    <String name="Button1">"C'est Button1"</String>
</resources>
NOTE: If you use in the string any special Charater (like ') the just put the whole String in Quotes, like in the french example above

Now lets assume that you have a Button named Button1:
B4X:
Sub Activity_Create
....
Dim trans as ahaTranslate
trans.initialize
Button1.text = trans.GetResourceString("Button1")
...
End Sub


If you set your phone language (in Android System settings) to English, Button1.text will be "This is Button1". However, if you set it to German, it will be "Das ist Button1". If you select Thai as your phone language, it will fall back to English and display "This is Button1", since you have no translation for Thai until now...


If you have any further questions, don't bother to ask :)


Edit: If you are searching for a library/class that translates strings for you, this migt be helpful
Bing translator class by NeoTechni
B4ATranslator by lonleystar
 
Last edited:

Theera

Well-Known Member
Licensed User
Longtime User
Hi RiverRaid,
Thank you for your kind of you,however I 've learned your library. I need to know that how to work. I've create strings.xml for Thai as a new test.
 

RiverRaid

Active Member
Licensed User
Longtime User
Hi Theera,
Sorry, I don't understand what exactly do you need to know?

If you look at the Project in the ahaTranslate.zip, the Main Activity shows how to use the class.
 
Last edited:

Theera

Well-Known Member
Licensed User
Longtime User
Hi Theera,
Sorry, I don't understand what exactly do you need to know?

If you look at the Project in the ahaTranslate.zip, the Main Activity shows how to use the class.

Hi RiverRaid,
I'm still study the B4A more and more,because I'm hungry to learn. There are many things which I'm unknown.
 
Last edited:

SandroB4A

Member
Licensed User
Longtime User
Hi RiverRaid,
I 'd like to use your library but I get an error:

B4X:
Parsing code.                          Error
Error parsing program.
Error description: Attribute not supported: libraryname
Occurred on line: 10
#LibraryName: ahaTranslate

I'm using B4A 2.52 that don't support compile to library.

Any suggestion?

Tks in advance
 

stevel05

Expert
Licensed User
Longtime User
I don't know the Class and whether its compatible with 2.52, but you could try removing the #LibraryName line, and any others related to the Library compilation.
 

itgirl

Active Member
Licensed User
Longtime User
Hello and thanks for this great class, one question tho , what about if we want to control the string returned ?!

i mean lets say the phone locale was EN and we want for button1 the string from locale FR ? is there any way to achieve that
 
Top