Skip to content

Commit 087b75d

Browse files
authored
Asp.Net Core localization (#6)
Samples using ForEvolve.AspNetCore.Localization - 01-Basic - 02-Basic with Resources
1 parent 6b00742 commit 087b75d

File tree

121 files changed

+47126
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+47126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27130.2010
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Localization", "AspNetCore.Localization.csproj", "{9AF3F30F-9473-4D93-B97C-73FBF37337CB}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Debug|x64.ActiveCfg = Debug|Any CPU
21+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Debug|x64.Build.0 = Debug|Any CPU
22+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Debug|x86.ActiveCfg = Debug|Any CPU
23+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Debug|x86.Build.0 = Debug|Any CPU
24+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Release|x64.ActiveCfg = Release|Any CPU
27+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Release|x64.Build.0 = Release|Any CPU
28+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Release|x86.ActiveCfg = Release|Any CPU
29+
{9AF3F30F-9473-4D93-B97C-73FBF37337CB}.Release|x86.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {DC15F761-45B0-4B0D-8E3A-E308302D6EF0}
36+
EndGlobalSection
37+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="ForEvolve.AspNetCore.Localization" Version="1.0.0-upsilon-00009" />
9+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
10+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
15+
</ItemGroup>
16+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
using AspNetCore.Localization.Models;
8+
9+
namespace AspNetCore.Localization.Controllers
10+
{
11+
public class HomeController : Controller
12+
{
13+
public IActionResult Index()
14+
{
15+
return View();
16+
}
17+
18+
public IActionResult Contact()
19+
{
20+
var viewModel = new ContactViewModel();
21+
return View(viewModel);
22+
}
23+
24+
[HttpPost]
25+
public IActionResult Contact(ContactViewModel viewModel)
26+
{
27+
return View("ContactResult", viewModel);
28+
}
29+
30+
public IActionResult Error()
31+
{
32+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace AspNetCore.Localization.Models
8+
{
9+
public class ContactViewModel
10+
{
11+
[Required]
12+
[MaxLength(20)]
13+
[Display(Name = "First name")]
14+
public string FirstName { get; set; }
15+
16+
[Required]
17+
[MaxLength(20)]
18+
[Display(Name = "Last name")]
19+
public string LastName { get; set; }
20+
21+
[Required]
22+
[EmailAddress]
23+
public string Email { get; set; }
24+
25+
[Url]
26+
[Display(Name = "Web site")]
27+
public string WebUri { get; set; }
28+
29+
[Required]
30+
[MaxLength(50)]
31+
public string Subject { get; set; }
32+
33+
[Required]
34+
[MaxLength(2000)]
35+
public string Message { get; set; }
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace AspNetCore.Localization.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace AspNetCore.Localization
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
BuildWebHost(args).Run();
18+
}
19+
20+
public static IWebHost BuildWebHost(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>()
23+
.Build();
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:53048/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"AspNetCore.Localization": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"launchUrl": "http://localhost:53048/"
15+
}
16+
}
17+
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
10+
namespace AspNetCore.Localization
11+
{
12+
public class Startup
13+
{
14+
public Startup(IConfiguration configuration)
15+
{
16+
Configuration = configuration;
17+
}
18+
19+
public IConfiguration Configuration { get; }
20+
21+
// This method gets called by the runtime. Use this method to add services to the container.
22+
public void ConfigureServices(IServiceCollection services)
23+
{
24+
// Register localization services & options with ASP.NET Core DI container
25+
services
26+
.AddForEvolveLocalization();
27+
28+
// MVC
29+
services
30+
.AddMvc()
31+
.AddForEvolveMvcLocalization(); // Configure MVC to use ForEvolve Localization, based on configuration
32+
}
33+
34+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
35+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
36+
{
37+
if (env.IsDevelopment())
38+
{
39+
app.UseDeveloperExceptionPage();
40+
}
41+
else
42+
{
43+
app.UseExceptionHandler("/Home/Error");
44+
}
45+
46+
app.UseStaticFiles();
47+
48+
// Call UseRequestLocalization(options):
49+
// Adds the Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware to automatically
50+
// set culture information for requests based on information provided by the client.
51+
app.UseForEvolveRequestLocalization();
52+
53+
// MVC
54+
app.UseMvc(routes =>
55+
{
56+
routes.MapRoute(
57+
name: "default",
58+
template: "{controller=Home}/{action=Index}/{id?}");
59+
});
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
@model AspNetCore.Localization.Models.ContactViewModel
2+
3+
@{
4+
ViewData["Title"] = "Contact";
5+
}
6+
7+
<h2>Contact</h2>
8+
<p>
9+
<a href="?culture=fr">Français</a> |
10+
<a href="?culture=en">English</a>
11+
</p>
12+
<hr />
13+
<div class="row">
14+
<div class="col-md-4">
15+
<form asp-action="Contact">
16+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
17+
<div class="form-group">
18+
<label asp-for="FirstName" class="control-label"></label>
19+
<input asp-for="FirstName" class="form-control" />
20+
<span asp-validation-for="FirstName" class="text-danger"></span>
21+
</div>
22+
<div class="form-group">
23+
<label asp-for="LastName" class="control-label"></label>
24+
<input asp-for="LastName" class="form-control" />
25+
<span asp-validation-for="LastName" class="text-danger"></span>
26+
</div>
27+
<div class="form-group">
28+
<label asp-for="Email" class="control-label"></label>
29+
<input asp-for="Email" class="form-control" />
30+
<span asp-validation-for="Email" class="text-danger"></span>
31+
</div>
32+
<div class="form-group">
33+
<label asp-for="WebUri" class="control-label"></label>
34+
<input asp-for="WebUri" class="form-control" />
35+
<span asp-validation-for="WebUri" class="text-danger"></span>
36+
</div>
37+
<div class="form-group">
38+
<label asp-for="Subject" class="control-label"></label>
39+
<input asp-for="Subject" class="form-control" />
40+
<span asp-validation-for="Subject" class="text-danger"></span>
41+
</div>
42+
<div class="form-group">
43+
<label asp-for="Message" class="control-label"></label>
44+
<textarea asp-for="Message" class="form-control"></textarea>
45+
<span asp-validation-for="Message" class="text-danger"></span>
46+
</div>
47+
<div class="form-group">
48+
<a asp-action="Index" class="btn btn-default">Cancel</a>
49+
<input type="submit" value="Send" class="btn btn-primary" />
50+
</div>
51+
</form>
52+
</div>
53+
</div>
54+
55+
@section Scripts {
56+
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@model AspNetCore.Localization.Models.ContactViewModel
2+
@{
3+
ViewData["Title"] = "Contact";
4+
}
5+
6+
<h2>@ViewData["Title"]</h2>
7+
<div>
8+
<hr />
9+
<dl class="dl-horizontal">
10+
<dt>
11+
@Html.DisplayNameFor(model => model.FirstName)
12+
</dt>
13+
<dd>
14+
@Html.DisplayFor(model => model.FirstName)
15+
</dd>
16+
<dt>
17+
@Html.DisplayNameFor(model => model.LastName)
18+
</dt>
19+
<dd>
20+
@Html.DisplayFor(model => model.LastName)
21+
</dd>
22+
<dt>
23+
@Html.DisplayNameFor(model => model.Email)
24+
</dt>
25+
<dd>
26+
@Html.DisplayFor(model => model.Email)
27+
</dd>
28+
<dt>
29+
@Html.DisplayNameFor(model => model.WebUri)
30+
</dt>
31+
<dd>
32+
@Html.DisplayFor(model => model.WebUri)
33+
</dd>
34+
<dt>
35+
@Html.DisplayNameFor(model => model.Subject)
36+
</dt>
37+
<dd>
38+
@Html.DisplayFor(model => model.Subject)
39+
</dd>
40+
<dt>
41+
@Html.DisplayNameFor(model => model.Message)
42+
</dt>
43+
<dd>
44+
<pre>@Html.DisplayFor(model => model.Message)</pre>
45+
</dd>
46+
</dl>
47+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<h2>Home page</h2>
6+
<hr />
7+
<p>
8+
Sample:
9+
<a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a>
10+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@model ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>Development environment should not be enabled in deployed applications</strong>, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>, and restarting the application.
22+
</p>

0 commit comments

Comments
 (0)