B4J Question [ABMaterial] Detecting Browser (device) language

Cableguy

Expert
Licensed User
Longtime User
Hi gurus...

How can I detect the device language so that I can present my content in accordance?

I mean, if my webpage is being viewed in a English set laptop, even if it is in china, I want to present my webapp in English, or if its a French set phone, browsing from Australia, I want to show in French!

Page.PageLanguage seem to always give "en" as result!!!
 

mindful

Active Member
Licensed User
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Hi, you need to have a look at https://www.b4x.com/android/forum/t...calization-using-dynamic-pages.65825/#content

code from WebSocket_Connected after page.Prepare is called:
B4X:
page.SetAcceptedLanguages(ArrayAsString("en", "fr", "de", "nl"), "en")
Dim ActiveFoundLanguage AsString = page.DetectLanguage(ws.UpgradeRequest.GetHeader("Accept-Language"))
page.SetActiveLanguage(ActiveFoundLanguage, "" )
I followed the tutorial, but since it was based on v1.09, i didn't find page_create() anywhere.
So i used the code in page_build(), also tried in the websocket_connect()...
Logging activefoundlanguage produces the results as expected... is, en in my desktop, fr in my phone, but i just can't figure out were the ABM.loadtranslations line should go, so i always get the default text i set when using the page.xtr method...
So... I can detect the browser language... But how/where do i load the translation files?
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
This is how I use it in my apps:

B4X:
Sub ConnectPage()
   page.SetAcceptedLanguages(Array As String("en", "fr", "de", "nl"), "en")
   ABM.LoadTranslations(File.DirApp & "/www/" & ABMShared.AppName & "/translations/")

   Dim ActiveFoundLanguage As String = page.DetectLanguage(ws.UpgradeRequest.GetHeader("Accept-Language"))
   page.SetActiveLanguage(ActiveFoundLanguage, "" )
   ...

Also, make sure the folder /translations/ does contain the translations.
 
Upvote 0

mindful

Active Member
Licensed User
You can call ABM.LoadTranslations from the StartServer method in your ABMApplication class - this way it loads the translations only when you start the server.

@alwaysbusy if we place ABM.LoadTranslations in ConnectPage of every page would it not load the translations on every request for that page ?
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
How can I detect the device language so that I can present my content in accordance?
Paulo, whatever you do, please always give the user the option to manually set its own language settings.
You have no idea how much I hate NVIDIA for not allowing me to change its Android app to English.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Paulo, whatever you do, please always give the user the option to manually set its own language settings.
You have no idea how much I hate NVIDIA for not allowing me to change its Android app to English.
In medium/large screens (tablet/desktop) the user will be able to choose from 3 languages, but on small (phone) screens, the language will be automatically set to the device language...
I took my own experience in account... My phone is set to English, the wife's set to French... And my pc is set to Portuguese lol
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
but on small (phone) screens, the language will be automatically set
I see no relation between screen size and language settings... In my opinion the user should always have the final word.

Imagine my phone is set to Portuguese but I'd like to show something to my Norwegian friend sitting next to me...

Wouldn't it be way simpler and faster changing the app's language?

Of course, by all means, you can always keep auto-detection... but without enforcing it.

B4X:
Language settings:
[*] Auto-detect
[ ] English
[ ] Français
[ ] Português
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
For the webapp in question, it will hardly apply... It will be my WebCV... So showing to the friend beside you will be very uncommon... Moreover, i intend to share my app using a at code... So, at best, you would simply need to either show the we code to your friend so that he can open the webapp in his device, or share the direct link...

I will consider your suggestion, thou...
At the end, it is a matter of screen real-estate.... I might implement an action button for it...
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
This is how I use it in my apps:

B4X:
Sub ConnectPage()
   page.SetAcceptedLanguages(Array As String("en", "fr", "de", "nl"), "en")
   ABM.LoadTranslations(File.DirApp & "/www/" & ABMShared.AppName & "/translations/")

   Dim ActiveFoundLanguage As String = page.DetectLanguage(ws.UpgradeRequest.GetHeader("Accept-Language"))
   page.SetActiveLanguage(ActiveFoundLanguage, "" )
   ...

Also, make sure the folder /translations/ does contain the translations.

My translations folder contains 3 files, I have placed and tried the code lines suggested, plus some mutations....
I can log the "ActiveFoundLanguage", resulting in "fr", "pt" or "en" according to which of the device I use, but the translation file seem to never be loaded!
I'm going crazy about this one! I might just create a "translations" code module and use the ActiveFoundLanguase in a select case!
 
Upvote 0

mindful

Active Member
Licensed User
@Cableguy are you running in Windows ? Because i think creating the path like: File.DirApp & "/www/" & ABMShared.AppName & "/translations/" will not work on linux. Please post BaseTranslations.lng file (it's created in the File.DirApp) and one language files you created (located in File.DirApp & "/www/" & ABMShared.AppName & "/translations/") so we can test. I run my project with 3 languages with no problem.

Also you should place the ABM.LoadTranslations call in the StartServer method, before srvr.Start
@mindful Yes, just checked the code and it does reload it on every request. Thought I had a check if it wasn't already loaded, but I don't.

So your suggestion of only putting it in StartServer is a valid one!

Did you create the 3 language files fr.lng, pt.lng, en.lng based on BaseTranslations.lng ?
I can log the "ActiveFoundLanguage", resulting in "fr", "pt" or "en" according to which of the device I use, but the translation file seem to never be loaded!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Hi @mindful
Thanks for taking the time to help me...
For time being, and since I will have a not so big number of translation to do, I have created a "translations" code module that works almost as I suspect ABM.loadtranslations does.
I know that doing so, every time I will need to make a text change, it will force me to recompile my webapp, but for time being, I can live with that.
I am taking this webapp as my learning curve, for ABM Basics, and so far, I have learnt a lot!
 
Upvote 0
Top