mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-05-04 06:03:00 +03:00
Initial i18n dev
Expecting bugs!
This commit is contained in:
parent
3f23fe6eb6
commit
c2e6d732c4
76 changed files with 13355 additions and 2042 deletions
src/utils
56
src/utils/lang.js
Normal file
56
src/utils/lang.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
import { i18n } from '@lingui/core';
|
||||
import {
|
||||
detect,
|
||||
fromNavigator,
|
||||
fromStorage,
|
||||
fromUrl,
|
||||
} from '@lingui/detect-locale';
|
||||
import Locale from 'intl-locale-textinfo-polyfill';
|
||||
|
||||
import { messages } from '../locales/en.po';
|
||||
import localeMatch from '../utils/locale-match';
|
||||
|
||||
const { PHANPY_DEFAULT_LANG } = import.meta.env;
|
||||
|
||||
export const DEFAULT_LANG = 'en';
|
||||
export const LOCALES = [DEFAULT_LANG];
|
||||
if (import.meta.env.DEV) {
|
||||
LOCALES.push('pseudo-LOCALE');
|
||||
}
|
||||
|
||||
export async function activateLang(lang) {
|
||||
if (!lang || lang === DEFAULT_LANG) {
|
||||
i18n.loadAndActivate({ locale: DEFAULT_LANG, messages });
|
||||
console.log('💬 ACTIVATE LANG', lang);
|
||||
} else {
|
||||
const { messages } = await import(`../locales/${lang}.po`);
|
||||
i18n.loadAndActivate({ locale: lang, messages });
|
||||
console.log('💬 ACTIVATE LANG', lang);
|
||||
}
|
||||
}
|
||||
|
||||
i18n.on('change', () => {
|
||||
const lang = i18n.locale;
|
||||
if (lang) {
|
||||
// LTR or RTL
|
||||
const { direction } = new Locale(lang).textInfo;
|
||||
document.documentElement.dir = direction;
|
||||
}
|
||||
});
|
||||
|
||||
export function initActivateLang() {
|
||||
const lang = detect(
|
||||
fromUrl('lang'),
|
||||
fromStorage('lang'),
|
||||
fromNavigator(),
|
||||
PHANPY_DEFAULT_LANG,
|
||||
DEFAULT_LANG,
|
||||
);
|
||||
const matchedLang = localeMatch(lang, LOCALES);
|
||||
activateLang(matchedLang);
|
||||
|
||||
// const yes = confirm(t`Reload to apply language setting?`);
|
||||
// if (yes) {
|
||||
// window.location.reload();
|
||||
// }
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue