コード例 #1
0
        public static IEnumerable <SelectorModel> GetUntranslatedSelectorsFor(this ControllerModel originalControllerModel,
                                                                              ILocalizer localizer, string culture)
        {
            ControllerModel localizedControllerModel = originalControllerModel.GetLocalizedModelFor(localizer, culture);

            return(originalControllerModel.Selectors.Skip(localizedControllerModel.Selectors.Count));
        }
コード例 #2
0
        public static ActionModel GetLocalizedModelFor(this ActionModel originalActionModel, ILocalizer localizer,
                                                       string culture)
        {
            ControllerModel originalControllerModel  = originalActionModel.Controller;
            ControllerModel localizedControllerModel = originalControllerModel.GetLocalizedModelFor(localizer, culture);

            return(localizedControllerModel.Actions.Single(action => action.MatchesActionName(originalActionModel.ActionName) &&
                                                           action.MatchesActionArguments(originalActionModel.Parameters)));
        }
コード例 #3
0
        public void GetLocalizedModelForLocalizedControllerThrowsException()
        {
            ApplicationModel applicationModel     = TestApplicationModel.Instance;
            ControllerModel  controller           = applicationModel.Controller1();
            ControllerModel  translatedController =
                controller.CreateLocalizedControllerModel(GetLocalizedRouteMarker(), ModelExtensionTest.Culture);

            applicationModel.Controllers.Add(translatedController);

            ControllerModel translatedController2 =
                translatedController.GetLocalizedModelFor(GetLocalizedRouteMarker(), ModelExtensionTest.Culture);
        }
コード例 #4
0
        public void GetLocalizedModelForOriginalControllerReturnsLocalizedController()
        {
            ApplicationModel applicationModel     = TestApplicationModel.Instance;
            ControllerModel  controller           = applicationModel.Controller1();
            ControllerModel  translatedController =
                controller.CreateLocalizedControllerModel(GetLocalizedRouteMarker(), ModelExtensionTest.Culture);

            applicationModel.Controllers.Add(translatedController);

            ControllerModel translatedController2 =
                controller.GetLocalizedModelFor(GetLocalizedRouteMarker(), ModelExtensionTest.Culture);

            Assert.AreSame(translatedController, translatedController2);
        }
        public override void Process(ICollection <RouteSelection> routeSelections)
        {
            foreach (RouteSelection routeSelection in routeSelections)
            {
                ControllerModel           originalControllerModel = routeSelection.ControllerModel;
                ICollection <ActionModel> originalActionModels    = routeSelection.ActionModels;

                originalControllerModel.ThrowIfNotOriginalModel(RouteTranslationConfiguration.Localizer);
                originalActionModels.ThrowIfNotPartOfModel(originalControllerModel);

                // Expected to remove controller route
                if (routeSelection.ActionModels.Count == 0)
                {
                    originalControllerModel.ThrowIfNotModelWithAttributeRoutes(RouteTranslationConfiguration.Localizer);

                    ICollection <ControllerModel> translatedControllerModels = Cultures
                                                                               .Select(culture => originalControllerModel.GetLocalizedModelFor(RouteTranslationConfiguration.Localizer, culture))
                                                                               .ToList();

                    translatedControllerModels.ThrowIfNotAllPartiallyTranslated(RouteTranslationConfiguration.Localizer);

                    // Throw if not controller only attribute route or all action attribute routes completely translated
                }

                bool HasNoActionAttributeRoute(ControllerModel model)
                {
                    return(originalControllerModel.Actions.All(action => !action.HasAttributeRoutes()));
                }

                bool HasCompletelyTranslatedActionAttributeRoutes(ControllerModel model)
                {
                    return(Cultures.All(culture => model.Actions.All(action => !action.HasAttributeRoutes() || action
                                                                     .TryGetLocalizedModelFor(RouteTranslationConfiguration.Localizer, culture)
                                                                     ?.IsCompletelyTranslated(RouteTranslationConfiguration.Localizer) == true)));
                }

                // Remove only if controller only attribute route or all action attribute routes completely translated
                if (originalControllerModel.HasAttributeRoutes() && (HasNoActionAttributeRoute(originalControllerModel) ||
                                                                     HasCompletelyTranslatedActionAttributeRoutes(originalControllerModel)))
                {
                    int minimumNumberOfPartialTranslations = Cultures.Min(culture => originalControllerModel
                                                                          .GetLocalizedModelFor(RouteTranslationConfiguration.Localizer, culture)
                                                                          .Selectors.Count);

                    for (int i = 0; i < minimumNumberOfPartialTranslations; i++)
                    {
                        if (!originalControllerModel.Selectors[i].ActionConstraints.OfType <NeverAcceptActionContraint>().Any())
                        {
                            Logger.LogDebug(
                                $"Disable selector for controller {originalControllerModel.ControllerName} with template \"{originalControllerModel.Selectors[i].AttributeRouteModel.Template}\".");

                            // TODO: Verify if this is the best solution
                            originalControllerModel.Selectors[i].ActionConstraints.Add(new NeverAcceptActionContraint());
                        }
                    }
                }

                if (routeSelection.ActionModels.Count == 0)
                {
                    continue;
                }

                originalActionModels.ThrowIfNotModelsWithAttributeRoutes(RouteTranslationConfiguration.Localizer);

                foreach (string culture in Cultures)
                {
                    IList <ActionModel> translatedActionModels = originalActionModels
                                                                 .Select(action => action.GetLocalizedModelFor(RouteTranslationConfiguration.Localizer, culture))
                                                                 .ToList();

                    translatedActionModels.ThrowIfNotAllPartiallyTranslated(RouteTranslationConfiguration.Localizer);
                }

                foreach (ActionModel originalActionModel in originalActionModels)
                {
                    int minimumNumberOfPartialTranslations = Cultures.Min(culture => originalActionModel
                                                                          .GetLocalizedModelFor(RouteTranslationConfiguration.Localizer, culture)
                                                                          .Selectors.Count);

                    for (int i = 0; i < minimumNumberOfPartialTranslations; i++)
                    {
                        if (!originalActionModel.Selectors[i].ActionConstraints.OfType <NeverAcceptActionContraint>().Any())
                        {
                            Logger.LogDebug(
                                $"Disable selector for action {originalActionModel.Controller.ControllerName}:{originalActionModel.ActionName} with template \"{originalActionModel.Selectors[i].AttributeRouteModel.Template}\".");

                            // TODO: Verify if this is the best solution
                            originalActionModel.Selectors[i].ActionConstraints.Add(new NeverAcceptActionContraint());
                        }
                    }
                }
            }
        }