双语应用问题与Next.js

huangapple go评论58阅读模式
英文:

Double-language application problem with Next.js

问题

我目前正在使用Next.js开发一个Web应用程序,它将支持两种语言:我的母语和英语。现在我正在考虑以下方法:

  1. 创建一个文件夹:/pages/en-us
pages/
|--(所有应用程序页面)
|--en-us/
   |--(所有应用程序页面如上所示)
  1. 使用LanguageContext包装应用程序,并使用中间件在加载时检测访问者的IP以确定其国家,并且如果他们是外国访客或当前语言设置为英语,则重定向到/en-us路径。

    例如,/login将被重定向到/en-us/login

我知道有一些多语言框架,比如<a href="https://www.i18next.com/">i18next</a>,但我的Web应用程序相当简单,只需要两种语言,因此不需要使用它们。

我的方法是否合适?如果不合适,请给我一些建议和推荐。

英文:

I'm currently developing a web application using Next.js which will support 2 languages: my mother language and English. Right now I'm thinking of the following approach:

  1. Create a folder: /pages/en-us
pages/
|--(all app pages)
|--en-us/
   |--(all app pages like above)
  1. Wrap the app with a LanguageContext, and use a middleware to detect visitor's country by their IP on load, and redirects to /en-us path if they're foreign visitor or current language is set to English.
    <br>
    For example, /login will be redirected to /en-us/login.
    <br>

I know that there're some multi-language frameworks like <a href="https://www.i18next.com/">i18next</a> but my web app is quite simple and only needs 2 languages, so it won't be necessary to use them.
<br>

Is my approach good? If not, please give me some advice and recommendation.

答案1

得分: 2

我建议您集成i18next。否则,您将不得不面对代码重复和无法调整语言文件中的所有更改。

中间件方法很有趣,但实际上更好的方法可能是使用Navigator.language查看用户的语言。Navigator.language 只读属性返回表示用户首选语言的字符串,通常是浏览器 UI 的语言。

希望我能帮助您。祝您的项目好运!

英文:

I would recommend you integrating i18next. Otherwise, you have to face code duplications and you cannot adjust all changes in the language files.
The middleware approach is interesting, but actually a better way me be to see the language of the user with Navigator.language. The Navigator.language read-only property returns a string representing the preferred language of the user, usually the language of the browser UI.

I hope I could help you. Good luck with your project!

huangapple
  • 本文由 发表于 2023年1月9日 00:13:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定