为 i18n 应用程序创建语言切换器
Creating a language switcher for i18n app
我正在学习如何在应用程序上实施 Lingui(i18n)。一切都已设置,但我想知道我应该如何创建语言切换器以在我的应用程序上的语言目录之间切换。
这是我的index.js文件
import React, { useEffect } from "react";
import { render } from "react-dom";
import App from "./App";
import { I18nProvider } from "@lingui/react";
import { i18n } from "@lingui/core";
import { defaultLocale, dynamicActivate } from "./i18n";
const Translation = () => {
useEffect(() => {
dynamicActivate(defaultLocale);
}, []);
return (
<I18nProvider i18n={i18n}>
<App />
</I18nProvider>
);
};
render(<Translation />, document.getElementById("root"));
我的App.js文件
import "./App.css";
import { Trans } from "@lingui/macro";
function App() {
return (
<div className="App">
<header className="App-header">
<h1>
<Trans>HELLOO</Trans>
</h1>
<p>
<Trans>it's me.</Trans>
</p>
</header>
</div>
);
}
export default App;
和i18n.ts文件
import { i18n } from '@lingui/core';
export const locales = {
en: "English",
es: "Spanish",
fr: "French",
};
export const defaultLocale = "fr";
/**
* We do a dynamic import of just the catalog that we need
* @param locale any locale string
*/
export async function dynamicActivate(locale: string) {
const { messages } = await import(`./locales/${locale}/messages`)
i18n.load(locale, messages)
i18n.activate(locale)
}
每次我指定 es,en 或 fr defaultLocale 时语言都会改变,但我希望在页面上有一个语言按钮来自动更改 select。
ex: "export const defaultLocale = "fr";" (在 i18n.ts)
您可以使用 i18n.activate()
方法切换到所需的语言环境。
i18n object API 定义在 @js-lingui/core.
如果之前没有加载语言环境,您还需要加载它。
对于您的项目,您可以使用您已经创建的方便的 dynamicActivate()
函数。
您的组件输出将如下所示:
<div>
<Trans>Switch to:</Trans>
<button
onClick={() => dynamicActivate('en')}>
English
</button>
<button
onClick={() => dynamicActivate('fr')}>
Français
</button>
<button
onClick={() => dynamicActivate('es')}>
Espanol
</button>
</div>
它将呈现 3 个按钮 [English] [Français] [Espanol]
每个按钮都会加载并激活所需的语言环境。
最佳做法是将按钮标题保留为他们自己的语言,这样用户可以找到他们理解的语言。
作为对上述内容的补充,突出显示 currently-selected 语言并禁用按钮可能是有意义的。
我正在使用 useLingui()
获取 i18n.locale
,它指示当前语言并在下面的按钮之一上设置 disabled
标志。
这里是LanguageSelector.js
组件的完整代码,您可以在App.js
中像<LanguageSelector />
一样使用它。祝你 project/learnings.
好运
import React from "react"
import { useLingui } from "@lingui/react"
import { Trans } from "@lingui/macro";
import { dynamicActivate } from "./i18n";
const LanguageSelector = () => {
const { i18n } = useLingui();
return <div>
<Trans>Switch to:</Trans>
<button
onClick={() => dynamicActivate('en')}
disabled={i18n.locale === 'en'}>
English
</button>
<button
onClick={() => dynamicActivate('fr')}
disabled={i18n.locale === 'fr'}>
Français
</button>
<button
onClick={() => dynamicActivate('es')}
disabled={i18n.locale === 'es'}>
Espanol
</button>
</div>
};
export default LanguageSelector
更新:
此外,您可以将选定的区域设置保存到浏览器的 LocalStorage
我们应该在每次调用 dynamicActivate()
时保存语言环境:
const LOCAL_STORAGE_KEY = 'lang';
function dynamicActivate(locale: string) {
// existing code here
window.localStorage.setItem(LOCAL_STORAGE_KEY, locale);
}
显然 @lingui/detect-locale
library 具有很好的覆盖范围,可以从许多来源(包括 LocalStorage)检测语言环境。
这里是如何应用它的:
import { detect, fromUrl, fromStorage, fromNavigator } from '@lingui/detect-locale';
// existing code from i18n.ts
export const locales = {
en: "English",
es: "Spanish",
fr: "French",
};
export const defaultLocale = "en";
const LOCAL_STORAGE_KEY = 'lang';
// checks that detected locale is available
const isLocaleValid = (locale: string | null) => `${locale}` in locales;
// returns locale
export function getLocale() {
const detectedLocale = detect(
fromUrl("lang"), // for example http://localhost:3000/?lang=es
fromStorage(LOCAL_STORAGE_KEY),
fromNavigator(), // from system settings
() => defaultLocale,
);
return isLocaleValid(detectedLocale) ? detectedLocale : defaultLocale;
}
最后一步是调用 getLocale()
而不是一直使用 defaultLocale
。
useEffect(() => {
// With this method we dynamically load the catalogs
dynamicActivate(getLocale());
}, []);
我正在学习如何在应用程序上实施 Lingui(i18n)。一切都已设置,但我想知道我应该如何创建语言切换器以在我的应用程序上的语言目录之间切换。
这是我的index.js文件
import React, { useEffect } from "react";
import { render } from "react-dom";
import App from "./App";
import { I18nProvider } from "@lingui/react";
import { i18n } from "@lingui/core";
import { defaultLocale, dynamicActivate } from "./i18n";
const Translation = () => {
useEffect(() => {
dynamicActivate(defaultLocale);
}, []);
return (
<I18nProvider i18n={i18n}>
<App />
</I18nProvider>
);
};
render(<Translation />, document.getElementById("root"));
我的App.js文件
import "./App.css";
import { Trans } from "@lingui/macro";
function App() {
return (
<div className="App">
<header className="App-header">
<h1>
<Trans>HELLOO</Trans>
</h1>
<p>
<Trans>it's me.</Trans>
</p>
</header>
</div>
);
}
export default App;
和i18n.ts文件
import { i18n } from '@lingui/core';
export const locales = {
en: "English",
es: "Spanish",
fr: "French",
};
export const defaultLocale = "fr";
/**
* We do a dynamic import of just the catalog that we need
* @param locale any locale string
*/
export async function dynamicActivate(locale: string) {
const { messages } = await import(`./locales/${locale}/messages`)
i18n.load(locale, messages)
i18n.activate(locale)
}
每次我指定 es,en 或 fr defaultLocale 时语言都会改变,但我希望在页面上有一个语言按钮来自动更改 select。
ex: "export const defaultLocale = "fr";" (在 i18n.ts)
您可以使用 i18n.activate()
方法切换到所需的语言环境。
i18n object API 定义在 @js-lingui/core.
如果之前没有加载语言环境,您还需要加载它。
对于您的项目,您可以使用您已经创建的方便的 dynamicActivate()
函数。
您的组件输出将如下所示:
<div>
<Trans>Switch to:</Trans>
<button
onClick={() => dynamicActivate('en')}>
English
</button>
<button
onClick={() => dynamicActivate('fr')}>
Français
</button>
<button
onClick={() => dynamicActivate('es')}>
Espanol
</button>
</div>
它将呈现 3 个按钮 [English] [Français] [Espanol]
每个按钮都会加载并激活所需的语言环境。
最佳做法是将按钮标题保留为他们自己的语言,这样用户可以找到他们理解的语言。
作为对上述内容的补充,突出显示 currently-selected 语言并禁用按钮可能是有意义的。
我正在使用 useLingui()
获取 i18n.locale
,它指示当前语言并在下面的按钮之一上设置 disabled
标志。
这里是LanguageSelector.js
组件的完整代码,您可以在App.js
中像<LanguageSelector />
一样使用它。祝你 project/learnings.
import React from "react"
import { useLingui } from "@lingui/react"
import { Trans } from "@lingui/macro";
import { dynamicActivate } from "./i18n";
const LanguageSelector = () => {
const { i18n } = useLingui();
return <div>
<Trans>Switch to:</Trans>
<button
onClick={() => dynamicActivate('en')}
disabled={i18n.locale === 'en'}>
English
</button>
<button
onClick={() => dynamicActivate('fr')}
disabled={i18n.locale === 'fr'}>
Français
</button>
<button
onClick={() => dynamicActivate('es')}
disabled={i18n.locale === 'es'}>
Espanol
</button>
</div>
};
export default LanguageSelector
更新:
此外,您可以将选定的区域设置保存到浏览器的 LocalStorage
我们应该在每次调用 dynamicActivate()
时保存语言环境:
const LOCAL_STORAGE_KEY = 'lang';
function dynamicActivate(locale: string) {
// existing code here
window.localStorage.setItem(LOCAL_STORAGE_KEY, locale);
}
显然 @lingui/detect-locale
library 具有很好的覆盖范围,可以从许多来源(包括 LocalStorage)检测语言环境。
这里是如何应用它的:
import { detect, fromUrl, fromStorage, fromNavigator } from '@lingui/detect-locale';
// existing code from i18n.ts
export const locales = {
en: "English",
es: "Spanish",
fr: "French",
};
export const defaultLocale = "en";
const LOCAL_STORAGE_KEY = 'lang';
// checks that detected locale is available
const isLocaleValid = (locale: string | null) => `${locale}` in locales;
// returns locale
export function getLocale() {
const detectedLocale = detect(
fromUrl("lang"), // for example http://localhost:3000/?lang=es
fromStorage(LOCAL_STORAGE_KEY),
fromNavigator(), // from system settings
() => defaultLocale,
);
return isLocaleValid(detectedLocale) ? detectedLocale : defaultLocale;
}
最后一步是调用 getLocale()
而不是一直使用 defaultLocale
。
useEffect(() => {
// With this method we dynamically load the catalogs
dynamicActivate(getLocale());
}, []);