简体   繁体   English

如何使用模块联合将 i18n 实现到微前端应用程序中

[英]How to implement i18n into a micro-frontend application using Module Federation

What would you recommend for implementing i18n in a module federation micro-frontend next.js app?对于在模块联合微前端 next.js 应用程序中实施 i18n,您有什么建议?

At the moment (just starting) I got it working with react-intl by declaring the provider in the _app.tsx目前(刚刚开始)我通过在_app.tsx中声明提供程序来使用react-intl

  <IntlProvider locale={locale} defaultLocale={defaultLocale} messages={enMessages}>
    <SiteLayout>
      <Component {...pageProps}></Component>
    </SiteLayout>
  </IntlProvider>

And then when importing a component from another micro-frontend I am sending the intl object as param.然后从另一个微前端导入组件时,我将intl object 作为参数发送。

const XComponent = (await import('microApp2/XComponent')).default;
export default function ShellApp(): React.ReactElement {
  const intl = useIntl();
  return <XComponent intl={intl} />;
}

So that then in the micro-app component I can use it to format strings.这样我就可以在微应用组件中使用它来格式化字符串。

export default XComponent = (props: XComponentProps): React.ReactElement => {
  const { intl } = props;
  return (<div>{intl.formatMessage({ id: 'XComponent.Description' })}</div>);
}

The caveat of this approach is that I can only format using functions, I cannot make use of React DOM tags like:这种方法的警告是我只能使用函数进行格式化,我不能使用 React DOM 标签,例如:

<FormattedMessage id="" />

I didn't found much documentation so not sure this is a good approach.我没有找到太多文档,所以不确定这是一个好方法。 Those anybody knows or suggest any other solutions?那些人知道或建议任何其他解决方案吗?

Thanks in advance!提前致谢!

You probably need to wrap each of your hosts with <IntlProvider> and get the value of your current language from a global source (like the URL or the state via a cookie etc).您可能需要用<IntlProvider>包装您的每个主机,并从全球来源(如 URL 或 state 通过 cookie 等)获取当前语言的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 withFonts() 配置 i18n - How to configure i18n with withFonts() 如何使用 next-i18next 将 i18n 或 t function 传递给依赖项 - How to pass i18n or t function to dependency using next-i18next 使用 next-translate 和 i18n 测试组件 - Testing components using next-translate and i18n 在 i18n 中为同一语言环境使用两种语言代码 - Using two language codes for same locale in i18n 如何在没有 getStaticPaths 的情况下使用 Next.js 构建具有国际化路由 (i18n) 的动态路由? - How to build dynamic routes with internationalized routes (i18n) using Next.js without getStaticPaths? 如何在 Next.js 中重新加载时强制执行 i18n 语言环境 slug 并实现 i18n 一致性? - How to enforce i18n locale slugs and achieve i18n consistency upon reload in Next.js? 如何从 api 端点在 next-i18next 中正确加载 i18n 资源? - How to properly load i18n resources in next-i18next from api endpoints? RSuite 错误消息使用 rsuite Schema 进行转换。 NextJS i18n - RSuite error messages translate using rsuite Schema. NextJS i18n 如何在带有 TypeScript 的 next.config.js 中使用 i18n 和 next/image? - How to use i18n and next/image in next.config.js with TypeScript? 如何在 next.js 中管理 i18n 路由以使用斜线而不是破折号 - How to manage i18n routing in next.js to use slash instead of dash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM