B4A Library AHLocale library

With this library you can convert dates, timestamps, numbers and normal texts to the format your device uses. You can even translate your apps to different languages.

The AHLocale object provides many information about your devices locale settings like Currency symbol, month names, weekday names etc.

The AHTranslator object provides methods for creating multilanguage apps.

The AHDateTime object provides methods for formatting and parsing date strings. The internal DateTime object of B4A has the problem that it uses localized format identifier so it is not possible to parse a date string in the format "2011/05/19 12:15 AM" on a german device because the "AM" is "vorm." on it. With the AHDateTime object you can parse and format dates in any locale.

The AHTimeZone object provides methods to get information about timezones (like offset to UTC, DST offset etc.)

The AHDateUtils object provides methods to format dates and date ranges and display them localized.

The AHNumeric object provides methods to handle different (localized) number formats.

The example app starts always in english. Translation files for german, italian and japanese are provided. Try to select another language with the topmost spinner and see how the language of the user interface changes.


History:
V1.1:
  • fixed Documentation
  • fixed some bugs (DisplayXXX methods)
  • new AHDateTime object
  • some minor improvements

V1.11:
  • fixed problem with AvailableLocales not working on all devices

V1.12:
  • new AHTimeZone object to get information from timezones

V1.13: (never released)
  • Add TimeZone property to AHDateTime object

V1.14:
  • AHTranslator.java - Removed log output
  • Timezone.java - fixed type

V1.15:
  • AHDateUtils object - new
  • AHNumeric object - new
V1.16:
  • AHDateTime - Methods for getting default date and time format strings
  • AHTranslation - Support for Countries
  • AHTranslation - Support for default language
  • AHTranslation - Support for different file extensions
 

Attachments

  • LocaleExample.zip
    8.3 KB · Views: 3,814
  • TimeZoneExample.zip
    5.7 KB · Views: 1,950
  • DateUtilsExample.zip
    6.1 KB · Views: 1,924
  • AHLocale1_16.zip
    17.3 KB · Views: 3,940
Last edited:

corwin42

Expert
Licensed User
Longtime User
AHTimeZone.AvailableIds returns a lot of zones, but ...
is posible to sort then by TimeOffset?

I think AHTimeZone.AvailableIds is s list, and then is posible to sort with sortType?

AHTimeZone.AvailableIds is just a wrapper to the TimeZone.getAvailableIDs() method. So there is no possibility to change the sort order in the library.
 

wimpie3

Well-Known Member
Licensed User
Longtime User
Can you please modify the library to accept also ".properties" files as the source? I'm using a java properties editor which writes .properties files. They cannot be read by AHLocale because of the file extension used.
 

wimpie3

Well-Known Member
Licensed User
Longtime User
The files cannot be opened in my resource editor when they don't have a ".properties" extension. I know I can rename the files every time I want to modify them, but why make life harder when it's (probably) a simple modification in AHLOCALE?
 

corwin42

Expert
Licensed User
Longtime User
Updated the library to V1.16
  • AHDateTime - Methods for getting default date and time format strings
  • AHTranslation - Support for Countries
  • AHTranslation - Support for default language
  • AHTranslation - Support for different file extensions
For the default language and file extension just set AHTranslator.Extension or AHTranslator.DefaultLanguage prior to initialize the Translator object and it will be initialized with the given options.

With the DefaultLanguage option you can create translations with real placeholder for the strings. Create a translation file with all translations in your default language ("en" for example). This translation file is loaded prior to other translation files. So if english is your default language you should supply a translation file for english which includes ALL translated strings. If your app then runs in german but your german translation file does not contain all strings, the english translation will be used if no german translation exists.

I think support for Countries needs a bit more testing. Translation files can be named xxxx_en_us.lng for example
 
Last edited:

corwin42

Expert
Licensed User
Longtime User
How to change Application Name for different languages ?
What do you want to do? The basename is a parameter to the initialize method.
 

enrico

Active Member
Licensed User
Longtime User
Is it not possible that an app is installed with a different name if the device language is different ?
 

corwin42

Expert
Licensed User
Longtime User
Is it not possible that an app is installed with a different name if the device language is different ?
If you want to translate your application name you have to use the "Android like" solution explained here
 

Richard Goh

Active Member
Licensed User
Longtime User
how can i add a new language file into the program and where to set it? i had created a new language file with "localeexample_zh.lng" and added the file into b4a. it's doesn't set the label text set in the file when I run the sample program and select the language with zh. where it's go wrong?
attached is the lng file. but i renamed it to .txt due to not a valid upload file for ".lng". pls advice. thank you.
 

Attachments

  • localeexample_zh.txt
    790 bytes · Views: 274

corwin42

Expert
Licensed User
Longtime User
Your file is UTF16 with DOS CR/LF encoding. Use UTF8 with Unix CR/LF.
You have to use unicode notation if you want to use non ASCII characters.
A online converter can be found here.
 

leitor79

Active Member
Licensed User
Longtime User
Hi!

Could someone explain the file format? I have this, from the example:


Although I could copy the format, I'd like to understand why the \ and the \:

Thank you!
 

corwin42

Expert
Licensed User
Longtime User
Could someone explain the file format? I have this, from the example:

Although I could copy the format, I'd like to understand why the \ and the \:

It is a standard java properties file. See here for more information
 

Richard Goh

Active Member
Licensed User
Longtime User
Hi!

Could someone explain the file format? I have this, from the example:



Although I could copy the format, I'd like to understand why the \ and the \:

Thank you!

I think it's an escape code used for space and some special characters... it's will not work without "\" is placed in.
 

Richard Goh

Active Member
Licensed User
Longtime User
I managed to translate the file to the language I required. Just for a reference to those like me facing the same problem.
Below is the steps I do for my translation. I did it in windows.
1. create or copy the sample file from the sample program and name the file to your language with the language code.
eg. Language_zh.lng (chinese)

2. add in the word or sentense to translate.
eg. Login\ Password=

3. translate the words to the language you required. (I used google translator)
eg. Login\ Password=登录密码

4. translate the language to java unicode by using the link provide by corwin42 (http://itpro.cz/juniconv/) and replace with the unicode.
eg. Login\ Password=\u767B\u5F55\u5BC6\u7801

5. then convert your language file to unix format using dos2unix programs. Can be found at http://dos2unix.sourceforge.net/

6. then add the tranlate function into the program. Please refer to the sample program.
trans.Initialize2(File.DirAssets, "Language", locale.Language)
passwordLabel.Text = trans.GetText("Login Password")
Note:
Remember to use "\" for space between the search words.
It's a case sensitive so the words must be match exactly.
This is what I had discover during this task. Please correct me if I am wrong.

Thank you.
 

Informatix

Expert
Licensed User
Longtime User
There's an issue with the map returned by AHTranslator. The order of keys is random compared to the original file because you use ReadMap (which mixes them for an obscure reason). It's a real problem when you want to display a menu or a list. You cannot use the order returned by your lib and have to sort the map yourself (which is not quick and easy because there's no sort function for maps and, in some cases, you don't want to sort, just to display things in a certain order). I'm currently in a case where I have to keep a pair of lines together, which is impossible, so it would be nice to change the way you convert files into maps. I can write the Java function if you wish.
 
Last edited:

corwin42

Expert
Licensed User
Longtime User

I don't really understand why you need the entries in a sorted order. You normally should get the strings with the GetText methods. I never use the translationmaps in my apps directly. They are intended for creating a translation file in development time.

But if you really need them in sorted order just write the Java function and I will add it.
 

Informatix

Expert
Licensed User
Longtime User
Suppose that you have a variable menu depending on the state of the app or the user profile (e.g. in some cases, you read menu1_fr.lng, in other cases, you read menu2_fr.lng or menu3_fr.lng for a given menu). You want to display the menu in the exact order of the entries. You have to get the map values by index, not by key. In my particular case, the file content can change during the application life so I have no idea of the number of lines and keys that it contains when I want to display the menu.
That being said, I finally solved my problem by writing my own routine in Basic4Android:
B4X:
Private Sub DecoderFichierTexte(Fichier As String, CodeLangue As String, TradMap As Map)
    Fichier = "trad/" & Fichier.ToLowerCase & "_" & CodeLangue.ToLowerCase & ".lng"
    Dim Reader As TextReader
    Reader.Initialize2(File.OpenInput(File.DirAssets, Fichier), "ISO-8859-1")
    TradMap.Initialize
    Dim S As String, PosEgal As Int
    S = Reader.ReadLine
    Do While S <> Null
        PosEgal = S.IndexOf("=")
        If PosEgal = -1 Then Continue
        TradMap.Put(S.SubString2(0, PosEgal), S.SubString(PosEgal + 1))
        S = Reader.ReadLine
    Loop
    Reader.Close
End Sub
Another problem (that I solved in the above code) is the encoding. ReadMap does not seem to support UTF-8.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…