How to make MicroBlocks start with the language defaulting to Chinese

Issue #434 resolved
Wenjie Wu created an issue

Some users hope that the Chinese version will start in Chinese by default and avoid initialization settings for each computer during teaching.

Comments (36)

  1. John Maloney repo owner

    Hi, Wenjie. Once you've set the language, that should be remembered in either a settings file (stand-alone versions) or in the browser. But I think you are talking about the very first startup on a new computer, right?

    I just looked at the code for this (line 943-967 in MicroBlocksEditor.gp). It looks like it is attempting to default to Chinese when it is running in the browser and the URL contains "microblocksfun.cn" or "blocks.aimaker.space". Does that case work for you?

    Are you asking for something similar work for the stand-alone apps? The might be possible if we can find a clever -- and, ideally, a platform-independent way -- to tell when the computer is using the Chinese language. There are OS specific functions for that, or course, but I'd much prefer to figure out something that doesn't require using an OS-specific API's. One idea might be to test for Chinese characters in the path to the users's Desktop. I believe there is already a primitive to get that path and we could check for Chinese Unicode characters.

  2. Wenjie Wu reporter

    But I think you are talking about the very first startup on a new computer, right?

    Are you asking for something similar work for the stand-alone apps?

    Yes! That's exactly what I want. Some teachers install MicroBlocks stand-alone apps in school computer labs, and they don't want to configure the language for each machine after installation (even though it's only a one-time configuration). So, I want to build stand-alone apps that default to Chinese, but it would be ideal to find “a platform-independent way – to tell when the computer is using the Chinese language”. This is most ideal because then we only need to work on the same version.

  3. Wenjie Wu reporter

    I believe there is already a primitive to get that path and we could check for Chinese Unicode characters.

    what should we do? I currently have a windows and a macos and can test it anytime

  4. John Maloney repo owner

    On MacOS, the full path to the user's home folder includes the user's name. Is the same true on Windows?

    If so, then when the user has not set any language preference yet -- as will be the case on a computer that has never run MicroBlocks -- the default language could be set to Chinese if the home folder path contains a Unicode character in the the range used for most Chinese characters (4E00-9FFF). There are Chinese characters in other Unicode ranges, but my guess is that most people's users names will include at least one character from the common range. Does that seem likely? (See https://stackoverflow.com/questions/1366068/whats-the-complete-range-for-chinese-characters-in-unicode).

    This is heuristic. It assumes that the user name for most Chinese computers will include Chinese characters. Does that seem likely?

    This will also result in Chinese as the default language in countries that use Chinese characters, including Korea and Japan. But my guess is that defaulting to Chinese in those countries will be no worse than defaulting to English and perhaps a bit better. And, of course, once the language has been selected on a given computer that choice is persistent.

    If this makes sense, I will implement it in the next pilot and you can let me know how it works on your computers. You'll need to delete the file "preferences.json" in your ~/Documents/MicroBlocks folder to simulate starting up on a brand new comptuer.

  5. Wenjie Wu reporter

    As far as I know, many Chinese users may use simple letters as their username, and using English when starting up seems to be the default configuration of most software. I think for most users this is no problem, the main demand seems to be in schools where teachers want an environment that works out of the box. If the heuristic method is not necessarily reliable, it may be a good choice to provide a compilation parameter(eg: ./build.sh --language cn) and provide it to the school separately.

  6. Wenjie Wu reporter

    Using English by default seems to be a common configuration for global software. I hope these special requirements can avoid affecting the main releases of MicroBlocks as much as possible.

  7. John Maloney repo owner

    Changing the default language is easy, but updating and distributing customized versions of the stand-alone apps is a lot of extra work.

    Is that something that you could handle?

    The job would involve making a small change to one of the .gp files, then running a shell script to build the three stand-alone apps and their installers, then uploading those to a server. You would not need to compile the GP app binaries (which is good, because that's a huge hassle).

    The customized versions of the apps might be downloaded from the microblocksfun.ch server. You could just replace the files in the release and/or pilot release folders with the customized versions.

  8. John Maloney repo owner

    Of course, you might just tell teachers we can't easily do this. 🙂

    As a work-around, those using stand-alone apps could run MicroBlocks once and set the language to Chinese on each new computer. If they are downloading and installing the stand-alone app on the computer, this is just a quick additional step, and it also verifies that the installation worked.

    If teachers are using a script to do a batch install on all the computers in a school, they could also make a script to install the MicroBlocks preferences.json file, preset to Chinese. The script would create a "MicroBlocks" folder in the Documents folder and store the preferences file there.

    So, there are many options... For teachers running MicroBlocks in the browser, we may be able to guess the language by looking at the browser's language or other setting. (I'd need to do some research on that.)

  9. Wenjie Wu reporter

    There must be a way to ask the OS for the current locale…

    Yes, there should be such a method, and it seems to work better in the browser. I just asked ChatGPT and it told me:

    #include <windows.h>
    #include <stdio.h>
    
    int main() {
        // Retrieve the default user interface language
        LANGID langId = GetUserDefaultUILanguage();
    
        switch (langId) {
        case MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US):
            printf("The current system language is English (US)\n");
            break;
        case MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED):
            printf("The current system language is Simplified Chinese\n");
            break;
        // You can add more cases here to handle other languages
        default:
            printf("The current system language ID is: %x\n", langId);
        }
    
        return 0;
    }
    

  10. Wenjie Wu reporter

    The customized versions of the apps might be downloaded from the microblocksfun.ch server. You could just replace the files in the release and/or pilot release folders with the customized versions.

    Yes, the build of MicroBlocks is quite simple, just run ./build.sh and it will build it for all platforms, so it is not a hassle to provide a separate build for a certain school. Generally speaking, they don't like to upgrade, so These questions won't be too troublesome.

    @John Computer classrooms in Chinese schools often don't have access to the Internet, so they generally want a native software rather than a web version.

  11. John Maloney repo owner

    many Chinese users may use simple letters as their username

    By simple letters, do you mean using only non-Chinese Unicode characters? If so, then my heuristic would not work.

    If you are in communication with any of the teachers making this request, perhaps you could ask them.

    Another question is whether they are using a stand-alone or browser version of MicroBlocks. If they are using the stand-alone version, what OS are they using. (I'd guess Windows is far more common in schools than MacOS.)

  12. Wenjie Wu reporter

    There must be a way to ask the OS for the current locale…

    It would be perfect if we could do this, otherwise a separate build would be a good choice.

  13. Wenjie Wu reporter

    If you are in communication with any of the teachers making this request, perhaps you could ask them.

    Yes, I keep in touch with them. Most schools use windows 7 or windows 10 and they want native software.

  14. Wenjie Wu reporter

    The most ideal situation is to use the pwa version, but currently the pwa version has encountered a problem. Several users have reported that after restarting the computer, the microblocks pwa app cannot be used and it requires an Internet connection.

  15. John Maloney repo owner

    Computer classrooms in Chinese schools often don't have access to the Internet, so they generally want a native software rather than a web version.

    That answers my question about the browser version.

    Yes, the build of MicroBlocks is quite simple, just run ./build.sh and it will build it for all platforms, so it is not a hassle to provide a separate build for a certain school. Generally speaking, they don't like to upgrade, so These questions won't be too troublesome.

    So teachers would grab the source from Bitbucket and build their own? I don't think most US teachers would be comfortable with that process, although a good school IT person might.

    Anyhow, I'll look into adding a switch to the build.sh script. I need to figure out exactly how that would work...

  16. Wenjie Wu reporter

    So teachers would grab the source from Bitbucket and build their own? I don't think most US teachers would be comfortable with that process, although a good school IT person might.

    Teachers won't accept building it themselves, they generally express their needs to me. This discussion is based on the requirements given by a teacher. He hopes that I will build a version that supports Chinese by default for him.

  17. John Maloney repo owner

    Thanks for the Windows C code from ChapGPT.

    However, the code is actually the easy part.

    You may not realize it, but the build.sh script does NOT build the GP apps from C code; it just packages them along with a bunch of GP files and and the embedded file system.

    It's a surprisingly huge hassle to rebuild the GP app for all platforms, especially now that I can no longer run Windows and Linux virtual machines on my Mac. It literally can take a day or more just to set up the build environments and tool chains for all three platforms. Thus, right now, I'm only considering options that do not require adding a new GP primitive.

  18. John Maloney repo owner

    The most ideal situation is to use the pwa version, but currently the pwa version has encountered a problem. Several users have reported that after restarting the computer, the microblocks pwa app cannot be used and it requires an Internet connection.

    Apologies! Making the PWA work is on my list, I just haven't gotten to it. However, if making that work would allow teachers to use the browser version rather than then stand-alone app I can prioritize it. I believe using the browser version also addresses an issue with entering Chinese characters on some Windows computers.

  19. John Maloney repo owner

    I've created a test version of the progressive web app at:

    https://microblocks.fun/mbtest/pwa/

    Please try installing it and then making sure that it runs even when it is offline.

    We need to test this carefully since if the service worker doesn't work correctly it can create a situation where new versions of MicroBlocks never get pulled from the server, even if you totally clear the browser cache and all website information. The only way to get out of that situation is to manually delete the service worker, something that most users would never figure out. But I've spent several hours testing and tweaking the service worker and I think this version will get updates. (I will do more testing of that before I deploy it, even as a pilot.)

    Meanwhile, please check that the above link be installed as a progressive web app and that it works offline.

    Once this is working, I will into how to detect and use the browser locale information to make the language default to Chinese.

    I hope a progressive web app will work for teacher. That will be much less work for everyone than building and distributing customize versions of the stand alone apps. It will also allow teachers to get automatic updates of either the stable or the pilot release channel, depending on which link they use.

  20. John Maloney repo owner

    @Wenjie Wu

    When you have a chance, could you open the Javascript console in Chrome and tell me what this returns:

    navigator.language
    

    I think it will be "zh" if that is your browser's current language setting, but it might be some variant of that. (Mine is "en-US".)

    It should be easy to make the MicroBlocks web app default to Chinese if the browser language is Chinese.

    Thanks!

  21. Wenjie Wu reporter

    could you open the Javascript console in Chrome and tell me what this returns:

    According to the documentation, navigator.language should be the correct choice. But strangely, Mine is also "en-US". It may be a problem with my system environment. I have invited more Chinese users to test it and I will list their feedback here.

  22. Wenjie Wu reporter

    It's a surprisingly huge hassle to rebuild the GP app for all platforms, especially now that I can no longer run Windows and Linux virtual machines on my Mac. It literally can take a day or more just to set up the build environments and tool chains for all three platforms. Thus, right now, I'm only considering options that do not require adding a new GP primitive.

    I have a little understanding of this. Currently, the build process uses a precompiled GP virtual machine built into the SmallVM code repository. Since the MicroBlocks code runs on these GP virtual machines, which are precompiled for different operating systems. The build process of microblocks is essentially just packaging work done on top of the virtual machines. This allows us to package MicroBlocks for all systems on any system. However, building GP primitives requires rebuilding the virtual machine, which needs to be done on each operating system.

    In the long term, maintaining MicroBlocks in the browser seems to be a better option, as it allows us to focus on a single platform: web platform. Browsers are continuously gaining more capabilities, such as wasmgc, WebGPU, and the File System API. Based on my observations, users tend to prefer this approach. Currently, we might need to address the PWA issue for school users.Many schools in China are using Windows 7, it's unclear if Chrome on Windows 7 supports PWA, In the long term, Windows 7 will eventually disappear. Even if PWA cannot be supported, we can still let them use the native version.

  23. Wenjie Wu reporter

    Making the PWA work is on my list, I just haven't gotten to it. However, if making that work would allow teachers to use the browser version rather than then stand-alone app I can prioritize it

    I believe it is feasible. I have already invited several teachers to test it (https://microblocks.fun/mbtest/pwa/) , and I will synchronize any feedback here.

  24. John Maloney repo owner

    Here are a few other things to try:

    navigator.languages
    navigator.userLanguage
    

    navigator.languages gives a list of language codes; we could search that list for zh. This may be the best approach.

    navigator.userLanguage seems to be a Windows-only option, but worth a try. (It's not defined in Chrome on my Mac.)

    There are also some tricks to get the user's geolocation, but some of those require using external services and they sound a little bit fragile.

    Another option would be to guess based on the timezone but, of course, other countries share the same timezones as China.

    We just need a heuristic for the first time MicroBlocks is used; it doesn't need to be perfect. If the heuristic guesses the wrong languages the user will change it and their language preference will be remembered in the browser's local storage.

  25. John Maloney repo owner

    Great! I think the heuristic can check for "zh" or "zh-CN" in the languages list and default to Chinese if the user language preference has not yet been set.

  26. John Maloney repo owner

    I just pushed a new version to the PWA test (https://microblocks.fun/mbtest/pwa/) that attempts to make the language default to Chinese if the navigator.languages list includes 'zh' or 'zh-CN'. When that happens, it should print a message in the javascript console.

    Edited: To test this, you'll need to clear the browser's local storage for the site. To do that in Chrome, click the lock icon to the left of the URL, select "cookies and site data", "manage site data", and finally click the trash can icon to remove the site data.

    Reload the page and it should default to Chinese. Fingers crossed!

  27. John Maloney repo owner

    Hooray! I will deploy this, along with the progressive web app, as a pilot in the next few days. I will delete the PWA test URL after I've done that.

  28. Wenjie Wu reporter

    I just received feedback from 3 users, and the situation seems very optimistic:

    1. Windows 10 Home(Chinese version), Edge browser,

    navigator.language: 'zh-CN'
    navigator.languages: ['zh-CN', 'en', 'en-GB', 'en-US']

    2. navigator.language: 'zh-CN'

    navigator.languages: ['zh-CN', 'en', 'zh-TW']

    3.

  29. Wenjie Wu reporter

    I will deploy this, along with the progressive web app, as a pilot in the next few days. I will delete the PWA test URL after I've done that.

    Can't wait to see it!

  30. Wenjie Wu reporter

    Just now, a few more users provided me with test results, and navigator.language: 'zh-CN' is quite stable and usable.

  31. Log in to comment