private async Task <string> TranslateLink(string action, string controller, object data = null)
        {
            var urlHelper = GetHelper(_urlHelperFactory, _actionContextAccessor);
            var culture   = _actionContextAccessor.ActionContext.RouteData.Values["culture"].ToString();

            var translatedAction = await _localizedRoutingProvider.ProvideRouteAsync(culture, action, controller, ProvideRouteType.OriginalToTranslated);

            var translatedController = await _localizedRoutingProvider.ProvideRouteAsync(culture, controller, controller, ProvideRouteType.OriginalToTranslated);

            if (data == null)
            {
                return(urlHelper.Action(translatedAction, translatedController));
            }

            return(urlHelper.Action(translatedAction, translatedController, data));
        }
Example #2
0
        public async Task <IActionResult> ChangeLanguage(string language)
        {
            var routeInformation = await _localizedRoutingProvider.ProvideRouteAsync(language, "Home", "Index", LocalizationDirection.OriginalToTranslated);

            return(RedirectToRoute(new
            {
                controller = routeInformation.Controller,
                action = routeInformation.Action,
                culture = language
            }));
        }
Example #3
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            var currentUICulture  = CultureInfo.CurrentUICulture;
            var currentController = HttpContext.Request.RouteValues["controller"].ToString();
            var currentAction     = HttpContext.Request.RouteValues["action"].ToString();
            var urlPattern        = String.Empty;

            if (HttpContext.Request.RouteValues["url_pattern"] != null)
            {
                urlPattern = HttpContext.Request.RouteValues["url_pattern"].ToString();
            }

            LocalizationDirection translationDirection = LocalizationDirection.TranslatedToOriginal;

            var cultureOptions = new List <LanguageSwitcherOption>();

            foreach (CultureInfo ci in _localizationOptions.Value.SupportedCultures)
            {
                if (ci.Name != DefaultCulture)
                {
                    translationDirection = LocalizationDirection.OriginalToTranslated;
                }

                var translation = await _localizedRoutingProvider.ProvideRouteAsync(
                    ci.Name,
                    currentController,
                    currentAction,
                    translationDirection);

                var option = new LanguageSwitcherOption
                {
                    DisplayName          = ci.NativeName.Substring(0, ci.NativeName.IndexOf("(")),
                    CultureCode          = ci.Name,
                    TranslatedController = translation.Controller,
                    TranslatedAction     = translation.Action,
                    UrlPattern           = urlPattern
                };

                cultureOptions.Add(option);
            }

            var languageSwitcher = new LanguageSwitcher
            {
                CurrentUICulture = currentUICulture,
                CultureOptions   = cultureOptions
            };

            return(View("languageSelector", languageSwitcher));
        }
Example #4
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "a";

            var culture = _contextAccessor.ActionContext.RouteData.Values["culture"]?.ToString();
            var routeInformationMetadata = await _localizedRoutingProvider.ProvideRouteAsync(culture, Controller, Action, LocalizationDirection.OriginalToTranslated);

            if (routeInformationMetadata != null)
            {
                Controller = routeInformationMetadata.Controller;
                Action     = routeInformationMetadata.Action;
            }

            await base.ProcessAsync(context, output);
        }
        public async Task <RouteValueDictionary> ResolveAsync(RouteValueDictionary values)
        {
            if (!values.TryGetValue("culture", out var culture) ||
                !values.TryGetValue("controller", out var controller) ||
                !values.TryGetValue("action", out var action))
            {
                return(values);
            }

            var routeInformationMetadata = await _localizedRoutingProvider.ProvideRouteAsync((string)culture, (string)controller, (string)action, LocalizationDirection.TranslatedToOriginal);

            values["controller"] = routeInformationMetadata.Controller;
            values["action"]     = routeInformationMetadata.Action;

            return(values);
        }