【发布时间】:2013-09-25 18:03:00
【问题描述】:
我想知道是使用谷歌翻译 API 还是使用这样的 url 更好:
/example.com/en/page/show
并使用带有大量 if 的 url 中提到的语言生成视图? 还是什么?
谢谢。
【问题讨论】:
标签: c# asp.net-mvc-4 culture
我想知道是使用谷歌翻译 API 还是使用这样的 url 更好:
/example.com/en/page/show
并使用带有大量 if 的 url 中提到的语言生成视图? 还是什么?
谢谢。
【问题讨论】:
标签: c# asp.net-mvc-4 culture
如果您想支持多种语言,一种方法是使用资源文件,而不是对页面上的任何字符串/资源进行硬编码,而是使用资源表示。
通过这种方式,您可以轻松添加新语言、翻译内容等...
关于它所依赖的 URL,通过 URL 设置它是一种方法,你也可以使用 cookie..
您可以在当前线程上设置 CurrentUICulture,所有资源检索方法和/或字符串操作实际上都支持您指定要使用的CultureInfo。它应该自动采用当前线程中设置的文化。
例如,您可以将这样的内容放入您的 global.asax
void context_BeginRequest(object sender, EventArgs e)
{
// eat the cookie (if any) and set the culture
if (HttpContext.Current.Request.Cookies["lang"] != null)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["lang"];
string lang = cookie.Value;
var culture = new System.Globalization.CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}
或者,如果您使用 MVC,您的路由配置可以包含语言,您可以使用 URL 路由来设置语言而不是 cookie...
所以这只是一个示例,我建议您在 google 上找到更多示例并找出解决您问题的最佳方法。
【讨论】: