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,820
  • TimeZoneExample.zip
    5.7 KB · Views: 1,955
  • DateUtilsExample.zip
    6.1 KB · Views: 1,928
  • AHLocale1_16.zip
    17.3 KB · Views: 3,947
Last edited:

corwin42

Expert
Licensed User
Longtime User
Another problem (that I solved in the above code) is the encoding. ReadMap does not seem to support UTF-8.

Yes, the files are standard Java porperties files and they are ISO encoded and not UTF. Special characters have to be written with the unicode code.
 

Roger C

Active Member
Licensed User
Longtime User
Great library.

Just for your information, when initialized with
B4X:
locale.Initialize3("sv","SE")
the code
B4X:
ticks2 = dt.Parse("5/19/11 12:45 AM Local Time") 'This is not possible with B4A DateTime object on a german device
...does not work in Sweden.
 

corwin42

Expert
Licensed User
Longtime User
Add your images to the files folder with localized endings (xxx_en.png, xxx_de.png, xxx_it.png, xxx_es.png etc)

Initialize a AHLocale object with current locale of the system and load the file with the correct ending:

B4X:
Dim loc As AHLocale
Dim localizedFileName As String

loc.Initialize
 
localizedFileName = "flag_" & loc.Language & ".png"
 
If File.Exists(File.DirAssets, localizedFileName) Then
    'You can load the flag here to an imageview
    Log("Flag exists: " & localizedFileName)
Else
    'Load a default image here for languages not directly supported
    Log("No flag for your country")
End If

Attached is a very simple example with this code.
 

Attachments

  • LocaleFileLoad.zip
    24 KB · Views: 266

achtrade

Active Member
Licensed User
Longtime User
Hi,

If I change the app's language from English to Spanish, how the app can remember the new language ?

thanks.
 

inakigarm

Well-Known Member
Licensed User
Longtime User
Hi corwin42

Is possible to have the AHLocale Library as a code or class module ?? I'm thinking about because when we'd have B4i, porting the app (the AHLocale component) to IOS won't be possible

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is true that the source code won't be very helpful. However in many cases it is possible to implement a B4i library with the exact (or at lease similar) API. For example iStringUtils B4i library is identical to StringUtils library (except of one method which is very specific to Android).

In other cases like iHttp library, the library API is not identical however iHttpUtils2 which is built over this library in B4i exposes the same API.
 

inakigarm

Well-Known Member
Licensed User
Longtime User
This library and a lot of libraries available for B4A are written in Java, not in B4A. Their source code won't help for B4i.

Ok thanks

I'll search an alternative method (maybe creating a file with keys and words in different languages or using somewhat like http://www.b4x.com/android/forum/threads/localize-your-app-using-androidresources.19165/ -almost I'll can use the xml strings)

Another possibility is create something like Po files ..(http://en.wikipedia.org/wiki/Gettext)
 

Pavka

Member
Licensed User
Longtime User
Can someone tell me how to change tabhost tabs names and listview items names with translated ones? Can it be done without destroying and creating these views again? Thank you.
 

Dadeda

Member
Licensed User
Longtime User
Yes, the files are standard Java porperties files and they are ISO encoded and not UTF. Special characters have to be written with the unicode code.

Will it ever be possible to use UTF8?, i really want to use this for my app, but it's a horrible job for anyone translating, they need to translate something into another language and then they need to make unreadable with unicode, it simply doesn't work for use with professional translators.
 

corwin42

Expert
Licensed User
Longtime User

This is a Java restriction so I can't do anything to change this.
 

ivan.tellez

Active Member
Licensed User
Longtime User
it simply doesn't work for use with professional translators.

That is not the job of the translator.

They just have to deliver the translated text and the developer has to make the encofing. So, what is the problem? There are online encoders that make the job on a sec.
 

Straker

Active Member
Licensed User
Longtime User
Just to avoid some of the restriction of the format I've made a little utility.
With this utility you can have all your translation in a Spreadsheet, one column for each language.
The file should then be saved in the CSV format. The csv is converted in unicode escaped through the jdk's native2ascii utility and then there is my utility that parse the final file and builds the corresponding translation files.
This way you have to update a single file with all the translations.

I didn't try this utility extensively, so if you find some bug just let me know. Also, let me know if you think there is something else to add (just like a b4a files parser, for example).
In the attachment there are also the VB6 source code

If you find this utility usefull, please donate to corwin42!
 

Attachments

  • th.zip
    15.7 KB · Views: 320

corwin42

Expert
Licensed User
Longtime User

Franck Danard

Member
Licensed User
Longtime User
Hello all.
I'm new with B4A
and i would like to take the language into my smartphone.

I installed the library AHLocale and i called it like this:

B4X:
Sub Activity_Create(FirstTime As Boolean)
  'Do not forget to load the layout file created with the visual designer. For example:
  'Activity.LoadLayout("Layout1")  
  Activity.LoadLayout("Main")

  Dim Mylang As String
  Dim AHLocale1 As AHLocale
 
  AHLocale1.Initialize
 
  Mylang = AHLocale1.DisplayLanguage
However at the line: Dim Mylang As String , the IDE says : Unused variable "Mylang" (Warning #9).

I don't understand, because i try to declaring my variable Mylang with this line.
And Mylang is setted bellow.

Mylang should include my language code, right?

What happen please?

Thanks for your help.

Franck
 

Cableguy

Expert
Licensed User
Longtime User
do a log of MyLang, setting a var value is not the same as using it!!
Var = value is not the same as OtherVar = FirstVar.

The IDE is just telling you that you didn't use the MyLang var
 

Franck Danard

Member
Licensed User
Longtime User
Hi.

Hmmm ok i see.
Indeed, if i add :

B4X:
  If Mylang = "" Then
     ' Do something
   
   End If
No error.

All was right in my code, but it was not really completed.
I understand now.

Ok good.

Thanks for all

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