英文:
Double-language application problem with Next.js
问题
我目前正在使用Next.js开发一个Web应用程序,它将支持两种语言:我的母语和英语。现在我正在考虑以下方法:
- 创建一个文件夹:/pages/en-us
pages/
|--(所有应用程序页面)
|--en-us/
|--(所有应用程序页面,如上所示)
- 使用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:
- Create a folder: /pages/en-us
pages/
|--(all app pages)
|--en-us/
|--(all app pages like above)
- 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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论