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:

MitchBu

Well-Known Member
Licensed User
Longtime User
I just tried this to detect the language of the device :
B4X:
Log("display language " & locale.DisplayLanguage)
Log("language " & locale.Language)

I then switched the device to French, but still I get English and En when I run in B4Bridge. Is it normal ?
 

corwin42

Expert
Licensed User
Longtime User

How did you initialize the locale object?
 

MitchBu

Well-Known Member
Licensed User
Longtime User
My bad. I had not noticed that locale was initialized as locale.initializeUS

Now this works and detects language as expected :
B4X:
Dim zlocale As AHLocale
zlocale.Initialize
Log("display language " & zlocale.DisplayLanguage)
Log("language " & zlocale.Language)

Thank you
 

uorhun

Member
Licensed User
Longtime User
Is there a way to read the locale that's already set on a device? e.g. GetCurrentLocale
 

corwin42

Expert
Licensed User
Longtime User

erosolmi

Member
Licensed User
Longtime User
Hi,

I'm using AHTranslator but seems to have some problems with accents.
Attached a screenshot of my file (cs_CZ) and how it it on screen.
File is UTF8

I'm the only one with this problem?
I'm using AHLocale 1_16 and it works very well for numbers and other stuff.

Thanks a lot
Eros
 

Attachments

  • VAT_CS.PNG
    68.7 KB · Views: 296

corwin42

Expert
Licensed User
Longtime User

Arf

Well-Known Member
Licensed User
Longtime User
Also, probably a dumb question but..

I want to split a string onto two lines. My orginal string (before adding AHtranslator) was this:
guideText = "Connect unit in order to view" & CRLF &"existing calibration details"

Can I have the entire line in the translation file like this:
Connect unit in order to view" & CRLF &"existing calibration details
and get the translated line like this
guideText = trans.GetText( "Connect unit in order to view" & CRLF &"existing calibration details")

or what is the best way to handle this?
Thanks
 

Arf

Well-Known Member
Licensed User
Longtime User

got it:
Disable the virtual assets folder feature. Add this line to the main activity: #DebuggerForceStandardAssets: true

works in debug now. I still need to solve the question above though.
 

Arf

Well-Known Member
Licensed User
Longtime User
If I put a colon at the end of the string (for example, "HEADING:"), AHtranslator doesn't find any translations and just returns the english string.
Is there any way I can work around this?
 

Kevin

Well-Known Member
Licensed User
Longtime User
I recently decided to add translations to one of my apps. Inspired by the awesome library from Markus and the tool that Straker provided, I decided to make another tool in Excel using VBA. In order to use it, you must have MS Excel.

You enter the words or phrases you want to translate into one of the sheets, then with a couple mouse clicks, it will automatically retrieve translations using Google's translation service (website). Essentially it scrapes the results out of the resulting webpage. The files generated are escaped unicode - so they are unreadable by humans, but it also generates a language map file in the source language that you can use for reference. Google doesn't always get translations right but you can manually edit the lines in the workbook if necessary if you have a better translation and the workbook will leave it alone when retrieving translations.

I hope someone finds this useful. It's about as easy as it gets for translating into other languages.

Edit to add that since there is no way to 'import' files into this tool, I'd recommend using a separate copy of the Excel workbook for each app. This way you could easily add a couple more lines down the road without the need to start over from scratch.
 

Attachments

  • Capture2.PNG
    56 KB · Views: 259
  • Capture1.PNG
    76.4 KB · Views: 275
  • Capture3.PNG
    56.8 KB · Views: 266
  • Capture4.PNG
    60.9 KB · Views: 266
  • Excel Translation Builder.zip
    73.4 KB · Views: 282
Last edited:

JackKirk

Well-Known Member
Licensed User
Longtime User
I seem to have a little problem...
B4X:
Private Obj_locale As AHLocale


'Initialize locale to default
Obj_locale.Initialize
   
'Get language used in device (in English)
Private wrk_language As String = Obj_locale.EnglishLanguage.ToLowerCase

Log(wrk_language)
If I run this code fragment on Android 6.0.1 it works correctly - wrk_language is the language set via Settings>Language and Input.

If I run it on Android 7 with the language setting panel as follows:

That is with Japanese the default.

I get wrk_language is english, not japanese.

Any help appreciated...
 
Last edited:

JackKirk

Well-Known Member
Licensed User
Longtime User
Fixed my problem of previous post.

Place code such as this:

B4X:
   CreateResource(values-xx, strings.xml,
<resources>
     <string name="app_label">Flag to signal app supports xxxx language</string>
</resources>
)

In Manifest, where xx is the ISO_639-1 2 char form of the language name.

This informs Android 7 and above that the app supports the particular language, preventing it from dropping down the language list.

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