Exemplo n.º 1
0
        /// <inheritdoc />
        public virtual LocalizedString GetString(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            return(_localizer.GetString(name));
        }
        /// <summary>
        ///   The function responsible for acquiring the text to localize, getting the required
        ///   parameters to pass to the localized text and replacing the old text with the new localized text.
        /// </summary>
        /// <inheritdoc />
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (output.Attributes.ContainsName("localize"))
            {
                int index = output.Attributes.IndexOfName("localize");
                output.Attributes.RemoveAt(index);
            }

            var content = await GetContentAsync(context, output);

            if (TrimWhitespace || TrimEachLine)
            {
                content = content?.Trim();
            }

            if (NewLineHandling != NewLineHandling.None || TrimEachLine)
            {
                content = HandleNormalization(content, NewLineHandling, TrimEachLine);
            }

            var parameters = GetParameters(context);

            if (IsHtml)
            {
                LocalizedHtmlString locString;
                if (parameters.Any())
                {
                    locString = localizer[content, parameters.ToArray()];
                }
                else
                {
                    locString = localizer[content];
                }

                SetHtmlContent(context, output.Content, locString);
            }
            else
            {
                LocalizedString locString;
                if (parameters.Any())
                {
                    locString = localizer.GetString(content, parameters.ToArray());
                }
                else
                {
                    locString = localizer.GetString(content);
                }

                SetContent(context, output.Content, locString);
            }
        }
        public static LocalizedHtmlString Plural(this IHtmlLocalizer localizer, int count, string singular, string plural, params object[] arguments)
        {
            if (plural == null)
            {
                throw new ArgumentNullException(nameof(plural), "Plural text can't be null. If you don't want to specify the plural text, use IStringLocalizer without Plural extention.");
            }

            var localizedString = localizer.GetString(singular, new PluralizationArgument {
                Count = count, Forms = new[] { singular, plural }, Arguments = arguments
            });

            return(new LocalizedHtmlString(singular, localizedString));
        }
Exemplo n.º 4
0
        /// <summary>
        ///   Synchronously executes the <see cref="T:Microsoft.AspNetCore.Razor.TagHelpers.TagHelper" />
        ///   with the given <paramref name="context" /> and <paramref name="output" />. This is the
        ///   method responsible for localizing the html attributes
        /// </summary>
        /// <param name="context">Contains information associated with the current HTML tag.</param>
        /// <param name="output">A stateful HTML element used to generate an HTML tag.</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            foreach (var attribute in AttributeValues)
            {
                string key   = attribute.Key;
                string value = attribute.Value;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    string newValue = localizer.GetString(value);

                    output.Attributes.Add(key, newValue);
                }
            }
        }
        /// <summary>
        ///   Synchronously executes the <see cref="T:Microsoft.AspNetCore.Razor.TagHelpers.TagHelper" />
        ///   with the given <paramref name="context" /> and <paramref name="output" />. This is the
        ///   method responsible for localizing the html attributes.
        /// </summary>
        /// <param name="context">Contains information associated with the current HTML tag.</param>
        /// <param name="output">A stateful HTML element used to generate an HTML tag.</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            foreach (var attribute in AttributeValues)
            {
                string key   = attribute.Key;
                string value = attribute.Value;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    string newValue = null;

                    if (ParameterValues.ContainsKey(key))
                    {
                        newValue = localizer.GetString(value, ParameterValues[key].Split(';'));
                    }
                    else
                    {
                        newValue = localizer.GetString(value);
                    }

                    output.Attributes.Add(key, newValue);
                }
            }
        }
        public static LocalizedHtmlString Plural(this IHtmlLocalizer localizer, int count, string[] pluralForms, params object[] arguments)
        {
            if (pluralForms == null)
            {
                throw new ArgumentNullException(nameof(pluralForms), "PluralForms array can't be null. If you don't want to specify the plural text, use IStringLocalizer without Plural extention.");
            }

            if (pluralForms.Length == 0)
            {
                throw new ArgumentException(nameof(pluralForms), "PluralForms array can't be empty, it must contain at least one element. If you don't want to specify the plural text, use IStringLocalizer without Plural extention.");
            }

            var name            = pluralForms[0];
            var localizedString = localizer.GetString(name, new PluralizationArgument {
                Count = count, Forms = pluralForms, Arguments = arguments
            });

            return(new LocalizedHtmlString(name, localizedString));
        }
        public IActionResult Privacy()
        {
            List <textbyculture> tbc = new List <textbyculture>()
            {
                new textbyculture("en", "this english text of privcy."),
                new textbyculture("fa", "این متن فارسی حریم شخصی است."),
                new textbyculture("ar", "العربی متون اف پرایوسی.")
            };
            var culture     = HttpContext.Features.Get <Microsoft.AspNetCore.Localization.IRequestCultureFeature>();
            var cultureList = locOptions.Value.SupportedUICultures.Select(x => new SelectListItem()
            {
                Value = x.Name,
                Text  = _localizer.GetString(x.Name)
            }).ToList();
            //var returnUrl = string.IsNullOrWhiteSpace(HttpContext.Request.Path) ? "~/" : $"~{HttpContext.Request.Path}{HttpContext.Request.QueryString}";

            string sText = tbc.FirstOrDefault(a => a.key == culture.RequestCulture.Culture.Name).val;

            ViewData["text"] = sText;
            return(View());
        }
 /// <inheritdoc />
 public LocalizedString GetString(string name)
 {
     return(_htmlLocalizer.GetString(name));
 }
Exemplo n.º 9
0
 /// <inheritdoc />
 public LocalizedString GetString(string name) => _localizer.GetString(name);
Exemplo n.º 10
0
 /// <inheritdoc />
 public virtual LocalizedString GetString([NotNull] string key) => _localizer.GetString(key);
Exemplo n.º 11
0
 /// <inheritdoc />
 public LocalizedString GetString([NotNull] string name) => _localizer.GetString(name);
Exemplo n.º 12
0
 public virtual LocalizedString GetString(string key) => _localizer.GetString(key);
Exemplo n.º 13
0
        public async Task <IActionResult> Index([FromRoute] string language, [FromRoute] string amp)
        {
            var xx = ViewBag.data;

            ViewBag.data = "controller value";

            // await EmailService.SendAsync(
            //   recipients: "*****@*****.**",
            //   subject: "Best Way Website Enquiry",
            //   templatePath: "~/EmailTemplate/Contact/Index.cshtml",
            //   replyTos: "*****@*****.**",
            //   model: new Email.Contact.ViewModel
            //   {
            //       email = "*****@*****.**",
            //       message = "msg",
            //       name = "keatkeat",
            //       subject = "test"
            //   }
            //   // attachs: model.fileFullPaths.Select(f => new Attachment(f)).ToList()
            //);


            //var x = Url.Action("Index", "AboutUsController", null, "https", "www.stooges.com.my");

            var resultA = HtmlLocalizer["test {0} with params", "dada"];
            var value   = resultA.Value;                                     // 注意 :这里很奇葩的哦, 是翻译成功了,但是 dada 没有被放入 {0} 里面, 这个放入的动作是留给 razor view 执行的.

            value = HtmlLocalizer.GetString("test {0} with params", "dada"); //用 GetString 就可以完全成功,我是不知道为什么啦


            var resultB     = StringLocalizer["test {0} with params", "dada"];
            var resultBInEn = StringLocalizer.WithCulture(new CultureInfo("en"))["test {0} with params", "dada"]; // 可以动态选语言哦.


            var resultAA = HtmlLocalizer["<p>test html with {0}</p>", "<div>123</div>"];
            var resultBB = StringLocalizer["<p>test html with {0}</p>", "<div>123</div>"];

            var requestCulture = HttpContextAccessor.HttpContext.Features.Get <IRequestCultureFeature>(); // 获取请求的 locale


            //var url = Url.RouteUrl(new UrlRouteContext
            //{
            //    RouteName = "about",
            //    Values = new
            //    {
            //        language = "cn",
            //        name = "keatkeat",
            //        age = 11,
            //        currentYear = true
            //    }
            //});

            //var url2 = Url.RouteUrl(new UrlRouteContext
            //{
            //    RouteName = "about",
            //    Values = new
            //    {
            //        language = "cn",
            //        age = 11,
            //        currentYear = true
            //    }
            //});

            if (string.IsNullOrEmpty(amp))
            {
                return(View());
            }
            else
            {
                return(View("~/Web/Home/Amp/Index.cshtml"));
            }
        }