英语和另一种语言的 ASP.NET 动态本地化
我正在创建一个网站模板,网站所有者可以选择英语以外的母语,并自行本地化一些内容。让我结合现在的实际情况更好地解释一下:我的网站将有英语和土耳其语(我的母语是土耳其语,但这应该可以更改并适用于任何非英语语言)版本的主要内容标题,例如 BLOG 、项目、关于、联系方式等,但这些的数量和名称完全取决于用户的选择,所以我可以打开一个名为 MySection 的新部分,它的标题及其土耳其语的本地翻译将存储在我的数据库中(使用EF)。此外,在我的数据库设置中,还存储了本机语言的代码(在本例中为“tr”)。根据浏览器发送网页内容的最模块化/有组织的方式是什么,就像这样:
在数据库中,部分以这种方式组织(标题及其土耳其语翻译)(所有这些都将是任意数量并且完全由用户创建):
Title NativeTitle
---------------------
BLOG BLOG
PROJECTS PROJELER
ABOUT HAKKINDA
另外,我的母语设置(我创建的)设置为 tr
(这取决于网站所有者,也可以更改)。因此,根据用户浏览器的首选语言设置,我想显示土耳其语内容,而对于所有其他语言首选项,包括(显然)英语,我想显示默认的英语内容。使用 RESX 文件对静态内容的特定语言进行本地化非常简单,但在我的情况下,我既没有静态内容,也没有特定的已知本机语言,因此我的所有数据都来自数据库。我想尽可能少地编写代码并尽可能地声明性。这样做的最佳实践是什么?检查页面中母语和当前文化的设置,如果它们相等,则发送母语,否则发送默认标题是唯一的方法吗?它肯定会起作用,但如果我想将其扩展到网站的其他部分怎么办?我不希望网站周围到处都是 if 和 else,我需要某种集中式字符串映射系统。最好的办法是什么?
I'm creating a website template where the owner of the site will be able to choose a native language other than English and localize some content by themselves. Let me explain it better with the real situation right now: My site will have English and Turkish (my native language is Turkish, but THIS should be changeable and applied to any non-English language) versions of the main content titles, such as BLOG, PROJECTS, ABOUT, CONTACT etc, but number and name of these are completely dependent to the choice of the user, so I could open a new section named MySection, and it's title and it's native translation to Turkish will be stored in my DB (using EF). Also in my settings at the DB, the native language's code (in this case "tr") is stored. What is the most modular/organized way of sending the webpage content according to browser, just like this:
In DB, sections are organized this way (titles and their Turkish translations) (all these will be at arbitrary number and completely user created):
Title NativeTitle
---------------------
BLOG BLOG
PROJECTS PROJELER
ABOUT HAKKINDA
Also I have my native language setting (that I've created) setting as tr
(which is up to the site owner and can be changed too). So, depending on the user's browsers preferred language setting, I want to show the Turkish content, and for all the other language preferences, including (obviously) English, I want to show the default English content. Localization for a specific language of static content is pretty straightforward with RESX files, but in my situation, I neither have static content nor a specific known native language, so all my data comes from the DB. I want to code this as less as possible and as declarative as possible. What is the best practice of doing this? Is checking the setting of the native language and the Current Culture in a page and if they are equal sending the native else sending the default title the only way? It WILL definitely work, but what if I want to extend it to other parts of the site? I don't want if's and else's everywhere around the site, I need some kind of a centralized string mapping system. What's the best way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以覆盖 InitializeCulture ( http://msdn .microsoft.com/en-us/library/system.web.ui.page.initializeculture.aspx ) 基本
Page
e 中的方法,通过检查Thread.CurrentThread.CurrentUICulture
属性 (http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentuiculture.aspx)You can override the InitializeCulture ( http://msdn.microsoft.com/en-us/library/system.web.ui.page.initializeculture.aspx ) method in your base
Pag
e to dynamically load your localized content by checking theThread.CurrentThread.CurrentUICulture
property ( http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentuiculture.aspx )