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:

sigster

Active Member
Licensed User
Longtime User
Hi

if I try this Obj1.RunStaticMethod("java.util.Locale", "getDefault", args, types
My local is = is_IS
 

corwin42

Expert
Licensed User
Longtime User
The library uses Locale.getAvailableLocales() to get a list of all available locales on the device. This complete list is added to the spinner. If your default locale is is_IS and this is not in the spinner than there is something strange on your device.
 

COBRASoft

Active Member
Licensed User
Longtime User
Corwin: any change you solve that issue I have? I posted it earlier in this thread.
 

corwin42

Expert
Licensed User
Longtime User
Error getting ICU gregorian resource bundle: U_MISSING_RESOURCE_ERROR

Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x2aac8578)

Sorry it took so long that I have a look into this.

The problem should be fixed with the 1.11 version.
BUT: If you select the th_TH_TRADITIONAL locale, you will get the error with GetFirstDayOfWeek(). It seems that some locales on your device don't have a gregorian calendar which is needed for GetFirstDayOfWeek() method. You can try to surround GetFirstDayOfWeek() with a try/catch block in B4A to solve this.
 

corwin42

Expert
Licensed User
Longtime User
Updated library to version 1.12 in first post

Because I just needed it I added a AHTimeZone object which allows you to get some information about timezones.
 

corwin42

Expert
Licensed User
Longtime User
You are right. This library is the perfect place for number format functions. It was planned to add such feature but since now I didn't need it.
I will have a look at it.

sent from my Galaxy Nexus
 

Reliable

New Member
Hey. I have a small problem. I'm trying to translate from English to Russian.
When you run your translation is not displayed correctly. The screen abracadabra ...
I understand that the problem is encoded in the file. But I tried different encodings UTF-8 and others but the result was negative.

Please tell me what to do, that the Russian language is displayed correctly
 

corwin42

Expert
Licensed User
Longtime User
You have to use unicode characters in your translation.

Write your russian translation in UTF-8 encoding and then use this online converter to convert to Unicode characters.
 

corwin42

Expert
Licensed User
Longtime User
Updated the library to version 1.15:

- AHDateUtils object. With this object you can format date and time ranges like "in 23 minutes", "midnight", "06:00 - 08:00" etc.

- AHNumeric object. With this object you can format and parse numbers.
 

airblaster

Active Member
Licensed User
Longtime User
Hi corwin42,

is it possible there is a Bug in AHTranslator that can cause an Exception?
I stored the AHTranslator object in a variable in Process_Globals in the main Activity.
Then I navigated to another activity, switched to other apps and returned to my app through long pressing the home button. When returning to my app I get the error "RuntimeException: Object should first be initialized (Map)" on a line that uses AHTranslator's gettext function.

Btw, thanks for sharing your great work!
 

Informatix

Expert
Licensed User
Longtime User

That sounds like the system freed some memory while your app was in the background. When you came back to your app, the needed data were missing in memory. Your app have to handle this case.
 

corwin42

Expert
Licensed User
Longtime User
Informatix is correct.

If the app is in the background the system may decide to kill its process completely. So in Activity_Create you have to do your initialization of the AHTranslator object again.

I normally put all global objects into one code module and add an initialization sub there which initializes the objects and sets a global variable. Then in all my activities (and most service modules) I call this initialization sub which first checks if the global variable is set to true and then just returns if it is. Otherwise the initialization is done. So I'm sure that all global structures needed by the program are always initialized correctly.
 

airblaster

Active Member
Licensed User
Longtime User
Thanks for the explanation!
Does this apply to all variables or just to certain objects?
Doesn't that mean that Process_Globals is pretty much useless apart from the one in the global variables module?
 

Informatix

Expert
Licensed User
Longtime User
Thanks for the explanation!
Does this apply to all variables or just to certain objects?

Any data referenced by a killed process. Processes can be killed when they are paused (that happens, for example, when you leave an activity).

Doesn't that mean that Process_Globals is pretty much useless apart from the one in the global variables module?

???
The Process_Globals section is there to share variables among modules. That's the main difference with Globals.
 

airblaster

Active Member
Licensed User
Longtime User
Any data referenced by a killed process. Processes can be killed when they are paused (that happens, for example, when you leave an activity).
Leaving an activity is a case I already handled. What I didn't notice is that an app can start from any activity.


???
The Process_Globals section is there to share variables among modules. That's the main difference with Globals.
What I mean is that it makes sense to put global variables in a separate module, as suggested by corwin42. Which leaves process_globals empty for most modules.
 

Firpas

Active Member
Licensed User
Longtime User
How sort AHTimeZone.AvailableIds

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?

Thanks and regards
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…