app.UseRequestLocalization(options =>
{
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR"), new CultureInfo("zh-CN"), new CultureInfo("ja-JP") };
options.SupportedUICultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR"), new CultureInfo("zh-CN"), new CultureInfo("ja-JP") };
});
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
app.UseRequestLocalization(options =>
{
options.DefaultRequestCulture = new RequestCulture("de-DE");
options.SupportedCultures = new[] { new CultureInfo("de-DE") };
options.SupportedUICultures = new[] { new CultureInfo("de-DE") };
});
在区域中添加对应区域文件夹和资源文件,例如Areas/Products/Resources/Products.en-US.resx
在区域中添加对应的本地化视图,例如Areas/Products/Views/Products.Edit.en-US.cshtml
在区域中使用[Route]特性和RouteData动态配置路由,例如:
[Route("en-US/[area]/[controller]/[action]")]
public class ProductsController : Controller
{
...
}
@product.Name
这样就可以使用区域路由和本地化来实现多语言的