os-js / osjs-client

OS.js Client Module
https://manual.os-js.org/
Other
31 stars 31 forks source link

Undefined data-locale #168

Closed mahsashadi closed 3 years ago

mahsashadi commented 3 years ago

I need to add an icon fro toggleling between english and persian language in desktop. I add the following code to src/client/index.js file

//toggle language between english and persian
  osjs.on('osjs/core:started', () =>
    osjs.make('osjs/tray', {
      title: 'Language',
      icon: osjs.make('osjs/theme').icon('preferences-desktop-locale'),
      onclick: async (ev) => {
        const settings = osjs.make('osjs/settings').language;
        const lang = settings.get('osjs/locale');
        console.log(lang);
        if (lang === 'fa_FA') {
          settings.set('osjs/locale', 'language', 'en_EN');
          await settings.save();
          document.location.reload();
        } else if (lang === 'en_EN') {
          settings.set('osjs/locale', 'language', 'fa_FA');
          await settings.save();
          document.location.reload();
        }
      },
    })
  );

It won't work, till at least one-time I change the language by setting app, after that it will toggle properly. But if I don't change language by settings, the code will console.log undefined. The property data-locale of element with class osjs-root is also undefined.

andersevenrud commented 3 years ago

Use await core.make('osjs/locale').setLocale('en_EN')

mahsashadi commented 3 years ago

Yes, thanks. It is solved. And It seems I should check both 'en_US' & 'en_EN', since at first it is set by 'en_US'

 //toggle language between english and persian
  osjs.on('osjs/core:started', () =>
    osjs.make('osjs/tray', {
      title: 'Language',
      icon: osjs.make('osjs/theme').icon('preferences-desktop-locale'),
      onclick: async (ev) => {
        const lang = await osjs.make('osjs/locale').getLocale();
        if (lang === 'fa_FA') {
          await osjs.make('osjs/locale').setLocale('en_EN');
          document.location.reload();
        } else if (lang === 'en_US' || lang === 'en_EN') {
          await osjs.make('osjs/locale').setLocale('fa_FA');
          document.location.reload();
        }
      },
    })
  );
andersevenrud commented 3 years ago

Just use lang.match(/^en/) :)