-1

我想知道从谷歌翻译 API 使用还是使用这样的网址更好:

/example.com/en/page/show

并使用带有大量 if 的 url 中提到的语言生成视图?还是什么元素?

谢谢。

4

1 回答 1

1

如果您想支持多种语言,一种方法是使用资源文件,而不是硬编码页面上的任何字符串/资源,而是使用资源表示。

通过这种方式,您可以轻松添加新语言、翻译内容等...

关于它所依赖的 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...

所以这只是一个例子,我建议你在谷歌上找到更多的例子,找出解决你问题的最佳方法。

于 2013-09-25T18:11:32.720 回答