mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
refactor!: next generation AFFiNE code structure (#1176)
This commit is contained in:
@@ -27,8 +27,9 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@affine/debug": "workspace:*",
|
||||
"react": "^18.2.0",
|
||||
"i18next": "^22.4.10",
|
||||
"next-i18next": "^13.1.5",
|
||||
"react": "^18.2.0",
|
||||
"react-i18next": "^12.1.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -36,6 +37,7 @@
|
||||
"@types/prettier": "^2.7.2",
|
||||
"prettier": "^2.8.4",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^4.9.5"
|
||||
"typescript": "^4.9.5",
|
||||
"next": "^13.2.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import i18next, { Resource } from 'i18next';
|
||||
import { initReactI18next, Trans, useTranslation } from 'react-i18next';
|
||||
import i18next, { i18n, Resource } from 'i18next';
|
||||
import { appWithTranslation } from 'next-i18next';
|
||||
import {
|
||||
I18nextProvider,
|
||||
initReactI18next,
|
||||
Trans,
|
||||
useTranslation,
|
||||
} from 'react-i18next';
|
||||
|
||||
import { LOCALES } from './resources';
|
||||
import type en_US from './resources/en.json';
|
||||
@@ -32,7 +38,7 @@ declare module 'react-i18next' {
|
||||
|
||||
const STORAGE_KEY = 'i18n_lng';
|
||||
|
||||
export { i18n, LOCALES, Trans, useTranslation };
|
||||
export { appWithTranslation, I18nextProvider, LOCALES, Trans, useTranslation };
|
||||
|
||||
const resources = LOCALES.reduce<Resource>(
|
||||
(acc, { tag, res }) => ({ ...acc, [tag]: { translation: res } }),
|
||||
@@ -55,29 +61,35 @@ const standardizeLocale = (language: string) => {
|
||||
|
||||
return fallbackLng;
|
||||
};
|
||||
let language = 'en';
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
const localStorageLanguage = localStorage.getItem(STORAGE_KEY);
|
||||
if (localStorageLanguage) {
|
||||
language = standardizeLocale(localStorageLanguage);
|
||||
} else {
|
||||
language = standardizeLocale(navigator.language);
|
||||
export const createI18n = () => {
|
||||
const i18n = i18next.createInstance();
|
||||
i18n.use(initReactI18next).init({
|
||||
lng: 'en',
|
||||
fallbackLng,
|
||||
debug: false,
|
||||
resources,
|
||||
interpolation: {
|
||||
escapeValue: false, // not needed for react as it escapes by default
|
||||
},
|
||||
});
|
||||
|
||||
i18n.on('languageChanged', lng => {
|
||||
localStorage.setItem(STORAGE_KEY, lng);
|
||||
});
|
||||
return i18n;
|
||||
};
|
||||
export function setUpLanguage(i: i18n) {
|
||||
if (typeof window !== 'undefined') {
|
||||
let language;
|
||||
const localStorageLanguage = localStorage.getItem(STORAGE_KEY);
|
||||
if (localStorageLanguage) {
|
||||
language = standardizeLocale(localStorageLanguage);
|
||||
} else {
|
||||
language = standardizeLocale(navigator.language);
|
||||
}
|
||||
return i.changeLanguage(language);
|
||||
}
|
||||
}
|
||||
const i18n = i18next.createInstance();
|
||||
i18n.use(initReactI18next).init({
|
||||
lng: language,
|
||||
fallbackLng,
|
||||
debug: false,
|
||||
resources,
|
||||
interpolation: {
|
||||
escapeValue: false, // not needed for react as it escapes by default
|
||||
},
|
||||
});
|
||||
|
||||
i18n.on('languageChanged', lng => {
|
||||
localStorage.setItem(STORAGE_KEY, lng);
|
||||
});
|
||||
|
||||
// const I18nProvider = I18nextProvider;
|
||||
|
||||
Reference in New Issue
Block a user