public void Ensure_that_inbound_routing_works_when_contraining_by_culture()
        {
            // re: issue #53

            var translations = new FluentTranslationProvider();

            translations.AddTranslations().ForController <CulturePrefixController>().RouteUrl(x => x.Index(), new Dictionary <String, String> {
                { "pt", "Inicio" }
            });
            translations.AddTranslations().ForController <CulturePrefixController>().RouteUrl(x => x.Index(), new Dictionary <String, String> {
                { "en", "Home" }
            });

            RouteTable.Routes.Clear();
            RouteTable.Routes.MapAttributeRoutes(config =>
            {
                config.AddRoutesFromController <CulturePrefixController>();
                config.AddTranslationProvider(translations);
                config.ConstrainTranslatedRoutesByCurrentUICulture = true;
                //config.UseRouteHandler(() => new Issue53RouteHandler());
            });

            RouteTable.Routes.Cast <Route>().LogTo(Console.Out);

            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");

            "~/en/cms/index".ShouldMapTo <CulturePrefixController>(x => x.Index());
        }
예제 #2
0
        public void Ensure_that_inbound_routing_works_when_contraining_by_culture()
        {
            // re: issue #53

            var translations = new FluentTranslationProvider();

            translations.AddTranslations().ForController <CulturePrefixController>().RouteUrl(x => x.Index(), new Dictionary <String, String> {
                { "pt", "Inicio" }
            });
            translations.AddTranslations().ForController <CulturePrefixController>().RouteUrl(x => x.Index(), new Dictionary <String, String> {
                { "en", "Home" }
            });

            RouteTable.Routes.Clear();
            RouteTable.Routes.MapAttributeRoutes(config =>
            {
                config.AddRoutesFromController <CulturePrefixController>();
                config.AddTranslationProvider(translations);
                config.ConstrainTranslatedRoutesByCurrentUICulture = true;
                config.CurrentUICultureResolver = (httpContext, routeData) =>
                {
                    return((string)routeData.Values["culture"]
                           ?? Thread.CurrentThread.CurrentUICulture.Name);
                };
            });

            RouteTable.Routes.Cast <Route>().LogTo(Console.Out);

            "~/en/cms/home".ShouldMapTo <CulturePrefixController>(x => x.Index());
            Assert.That("~/en/cms/inicio".Route(), Is.Null);
            Assert.That("~/pt/cms/home".Route(), Is.Null);
            "~/pt/cms/inicio".ShouldMapTo <CulturePrefixController>(x => x.Index());
        }
예제 #3
0
        public void It_returns_translated_routes()
        {
            var translations = new FluentTranslationProvider();

            translations.AddTranslations().ForController <TranslationController>()
            .AreaUrl(new Dictionary <string, string>
            {
                { "es", "es-Area" }
            })
            .RoutePrefixUrl(new Dictionary <string, string>
            {
                { "es", "es-Prefix" }
            })
            .RouteUrl(c => c.Index(), new Dictionary <string, string>
            {
                { "es", "es-Index" }
            });

            translations.AddTranslations()
            .ForKey("CustomAreaKey", new Dictionary <string, string>
            {
                { "es", "es-CustomArea" }
            })
            .ForKey("CustomPrefixKey", new Dictionary <string, string>
            {
                { "es", "es-CustomPrefix" }
            })
            .ForKey("CustomRouteKey", new Dictionary <string, string>
            {
                { "es", "es-CustomIndex" }
            });

            RouteTable.Routes.Clear();
            RouteTable.Routes.MapAttributeRoutes(config =>
            {
                config.AddRoutesFromController <TranslationController>();
                config.AddRoutesFromController <TranslationWithCustomKeysController>();
                config.AddTranslationProvider(translations);
            });

            var requestContext = MockBuilder.BuildRequestContext();

            // Default culture
            var urlHelper = new UrlHelper(requestContext, RouteTable.Routes);

            Assert.That(urlHelper.Action("Index", "Translation", new { area = "Area" }),
                        Is.EqualTo("/Area/Prefix/Index"));

            // es-ES culture
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");
            Assert.That(urlHelper.Action("Index", "Translation", new { area = "Area" }),
                        Is.EqualTo("/es-Area/es-Prefix/es-Index"));

            // es culture
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");
            Assert.That(urlHelper.Action("Index", "Translation", new { area = "Area" }),
                        Is.EqualTo("/es-Area/es-Prefix/es-Index"));
        }
        private AttributeRoute MapRoutesAndFetchFirst(Func <AttributeRoute, bool> predicate)
        {
            var provider = new FluentTranslationProvider();

            provider.AddTranslations().ForController <TranslateActionsController>()
            .RouteUrl(c => c.Index(1), new Dictionary <string, string>
            {
                { "es", "hola" },
                { "es-ES", "HOLA!" }
            });

            var routes = RouteTable.Routes;

            routes.MapAttributeRoutes(config =>
            {
                config.AddRoutesFromController <TranslateActionsController>();
                config.AddTranslationProvider(provider);
                config.ConstrainTranslatedRoutesByCurrentUICulture = true;
            });

            // Fetch the first route
            var route = routes.Cast <AttributeRoute>().FirstOrDefault(predicate);

            Assert.That(route, Is.Not.Null);

            return(route);
        }
예제 #5
0
        public void It_adds_partially_translated_route_to_route_table_when_only_parts_of_the_route_are_translated()
        {
            var translationProvider = new FluentTranslationProvider();

            translationProvider.AddTranslations().ForController <TranslationController>()
            .RouteUrl(c => c.Index(), new Dictionary <string, string>
            {
                { "es", "es-Index" }
            });

            RouteTable.Routes.Clear();
            RouteTable.Routes.MapAttributeRoutes(config =>
            {
                config.AddRoutesFromController <TranslationController>();
                config.AddTranslationProvider(translationProvider);
            });

            // Ensure that a route is added for each translation
            Assert.That(RouteTable.Routes.Count, Is.EqualTo(2));

            var translatedRoute = RouteTable.Routes.Cast <AttributeRoute>().SingleOrDefault(r => (string)r.DataTokens["cultureName"] == "es");

            Assert.That(translatedRoute, Is.Not.Null);
            Assert.That(translatedRoute.Url, Is.EqualTo("Area/Prefix/es-Index"));

            RouteTable.Routes.Cast <Route>().LogTo(Console.Out);
        }
        public void It_returns_translation_for_given_keys()
        {
            var provider = new FluentTranslationProvider();

            provider.AddTranslations().ForController <TranslationController>()
            .AreaUrl(new Dictionary <string, string>
            {
                { "es", "es-Area" }
            })
            .RoutePrefixUrl(new Dictionary <string, string>
            {
                { "es", "es-Prefix" }
            })
            .RouteUrl(c => c.Index(), new Dictionary <string, string>
            {
                { "es", "es-Index" }
            });

            provider.AddTranslations()
            .ForKey("CustomAreaKey", new Dictionary <string, string>
            {
                { "es", "es-CustomArea" }
            })
            .ForKey("CustomPrefixKey", new Dictionary <string, string>
            {
                { "es", "es-CustomPrefix" }
            })
            .ForKey("CustomRouteKey", new Dictionary <string, string>
            {
                { "es", "es-CustomIndex" }
            });

            var keyGenerator = new TranslationKeyGenerator();

            Assert.That(provider.GetTranslation(keyGenerator.AreaUrl <TranslationController>(), "en"), Is.Null);
            Assert.That(provider.GetTranslation(keyGenerator.RoutePrefixUrl <TranslationController>(), "en"), Is.Null);
            Assert.That(provider.GetTranslation(keyGenerator.RouteUrl <TranslationController>(c => c.Index()), "en"), Is.Null);

            Assert.That(provider.GetTranslation(keyGenerator.AreaUrl <TranslationController>(), "es"), Is.EqualTo("es-Area"));
            Assert.That(provider.GetTranslation(keyGenerator.RoutePrefixUrl <TranslationController>(), "es"), Is.EqualTo("es-Prefix"));
            Assert.That(provider.GetTranslation(keyGenerator.RouteUrl <TranslationController>(c => c.Index()), "es"), Is.EqualTo("es-Index"));

            Assert.That(provider.GetTranslation("CustomAreaKey", "es"), Is.EqualTo("es-CustomArea"));
            Assert.That(provider.GetTranslation("CustomPrefixKey", "es"), Is.EqualTo("es-CustomPrefix"));
            Assert.That(provider.GetTranslation("CustomRouteKey", "es"), Is.EqualTo("es-CustomIndex"));
        }
        public void It_can_add_tranlsations_for_actions_with_defaulted_params()
        {
            var provider = new FluentTranslationProvider();

            provider.AddTranslations().ForController <TranslateActionsController>()
            .RouteUrl(c => c.Index(1), new Dictionary <string, string>
            {
                { "es", "hola" }
            });

            var keyGenerator   = new TranslationKeyGenerator();
            var translationKey = keyGenerator.RouteUrl <TranslateActionsController>(c => c.Index(1));
            var translation    = provider.GetTranslation(translationKey, "es");

            Assert.That(translation, Is.EqualTo("hola"));
        }
예제 #8
0
        public void It_will_mix_and_match_translations_suppied_by_all_providers_when_building_route_url()
        {
            var translationProvider1 = new FluentTranslationProvider();

            translationProvider1.AddTranslations().ForController <TranslationController>()
            .RouteUrl(c => c.Index(), new Dictionary <string, string>
            {
                { "es", "es-Index" }
            });

            var translationProvider2 = new FluentTranslationProvider();

            translationProvider2.AddTranslations().ForController <TranslationController>()
            .RoutePrefixUrl(new Dictionary <string, string>
            {
                { "es", "es-Prefix" }
            });

            var translationProvider3 = new FluentTranslationProvider();

            translationProvider3.AddTranslations().ForController <TranslationController>()
            .AreaUrl(new Dictionary <string, string>
            {
                { "es", "es-Area" }
            });

            RouteTable.Routes.Clear();
            RouteTable.Routes.MapAttributeRoutes(config =>
            {
                config.AddRoutesFromController <TranslationController>();
                config.AddTranslationProvider(translationProvider1);
                config.AddTranslationProvider(translationProvider2);
                config.AddTranslationProvider(translationProvider3);
            });

            // Ensure that a route is added for each translation
            Assert.That(RouteTable.Routes.Count, Is.EqualTo(2));

            var translatedRoute = RouteTable.Routes.Cast <AttributeRoute>().SingleOrDefault(r => (string)r.DataTokens["cultureName"] == "es");

            Assert.That(translatedRoute, Is.Not.Null);
            Assert.That(translatedRoute.Url, Is.EqualTo("es-Area/es-Prefix/es-Index"));

            RouteTable.Routes.Cast <Route>().LogTo(Console.Out);
        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            var translationProvider = new FluentTranslationProvider();

            translationProvider.AddTranslations().ForController <LocalizationController>()
            .AreaUrl(new Dictionary <string, string>
            {
                { "es", "{culture}/es-AreaUrl" },
                { "fr", "{culture}/fr-AreaUrl" },
            })
            .RoutePrefixUrl(new Dictionary <string, string>
            {
                { "es", "es-RoutePrefixUrl" },
                { "fr", "fr-RoutePrefixUrl" },
            })
            .RouteUrl(c => c.Index(), new Dictionary <string, string>
            {
                { "es", "es-RouteUrl" },
                { "fr", "fr-RouteUrl" },
            });

            routes.MapAttributeRoutes(config =>
            {
                config.ScanAssemblyOf <ControllerBase>();
                config.AddDefaultRouteConstraint(@"[Ii]d$", new RegexRouteConstraint(@"^\d+$"));
                config.AddTranslationProvider(translationProvider);
                config.UseRouteHandler(() => new CultureAwareRouteHandler());
                config.UseLowercaseRoutes = true;
                config.InheritActionsFromBaseController = true;
            });

            routes.MapRoute("CatchAll",
                            "{*path}",
                            new { controller = "home", action = "filenotfound" },
                            new[] { typeof(HomeController).Namespace });
        }
예제 #10
0
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            var translationProvider = new FluentTranslationProvider();
            var routePrefixDict     = new Dictionary <string, string>
            {
                { "es", "es-RoutePrefixUrl" },
                { "fr", "fr-RoutePrefixUrl" },
            };
            var indexRouteUrlDict = new Dictionary <string, string>
            {
                { "es", "es-RouteUrl" },
                { "fr", "fr-RouteUrl" },
            };

            // MVC localized controller
            translationProvider.AddTranslations()
            .ForController <Controllers.LocalizationController>()
            .AreaUrl(new Dictionary <string, string>
            {
                { "es", "{culture}/es-AreaUrl" },
                { "fr", "{culture}/fr-AreaUrl" },
            })
            .RoutePrefixUrl(routePrefixDict)
            .RouteUrl(c => c.Index(), indexRouteUrlDict);

            // Web API localized controller
            translationProvider.AddTranslations()
            .ForController <Areas.Api.Controllers.LocalizationController>()
            .AreaUrl(new Dictionary <string, string>
            {
                { "es", "api/{culture}/es-AreaUrl" },
                { "fr", "api/{culture}/fr-AreaUrl" },
            })
            .RoutePrefixUrl(routePrefixDict)
            .RouteUrl(c => c.GetLocalized(), indexRouteUrlDict);

            // Web API (WebHost)
            GlobalConfiguration.Configuration.Routes.MapHttpAttributeRoutes(config =>
            {
                config.AddRoutesFromAssemblyOf <MvcApplication>();
                config.AddDefaultRouteConstraint(@"[Ii]d$", new RegexRouteConstraint(@"^\d+$"));
                config.UseRouteHandler(() => new HttpCultureAwareRoutingHandler());
                config.AddTranslationProvider(translationProvider);
                config.UseLowercaseRoutes = true;
                config.InheritActionsFromBaseController = true;
                config.AutoGenerateRouteNames           = true;
            });

            routes.MapAttributeRoutes(config =>
            {
                config.AddRoutesFromAssemblyOf <MvcApplication>();
                config.AddDefaultRouteConstraint(@"[Ii]d$", new RegexRouteConstraint(@"^\d+$"));
                config.AddTranslationProvider(translationProvider);
                config.UseRouteHandler(() => new CultureAwareRouteHandler());
                config.UseLowercaseRoutes = true;
                config.InheritActionsFromBaseController = true;
                config.AutoGenerateRouteNames           = true;
            });

            routes.MapRoute("CatchAll",
                            "{*path}",
                            new { controller = "home", action = "filenotfound" },
                            new[] { typeof(HomeController).Namespace });

            // Testing issue 146
            GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new CustomWebHostBufferPolicySelector());
        }