I have developed my first ASP.NET 5 web application with this characteristics:
我开发了我的第一个ASP。NET 5 web应用程序具有以下特点:
- ASP.NET 5 (Core 1.0)
- ASP。1.0净5(核心)
- MVC 6
- MVC 6
- EF 7
- EF 7
I'm trying to translate it to multilanguage and that the user could be able to change the language with a selector, but I'm not able to achieving.
我想把它翻译成多语言用户可以用选择器改变语言,但我做不到。
I had developed ASP.NET 2.0/3.5 projects before, and I remember that the texts were into resource (.resx) files or into an XML file. But now, as a lot of things that has changed recently in the new ASP, I think the localization also has changed.
我已经开发了ASP。NET 2.0/3.5之前的项目,我记得这些文本被放入资源(.resx)文件或XML文件中。但是现在,在新的ASP中发生了很多变化,我认为本地化也发生了变化。
I have found very little information about it, and that I have found, doesn't help me enough.
我发现的关于它的信息很少,而且我发现的这些信息对我帮助不大。
In the official support website doesn't explain this topic yet (docs.asp.net).
在官方支持网站上还没有解释这个主题(doc .asp.net)。
I have found an GitHub code sample, and interesting explanation in this website.
我在这个网站上找到了一个GitHub代码示例和有趣的解释。
But I'm no be able to linking concepts. I'm going to explain the steps that I have done.
但是我不能把概念联系起来。我将解释我所做的步骤。
I have created a "Resource" folder, into my ASP.NET project. Into this folder, I have created some (.resx) files. The name of this files follows the structure:
我已经在我的ASP中创建了一个“资源”文件夹。网络项目。在这个文件夹中,我创建了一些(.resx)文件。该文件的名称遵循以下结构:
- Controllers.NameOfController.ca-ES.resx
- Controllers.NameOfController.ca-ES.resx
- Views.NameOfView.ca-ES.resx
- Views.NameOfView.ca-ES.resx
- Models.NameOfModel.ca-ES.resx
- Models.NameOfModel.ca-ES.resx
In my Startup.cs file, I have defined the following code to set which folder are the text resources:
在我的创业公司。cs文件中,我定义了以下代码来设置哪个文件夹是文本资源:
public void ConfigureServices(IServiceCollection services)
{
...
services
.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
...
}
In the same file, in the Configure method, I have defined:
在同一个文件中,在Configure方法中,我定义了:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
//template: "{culture?}/{controller}/{action}/{id?}", //we define the structure of the routes
//defaults: new { culture = "ca", controller = "Home", action = "Index" }); //we define the default values
});
And also in the same function, the supported cultures:
在同样的功能中,被支持的文化:
//To set the localization configuration
List<CultureInfo> supportedCultures = new List<CultureInfo>();
supportedCultures.Add(new CultureInfo("ca-ES"));
supportedCultures.Add(new CultureInfo("es-ES"));
List<CultureInfo> supportedUICultures = new List<CultureInfo>();
supportedUICultures.Add(new CultureInfo("ca-ES"));
supportedUICultures.Add(new CultureInfo("es-ES"));
RequestLocalizationOptions requestLocalizationOptions = new RequestLocalizationOptions();
requestLocalizationOptions.SupportedCultures = supportedCultures;
requestLocalizationOptions.SupportedUICultures = supportedUICultures;
RequestCulture defaultRequestCulture = new RequestCulture("ca-ES");
app.UseRequestLocalization(requestLocalizationOptions, defaultRequestCulture);
And now is the point that I'm lost.
现在我迷失了方向。
- I have read, that I have to develop an controller class that beacome the responsible to manage the localization. I'm right?
- 我读过,我必须开发一个控制器类,因为负责管理本地化。我是正确的吗?
- I have read, that exists multiple ways to define the language that I have to display: from the query string, from a cookie, from the Accept-Language HTTP header, from the DefaultRequestCulture property of the RequestLocalizationOptions class, from the thread culture. I don't know if I have to consider all of this options or one is enough.
- 我已经读过,有多种方法可以定义我必须显示的语言:从查询字符串、从cookie、从接受语言HTTP头、从RequestLocalizationOptions类的DefaultRequestCulture属性、从线程文化。我不知道我是否需要考虑所有这些选项或者一个就足够了。
- .resx are active? Are they the good solution? Can I use it in ASP :NET 5?
- . resx活跃吗?它们是好的解决方案吗?我可以在ASP:NET 5中使用吗?
- How I can do a selector to commute the language?
- 如何做选择符来转换语言?
EDIT:
编辑:
I have checked the documentation again, and I have seen in the comments that Rick Anderson has been writing an entry. I haven't read it in calm yet, but I share with you if somebody is interested about this topic.
我再次检查了文档,我在评论中看到Rick Anderson正在写一个条目。我还没平静地读过,但如果有人对这个话题感兴趣,我可以和你们分享。
2 个解决方案
#1
1
I have reticently done an example which demonstrates all aspects of localization in ASP.NET 5 (ASP.NET Core 1.0). You can download it and have a look https://github.com/feradz/ASPNetCoreLocalization/wiki. Download and try it.
我已经做了一个简单的例子,演示了ASP中本地化的所有方面。净5(ASP。网络核心1.0)。您可以下载它并查看https://github.com/feradz/aspnetcorelocaliz/wiki。下载并试用。
Answering your questions:
回答你的问题:
- You don't need any controller class that will be responsible to manage the localization.
- 您不需要任何负责管理本地化的控制器类。
- There are different ways to set the default language. The example project demonstrates using cookie
- 设置默认语言有不同的方法。示例项目演示了如何使用cookie
- I didn't understand this question.
- 我不明白这个问题。
- The example project has a language selector.
- 示例项目有一个语言选择器。
The example demonstrates the following features:
这个例子演示了以下特性:
- Localization with
IStringLocalizer
- 本地化与IStringLocalizer
- Localization with
IHtmlLocalizer
- 本地化与IHtmlLocalizer
- Localization with
IViewLocalizer
- 本地化与IViewLocalizer
- Localization using shared resource file
- 使用共享资源文件进行本地化
- Localization using per class controller resource file
- 使用每个类控制器资源文件进行本地化
- Localization of error messages for a view model
- 视图模型错误消息的本地化
- Localization of the
Display
attribute of a model property using prior ASP.NET Core 1.0 resources - 使用先前的ASP对模型属性的显示属性进行本地化。1.0网络核心资源
- Localization for enums and enum elements using
EnumHelper
- 使用枚举助手对枚举和枚举元素进行本地化
- Localization for Views e.g.
View.cshtml
View.es-ES.cshtml
- 视图的本地化。cshtml View.es-ES.cshtml
- Example of switching culture using cookie and select input
CookieRequestCultureProvider
- 使用cookie切换区域性并选择输入CookieRequestCultureProvider的示例
#2
0
-
In the ConfigureServices(...) put the command
在ConfigureServices(…)中放置命令。
services.AddLocalization(options => options.ResourcesPath = "Resources");
服务。AddLocalization(选项= >选项。ResourcesPath = "资源");
-
It is very important that the command
命令是非常重要的
app.UseRequestLocalization(...)
app.UseRequestLocalization(…)
must be before all other commands in the method Configure(...). Otherwise localization just not works.
必须在方法Configure(…)中的所有其他命令之前。否则本地化就不管用了。
Maybe that will help you.
也许这对你有帮助。
#1
1
I have reticently done an example which demonstrates all aspects of localization in ASP.NET 5 (ASP.NET Core 1.0). You can download it and have a look https://github.com/feradz/ASPNetCoreLocalization/wiki. Download and try it.
我已经做了一个简单的例子,演示了ASP中本地化的所有方面。净5(ASP。网络核心1.0)。您可以下载它并查看https://github.com/feradz/aspnetcorelocaliz/wiki。下载并试用。
Answering your questions:
回答你的问题:
- You don't need any controller class that will be responsible to manage the localization.
- 您不需要任何负责管理本地化的控制器类。
- There are different ways to set the default language. The example project demonstrates using cookie
- 设置默认语言有不同的方法。示例项目演示了如何使用cookie
- I didn't understand this question.
- 我不明白这个问题。
- The example project has a language selector.
- 示例项目有一个语言选择器。
The example demonstrates the following features:
这个例子演示了以下特性:
- Localization with
IStringLocalizer
- 本地化与IStringLocalizer
- Localization with
IHtmlLocalizer
- 本地化与IHtmlLocalizer
- Localization with
IViewLocalizer
- 本地化与IViewLocalizer
- Localization using shared resource file
- 使用共享资源文件进行本地化
- Localization using per class controller resource file
- 使用每个类控制器资源文件进行本地化
- Localization of error messages for a view model
- 视图模型错误消息的本地化
- Localization of the
Display
attribute of a model property using prior ASP.NET Core 1.0 resources - 使用先前的ASP对模型属性的显示属性进行本地化。1.0网络核心资源
- Localization for enums and enum elements using
EnumHelper
- 使用枚举助手对枚举和枚举元素进行本地化
- Localization for Views e.g.
View.cshtml
View.es-ES.cshtml
- 视图的本地化。cshtml View.es-ES.cshtml
- Example of switching culture using cookie and select input
CookieRequestCultureProvider
- 使用cookie切换区域性并选择输入CookieRequestCultureProvider的示例
#2
0
-
In the ConfigureServices(...) put the command
在ConfigureServices(…)中放置命令。
services.AddLocalization(options => options.ResourcesPath = "Resources");
服务。AddLocalization(选项= >选项。ResourcesPath = "资源");
-
It is very important that the command
命令是非常重要的
app.UseRequestLocalization(...)
app.UseRequestLocalization(…)
must be before all other commands in the method Configure(...). Otherwise localization just not works.
必须在方法Configure(…)中的所有其他命令之前。否则本地化就不管用了。
Maybe that will help you.
也许这对你有帮助。