I have implemented a game using pygame-ce that has its strings associated with specifics key languages. So when running the game, I get the language code by running the function locale.getdefaultlocale()
and display the accordingly strings, otherwise it displays the default language strings. It works pretty well when I run the game in my desktop, however when running on browser with the pygbag
library the function does not work and only brings the default language.
The function I use is like this:
def _get_language():
l, e = locale.getdefaultlocale()
if os.path.exists(os.path.join(STRDIR, f'{l}.json')):
return (l, 'UTF-8')
else:
return ('en_US', 'UTF-8')
Is there a way to get this function working fine for pygbag (web) environment and also for desktop?
I thought that I could pass the language code as an argument for the python app, but I couldn't figure out how to do this by changing the index.html
file generated by pygbag. This way I would get the language code maybe using JavaScript.
I accept other working arounds too for the same problem.
If you want to get the language of the browser when using pygbag, you can investigate the platform parameters.
Accordingly to the documentation you can differentiate primarly the system platform running from browser to desktop. If the platform is "emscripten" than you can be sure that if you import "platform" there'll be almost all those information you can getter with javascript from your Browser instance. The example below is what I did to get the language code:
import sys, platform
if sys.platform == 'emscripten':
lang = platform.window.navigator.language
change_language_to(lang)