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,676
  • TimeZoneExample.zip
    5.7 KB · Views: 1,824
  • DateUtilsExample.zip
    6.1 KB · Views: 1,789
  • AHLocale1_16.zip
    17.3 KB · Views: 3,743
Last edited:

moster67

Expert
Licensed User
Longtime User
Very nice Markus. :icon_clap:

I just tried the example-project, changing the language on the emulator and then running your application and noted it works very well (for instance the variables from English into German and vice versa).

The AHLocale is also very useful to get local information about the device.

Thank you very much for this useful library. I hope Erel will put it online (of course with your permission) among the other libraries.
 

moster67

Expert
Licensed User
Longtime User
Marcus,

one question: in your German language-file I noted that you used:

B4X:
u00DC
to get Ü

but I noted that if I wrote Ü directly in the file, it worked anyway.

I also tried with other characters such as ö å ä ò è and they all showed up correctly.

Do you suggest using Unicodes? (Unicode Escape I believe it is called)

PS: I added an Italian language-file and the application changed languages nicely between English, German and Italian according to the language set in the emulator. Nice job. Thanks!
 
Last edited:

moster67

Expert
Licensed User
Longtime User
I played again with the library and the language-file.

Just for fun, I tried Japanese although I don't know nothing about Japanese. I used the same words as in your example and then translated them into Japanese using Google-translation tools. I saved the file as UTF-8 using "ja" as the language-attribute in the file-name.

I set Japanese as language in the emulator. The application detects Japanese but it seems like the characters are not shown correctly. Do you know why this happens?

I attach an image and the exported zip-project. The Japanese language-file is in the Files-folder.
 

Attachments

  • device.jpg
    10.7 KB · Views: 1,406
  • LocalExample_IT_JA.zip
    8 KB · Views: 674

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Moster67

I have tried it on my Japanese phone and it looks very much like in your emulator.

Also experimented with several encodings for the localeexample_ja file, SHIFT_JIS, JIS, EUC, UTF-8, however, without success. I need to get familiar with locale settings before I can do more.
 

Attachments

  • ja1.jpg
    10.8 KB · Views: 803
  • ja2.jpg
    10.9 KB · Views: 856

moster67

Expert
Licensed User
Longtime User
Maybe we need to use Unicode Escape? I was looking for an online-converter but I coulnd't find one. I will try again tomorrow.

 

corwin42

Expert
Licensed User
Longtime User
I tried it yesterday with no success too. I even tried Unicode escape sequences with the same result.
One idea I haven't tried yet: Add a translation manually to the translation map using japanese weekdayname or monthname and write it to SD - card with file.writemap or trans.writetranslation to see what he generates.
 

agraham

Expert
Licensed User
Longtime User
Someone may have tried this but check that a phrase that does not display properly with the translator does display when directly added to a Label or EditText. If it then displays properly it is an encoding problem, if it does not then it is probably a font problem.
 

moster67

Expert
Licensed User
Longtime User
Good suggestion. I tried that by insering a label called myJapaneseLabel and then used the following code in the source-code:

myJapaneseLabel.Text="週の最初の曜日"

The Japanese characters are displayed correctly so I guess it is an encoding problem at this point.

 

moster67

Expert
Licensed User
Longtime User
Marcus,

I don't know JAVA very well but I guess you are using map for storing data in your library. When you read the file, perhaps there is an option, when dealing with strings, how to deal with encoding.

In VB.net, I believe that with certain string-objects/methods you can set parameters as to the encoding. If I recall, you can tell it to use UTF-8. Maybe that is the way it must be done in JAVA as well?

Just some thoughts...
 
Last edited:

agraham

Expert
Licensed User
Longtime User
The library uses a Basic4android Map to store the language strings and uses File.ReadMap to populate it which in turn uses an instance of the Java Properties class and its load method. After a bit of Googling I think this is where the problem lies. The Android documentation doesn't mention it but the Java Platform SE 6 Properties class docs do say that, I assume for historical reasons, "This format uses the ISO 8859-1 character encoding".

I suspect this may have something to do with the problem as according to that documentation Unicode characters need special escaping to be represented in this format. The Properties.store method used by File.WriteMap might well do this but if you are using an external editor that writes UTF-8 to produce the language file I guess this is causing the problem.
 

corwin42

Expert
Licensed User
Longtime User
Andrew seems to be correct with ISO8859-1 character set. I converted all japanese characters to unicode escape sequences (Used this online converter)

After you convert all japanese characters to the unicode escape codes then everything seems to work.
 

Attachments

  • LocaleExample.zip
    7.1 KB · Views: 468
  • localeexample_ja.jpg
    10.3 KB · Views: 637
Last edited:

moster67

Expert
Licensed User
Longtime User
Fantastic! Now it works! :sign0060:

Thank you very much Marcus and of course many thanks to Andrew as well for the hint to get it working.

Great job.


Edit: Oops. Did a mistake. Wait a moment. I think I have the solution.

Andrew seems to be correct with ISO8859-1 character set. I converted all japanese characters to unicode escape sequences (Used this online converter)
 

corwin42

Expert
Licensed User
Longtime User
Do you suggest using Unicodes? (Unicode Escape I believe it is called)

For japanese language: YES


:signOops:

Argh, I created a copy of the example project and edited the language files in the wrong folder. :BangHead:

But while searching for the problem I found some smaller bugs and some bad things in the documentation. I hope to find the time to fix all this very soon.
 

corwin42

Expert
Licensed User
Longtime User
Version 1.1 of this library is available in the first post.

Most interesting improvement is the AHDateTime object which provides methods for parsing and formatting dates. The difference to the builtin DateTime object is that the AHDateTime object can be used with any locale you want.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Dropped this in my library folder but it says version 1. It does not have the AHDateTime object.
 

corwin42

Expert
Licensed User
Longtime User
Dropped this in my library folder but it says version 1. It does not have the AHDateTime object.

Should be the correct version in 1st post. Did you reload libraries in B4A?
 

bluedude

Well-Known Member
Licensed User
Longtime User
Version problem

corwin,

Still have the same problem after downloading the library, it says 1.00. See attached screenshot for prove.
 

Attachments

  • ahlocale.jpg
    20.9 KB · Views: 656
Cookies are required to use this site. You must accept them to continue using the site. Learn more…