2

我想知道实现这一目标的最佳方法是什么。

我的控制器中有一个ActionResult,实际上它有news名称,现在我需要对我的网站进行国际化,我不能使用相同的news名称,它必须根据访问的国家/地区进行更改。

例如,现在我需要类似的东西。

www.something.com/en/us/news英文版

www.something.com/co/es/noticias对于西班牙语版本

你有关于下一个国家的观点。

我认为我不需要根据完全相同的x url创建x 方法,但我不知道如何以非常有效的方式实现它...谢谢

4

2 回答 2

0

您的路由现在如何工作?如果您还没有使用它,也许像这个答案这样的东西会起作用。也许这有一些变化,URL 的各个部分以不同的顺序排列,以满足您的需求。例如,控制器不一定要在路由中排在第一位(或者根本不需要,在这种情况下总是使用相同的控制器名称)。使用语言代码作为键,制作某种地图,以每种不同的语言为您提供“新闻”一词。

// populate this map somewhere - language code to word for "news" (and any other name of the controllers that you have)
var newsControllerMap = new Dictionary<string, string>();
newsControllerMap["en"] = "news"; // etc.

// ...

// inside of the RouteConfig class (MVC 4) or RegisterRoutes() method in Global.asax.cs (MVC 3)
// just making an assumption that whatever class/entity you use ("LanguageAndCountry" in this case) also has a country code to make this easier. Obviously this would be refactored to have better naming/functionality to make sense and meet your needs.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    LanguageAndCountryRepository langRepo = new LanguageAndCountryRepository();
    var languagesandCountries = langRepo.GetAllLanguagesWithCountries();

    foreach (LanguageAndCountry langAndCountry in languagesandCountries)
    {
        routes.MapRoute(
        "LocalizationNews_" + langAndCountry.LanguageAbbreviation,
        langAndCountry.LanguageAbbreviation + "/" + langAndCountry.CountryCode + "/" + newsControllerMap[langAndCountry.LanguageAbbreviation],
        new { lang = language.LanguageAbbreviation, country = langAndCountry.CountryCode, controller = "News", action = "Index"});

        // map more routes to each controller you have, each controller having a corresponding map to the name of the controller in any given language
    }
于 2013-05-15T21:15:37.960 回答
0

您可以创建一个新类 TranslatedRoute 和 TranslationProvider 以将不同的翻译映射到相同的操作。然后您可以将它们插入路由系统并覆盖默认映射。

这是一篇很好的博客文章,描述了这个想法:http ://blog.maartenballiauw.be/post/2010/01/26/Translating-routes-%28ASPNET-MVC-and-Webforms%29.aspx

于 2013-05-15T21:16:11.137 回答