Globalization and localization
****************************************************************************************************
using Microsoft.AspNet.Localization;
using Microsoft.AspNet.Mvc.Localization; namespace Localization.StarterWeb.Controllers
{
public class BookController : Controller
{
//private readonly IStringLocalizer<AboutController> _localizer;
private readonly IHtmlLocalizer<BookController> _localizer; public BookController(IHtmlLocalizer<BookController> localizer)
{
_localizer = localizer;
} public IActionResult Hello(string name)
{
ViewData["Message"] = _localizer["<b>Hello</b><i> {0}</i>", name]; return View();
} public class TestController : Controller
{
private readonly IStringLocalizer _localizer;
private readonly IStringLocalizer _localizer2; public TestController(IStringLocalizerFactory factory)
{
_localizer = factory.Create(typeof(SharedResource));
_localizer2 = factory.Create("SharedResource", location: null);
} public IActionResult About()
{
ViewData["Message"] = _localizer["Your application description page."]
+ " loc 2: " + _localizer2["Your application description page."]; return View();
} public class SharedResource
{
}
public class InfoController : Controller
{
private readonly IStringLocalizer<InfoController> _localizer;
private readonly IStringLocalizer<SharedResource> _sharedLocalizer; public InfoController(IStringLocalizer<InfoController> localizer,
IStringLocalizer<SharedResource> sharedLocalizer)
{
_localizer = localizer;
_sharedLocalizer = sharedLocalizer;
} public string TestLoc()
{
string msg = "Shared resx: " + _sharedLocalizer["Hello!"] +
" Info resx " + _localizer["Hello!"];
return msg;
}
**************************************************************************************************** View localization @using Microsoft.AspNet.Mvc.Localization @inject IViewLocalizer Localizer @{
ViewData["Title"] = Localizer["About"];
}
<h2>@ViewData["Title"].</h2>
<h3>@ViewData["Message"]</h3> <p>@Localizer["Use this area to provide additional information."]</p> ----------------------------------------------------------------------------------
@Localizer["<i>Hello</i> <b>{0}!</b>", UserManager.GetUserName(User)]
----------------------------------------------------------------------------------------
@using Microsoft.AspNet.Mvc.Localization
@using Localization.StarterWeb.Services @inject IViewLocalizer Localizer
@inject IHtmlLocalizer<SharedResource> SharedLocalizer @{
ViewData["Title"] = Localizer["About"];
}
<h2>@ViewData["Title"].</h2> <h1>@SharedLocalizer["Hello!"]</h1> ---------------------------------------------------------------------------------------- DataAnnotations localization DataAnnotations error messages are localized with IStringLocalizer<T>. Using the option ResourcesPath = "Resources", the error messages in RegisterViewModel can be stored in either of the following paths:
•Resources/ViewModels.Account.RegisterViewModel.fr.resx
•Resources/ViewModels/Account/RegisterViewModel.fr.resx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class RegisterViewModel
{
[Required(ErrorMessage = "The Email field is required.")]
[EmailAddress(ErrorMessage = "The Email field is not a valid e-mail address.")]
[Display(Name = "Email")]
public string Email { get; set; } [Required(ErrorMessage = "The Password field is required.")]
[StringLength(, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = )]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; } [DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
} The runtime doesn’t look up localized strings for non-validation attributes. In the code above, “Email” (from [Display(Name = "Email")]) will not be localized.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Configuring localization public void ConfigureServices(IServiceCollection services)
{ services.AddLocalization(options => options.ResourcesPath = "Resources"); services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
-------------------------------------------------------------------------------------
Localization middleware public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en-AU"),
new CultureInfo("en-GB"),
new CultureInfo("en"),
new CultureInfo("es-ES"),
new CultureInfo("es-MX"),
new CultureInfo("es"),
new CultureInfo("fr-FR"),
new CultureInfo("fr"),
}; app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
}); .QueryStringRequestCultureProvider
.CookieRequestCultureProvider
.AcceptLanguageHeaderRequestCultureProvider
--------------------------------------------------------------------------------------------
Using a custom provider:
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr")
}; options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures; options.RequestCultureProviders.Insert(, new CustomRequestCultureProvider(async context =>
{
// My custom request culture logic
return new ProviderCultureResult("en");
}));
}); --------------------------------------------------------------------------------------------
Resource file naming: Resource name Dot or path naming
Resources/Controllers.HomeController.fr.resx Dot
Resources/Controllers/HomeController.fr.resx Path Setting the culture programmatically
The Views/Shared/_SelectLanguagePartial.cshtml file allows you to select the culture from the list of supported cultures:
@using Microsoft.AspNet.Builder
@using Microsoft.AspNet.Http.Features
@using Microsoft.AspNet.Localization
@using Microsoft.AspNet.Mvc.Localization
@using Microsoft.Extensions.Options @inject IViewLocalizer Localizer
@inject IOptions<RequestLocalizationOptions> LocOptions @{
var requestCulture = Context.Features.Get<IRequestCultureFeature>();
var cultureItems = LocOptions.Value.SupportedUICultures
.Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
.ToList();
} <div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
<form id="selectLanguage" asp-controller="Home"
asp-action="SetLanguage" asp-route-returnUrl="@Context.Request.Path"
method="post" class="form-horizontal" role="form">
@Localizer["Language:"] <select name="culture"
asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems">
</select>
</form>
</div> <div class="container body-content">
@RenderBody()
<hr />
<footer>
<div class="row">
<div class="col-md-6">
<p>&copy; - Localization.StarterWeb</p>
</div>
<div class="col-md-6 text-right">
@await Html.PartialAsync("_SelectLanguagePartial")
</div>
</div>
</footer>
</div> [HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears() }
); return LocalRedirect(returnUrl);
} Terms:
•Globalization (G11N): The process of making an app support different languages and regions.
•Localization (L10N): The process of customizing an app for a given language and region.
•Internationalization (I18N): Describes both globalization and localization.
•Culture: It is a language and, optionally, a region.
•Neutral culture: A culture that has a specified language, but not a region. (for example “en”, “es”)
•Specific culture: A culture that has a specified language and region. (for example “en-US”, “en-GB”, “ es-CL”)
•Locale: A locale is the same as a culture.

最新文章

  1. 【CodeVS 2822】爱在心中
  2. java继承关系中成员变量,构造方法,成员方法的关系
  3. new Image()的用途
  4. Java 编程的动态性,第 7 部分: 用 BCEL 设计字节码--转载
  5. C# List使用District去重复数据
  6. Winform带dataGridview的Combox控件
  7. Day4-迭代器
  8. vue-cli项目多页面配置
  9. js 实现仿 淘宝 五星评价 demo
  10. 【log4j2】log4j的升级版log4j2的简单入门使用
  11. Vue相关开源项目库汇总(史上最全)
  12. hbase.client.keyvalue.maxsize的默认值
  13. cobbler的ks文件带有特殊字符导致错误
  14. Multi-View Region Adaptive Multi-temporal DMM and RGB Action Recognition
  15. Python从入门到放弃系列(Django/Flask/爬虫)
  16. u-boot 编译,启动流程分析,移植
  17. Dell服务器Raid5之后安装系统
  18. IE下 input 的父级标签被 disabled 之后引发的怪异行为
  19. bata2
  20. Struts2 (下)

热门文章

  1. 「赛后补题」HBCPC2018题目代码与思路简析
  2. JMeter学习笔记(九) 参数化3--User Defined Variables
  3. Linux复制和移动文件
  4. BFS实现模板
  5. 【转】Virtual DOM
  6. 在C的头文件中定义的结构体,如何在cpp文件中引用
  7. maven第一个HelloWorld
  8. Spark+Python+Pycharm在Windows下的配置
  9. 51nod 1831 小C的游戏(博弈论+打表)
  10. [CF327E]Axis Walking([洛谷P2396]yyy loves Maths VII)