Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

refactor(i18n): language setup (#3484)

Alex Yang452c780d40d9
diff --git a/apps/core/src/app.tsx b/apps/core/src/app.tsx
index bc8783d705..76c45c8cd3 100644
--- a/apps/core/src/app.tsx
+++ b/apps/core/src/app.tsx
@@ -5,8 +5,9 @@ import { AffineContext } from '@affine/component/context';
import { WorkspaceFallback } from '@affine/component/workspace';
import { createI18n, setUpLanguage } from '@affine/i18n';
import { CacheProvider } from '@emotion/react';
+import { use } from 'foxact/use';
import type { PropsWithChildren, ReactElement } from 'react';
-import { lazy, memo, Suspense, useEffect } from 'react';
+import { lazy, memo, Suspense } from 'react';
import { RouterProvider } from 'react-router-dom';
import { router } from './router';
@@ -32,14 +33,21 @@ const future = {
v7_startTransition: true,
} as const;
-export const App = memo(function App() {
- useEffect(() => {
+const languageLoadingPromise = new Promise<void>(resolve => {
+ if (environment.isBrowser) {
document.documentElement.lang = i18n.language;
- // todo(himself65): this is a hack, we should use a better way to set the language
- setUpLanguage(i18n)?.catch(error => {
- console.error(error);
- });
- }, []);
+ setUpLanguage(i18n)
+ .then(() => resolve())
+ .catch(error => {
+ console.error(error);
+ });
+ } else {
+ resolve();
+ }
+});
+
+export const App = memo(function App() {
+ use(languageLoadingPromise);
return (
<CacheProvider value={cache}>
<AffineContext>
diff --git a/packages/i18n/src/index.ts b/packages/i18n/src/index.ts
index 5096ef6e40..19f1ceeccc 100644
--- a/packages/i18n/src/index.ts
+++ b/packages/i18n/src/index.ts
@@ -92,17 +92,14 @@ export const createI18n = () => {
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);
+ let language;
+ const localStorageLanguage = localStorage.getItem(STORAGE_KEY);
+ if (localStorageLanguage) {
+ language = standardizeLocale(localStorageLanguage);
+ } else {
+ language = standardizeLocale(navigator.language);
}
- return void 0;
+ return i.changeLanguage(language);
}
// const I18nProvider = I18nextProvider;