Skip to content

Commit

Permalink
Initial created
Browse files Browse the repository at this point in the history
  • Loading branch information
arbems committed Aug 29, 2023
1 parent 9608635 commit b5b53bc
Show file tree
Hide file tree
Showing 109 changed files with 77,340 additions and 0 deletions.
30 changes: 30 additions & 0 deletions AspNetCoreLocalizationSamples.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33122.133
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Backend", "src\Backend\Backend.csproj", "{F5E7383C-FB95-423D-956C-CA904B0129E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Frontend", "src\Frontend\Frontend.csproj", "{7152C950-E4CF-402F-8FD6-88B11FB2A8AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F5E7383C-FB95-423D-956C-CA904B0129E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5E7383C-FB95-423D-956C-CA904B0129E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5E7383C-FB95-423D-956C-CA904B0129E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5E7383C-FB95-423D-956C-CA904B0129E8}.Release|Any CPU.Build.0 = Release|Any CPU
{7152C950-E4CF-402F-8FD6-88B11FB2A8AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7152C950-E4CF-402F-8FD6-88B11FB2A8AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7152C950-E4CF-402F-8FD6-88B11FB2A8AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7152C950-E4CF-402F-8FD6-88B11FB2A8AD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2B6DD9C8-812D-472F-AFEB-A5817374C14D}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions src/Backend/Backend.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions src/Backend/Controllers/AboutController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;

namespace Backend.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class AboutController : ControllerBase
{
private readonly IStringLocalizer<AboutController> _localizer;

public AboutController(IStringLocalizer<AboutController> localizer)
{
_localizer = localizer;
}

[HttpGet]
public string Get()
{
return _localizer["About Title"];
}
}
}
38 changes: 38 additions & 0 deletions src/Backend/Controllers/CookiesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;

namespace Backend.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CookiesController : ControllerBase
{
[HttpGet("GetLanguage")]
public IActionResult GetLanguage()
{
var currentLanguage = HttpContext.Features.Get<IRequestCultureFeature>()?
.RequestCulture.Culture.Name;

return Ok(currentLanguage);
}

[HttpPost("SetLanguage")]
public IActionResult SetLanguage(string culture)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);

return Ok();
}

[HttpDelete("ClearLanguage")]
public IActionResult ClearLanguage()
{
Response.Cookies.Delete(CookieRequestCultureProvider.DefaultCookieName);
return Ok();
}
}
}
23 changes: 23 additions & 0 deletions src/Backend/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;

namespace Backend.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class HomeController : ControllerBase
{
private readonly IStringLocalizer<SharedResource> _sharedLocalizer;

public HomeController(IStringLocalizer<SharedResource> sharedLocalizer)
{
_sharedLocalizer = sharedLocalizer;
}

[HttpGet]
public IActionResult Get()
{
return Ok(_sharedLocalizer["Your application shared resources."].Value);
}
}
}
24 changes: 24 additions & 0 deletions src/Backend/Controllers/RouteController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;

namespace Backend.Controllers
{
[Route("api/{culture}/[controller]")]
[ApiController]
public class RouteController : ControllerBase
{
private readonly IStringLocalizer<RouteController> _localizer;

public RouteController(IStringLocalizer<RouteController> sharedLocalizer)
{
_localizer = sharedLocalizer;
}

[HttpGet]
public IActionResult Get()
{
return Ok(_localizer["The route language is {0}."].Value);
}
}
}
29 changes: 29 additions & 0 deletions src/Backend/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using System.Reflection;

namespace Backend.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly IStringLocalizer _localizer;
private readonly IStringLocalizer _localizer2;

public ValuesController(IStringLocalizerFactory factory)
{
var type = typeof(SharedResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
_localizer = factory.Create(type);
_localizer2 = factory.Create("SharedResource", assemblyName.Name);
}

[HttpGet]
public IActionResult Get()
{
return Ok(_localizer["Your application description page."]
+ " loc 2: " + _localizer2["Your application description page."]);
}
}
}
59 changes: 59 additions & 0 deletions src/Backend/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Localization.Routing;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Add location services
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");

// Configuring location options
builder.Services.Configure<RequestLocalizationOptions>(options => {
var SupportedCultures = new[] { "en-US", "es-ES", "en", "es" };
options.SetDefaultCulture(SupportedCultures[0])
.AddSupportedCultures(SupportedCultures)
.AddSupportedUICultures(SupportedCultures)
.RequestCultureProviders = new List<IRequestCultureProvider>
{
//new AcceptLanguageHeaderRequestCultureProvider(),
//new QueryStringRequestCultureProvider(),
//new CookieRequestCultureProvider(),
//new RouteDataRequestCultureProvider(),
new CustomRequestCultureProvider(async context =>
{
// Aquí debes implementar tu lógica para determinar la cultura basada en el contexto de la solicitud.
// Puedes leer cookies, cabeceras, etc. para decidir la cultura.

// En este ejemplo, se simula que se obtiene la cultura "en-US" como predeterminada.
string culture = "es-ES";

return new ProviderCultureResult(culture);
})
};
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

// Configure and enable request-based localization
app.UseRequestLocalization();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
41 changes: 41 additions & 0 deletions src/Backend/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:55318",
"sslPort": 44313
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5053",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7153;http://localhost:5053",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Loading

0 comments on commit b5b53bc

Please sign in to comment.