Find system language to localize

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
trevix
Posts: 408
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Find system language to localize

Post by trevix » Thu Mar 15, 2018 8:09 pm

I have been using this script on opening, in order to find out what is the preferred user language, on desktop:

Code: Select all

function getUserLang
   local tAppLangs = "en,fr,de,es,it,pt,zh,zh,zh,zh,ja,ko,ru"      -->  LANGUAGE CODES THAT WILL BE RETURNED BY FUNCTION
   local tAppLangsMac = "en,fr,de,es,it-IT,pt,zh-Hans,zh-Hant,zh_TW,zh_CN,ja,ko,ru"   --> LANGUAGE CODES THAT MAC OS RETURNS
   local tAppLangsWin = "9,12,7,10,16,22,4,4,4,4,17,18,25"     -->  DECIMAL REPRESENTATIONS OF PRIMARY LANGUAGE CODES WINDOWS RETURNS
   local tUserLangs
   local i
   set wholeMatches to true
   if platform() is "MacOS" then
      put replaceText(shell("defaults read NSGlobalDomain AppleLanguages"),"(\s|\(|\))","") into tUserLangs
      replace quote with "" in tUserLangs
      repeat for each item tLang in tUserLangs
         put itemOffset(tLang, tAppLangsMac) into i
         if i > 0 then exit repeat
      end repeat
   else if platform() is "Win32" then
      put queryRegistry("HKEY_CURRENT_USER\Control Panel\International\Locale") into tUserLangs
      put baseConvert(tUserLangs,16,10) bitAND 1023 into tUserLangs
      repeat for each item tLang in tUserLangs
         put itemOffset(tLang, tAppLangsWin) into i
         if i > 0 then exit repeat
      end repeat
   end if
   if i > 0 then 
      return item i of tAppLangs
   else 
      return "en"
   end if
end getUserLang
I feel that there should be a IDE function to easily return this, like, I thing, mobilePreferredLanguages() does for mobile.
Is there? Is any system var that can be used? If not it would be a great addition...
Regards
Trevix

Klaus
Posts: 10743
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Find system language to localize

Post by Klaus » Thu Mar 15, 2018 10:48 pm

Unfortunatley no IDE function for this.

But this sounds like a good candidate for an enhancement request:
http://quality.livecode.com :D

trevix
Posts: 408
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Find system language to localize

Post by trevix » Fri Mar 16, 2018 10:48 am

Done (21075).
I spend more time in the LC quality center, then in developing my stuff...sigh
Trevix

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3061
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Find system language to localize

Post by richmond62 » Fri Mar 16, 2018 7:12 pm

I'm going to be "nasty" about languages . . .

Consider:

1. I run several Polycarbonate iMacs running either Mac OS 10.6 or 10.7 that have, as their system language "English" (presumably US English, although I've never bothered to find out), but with a selection of Keyboard layouts (including, among others, Bulgarian).
sysPrefsX.png
There are 2 "things" (I've outlined them in red) where one can fiddle around with languages:

The "Language & Text" one allows one to set up a sort of hierarchy of languages
one might prefer (never used by me).

The "keyboard" one allows one to activate multiple keyboard layouts.

How in "the hairy Mary" is LiveCode going to get info about a hierarchy of preferred languages?

2. I run a large number of generic PCs running various distros and recensions of those distros of Linux:
again, one can "fa*t around" with language settings left, right and centre.

trevix
Posts: 408
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Find system language to localize

Post by trevix » Fri Mar 16, 2018 7:43 pm

Apparently, according to Panos, is not that difficult:
http://quality.livecode.com/show_bug.cgi?id=21075

I don't understand just why LC has not this built in (beside, in LC 8.1.9 doesn't work)

As for the OSX keyboard language, I am not sure of how this matter.
Trevix

golife
Posts: 25
Joined: Fri Apr 02, 2010 12:10 pm

Re: Find system language to localize

Post by golife » Thu Feb 28, 2019 6:31 pm

Well, on Windows 10 it is possible to retrieve the strings of a hierarchy of languages installed by the user/admin. The first line is just a priority. I am not sure about earlier Windows version since they all have been deprecated and are no longer well supported by Microsoft (including Windows 7).

Code: Select all

put queryregistry("HKEY_CURRENT_USER\Control Panel\International\User Profile\Languages") into temp
// or put textdecode (temp, "UTF-8") into msg
The result is a string of language codes that do not appear to be separate items if more than one language is supported. There is is a hidden character separating them. The hidden separator is codepoint(0).

So, easy as it is, you get all language in Window using the following script. Since text is coming from outside of LiveCode, I text-decode it anyway, even though not necessary here.

Code: Select all

on mouseUp
 local temp
  if the platform is "Win32" then
     put queryregistry("HKEY_CURRENT_USER\Control Panel\International\User Profile\Languages") into temp
     put textdecode (temp,"UTF-8") into temp -- not really needed, but text comes from outside LiveCode
     -- replace numtochar(0) with CR in temp -- do not use, it is deprecated but works also
     replace numtoCodePoint (0) with CR in temp
     put temp into msg -- or any field...
  end if --x
end mouseUp
So, if the first line is "de-CH" as in my environment then this means that there is a priority system setting with the Swiss-German language. You may receive "en_GB" for British English, "en-US" for US English, "ru" for Russian, etc.

But -- I am looking for more specific user settings such as keyboard layout and language, number formats, date string, etc. The user settings do not necessarily represent default settings of the system. So, how to know about such specific settings for internationalization of a LiveCode app? The language string is not enough.

Any suggestions for how to get all these data without having to go into C coding and creating a widget? I am not up to LiveCode Builder yet. I will also be searching around if there are other keys in the Windows registry or Shell() commands.

Golive

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”