1

I'm using localization for my API application. The problem is, everything work well on local machine, but when I deployed the application on Azure, localization doesn't work anymore.

Here is the result when requesting on debugging (the message is translated):

{
  "status": "NotFound",
  "timestamp": "2021-05-05T03:31:10Z",
  "code": "R001",
  "message": "Customer order was not found!"
}

Here is the result when requesting on production (the message is dispalyed as key - R001, not translation value):

{
  "status": "NotFound",
  "timestamp": "2021-05-05T03:29:04Z",
  "code": "R001",
  "message": "R001"
}

I can't figure out what's wrong.

The prject hierarchy is as following: enter image description here

My CustomerOrder/Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    ...
    // Localization service
    services.AddLocalization(options => options.ResourcesPath = "Resources")
            .AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    // adding culture
    var supportedCultures = new List<CultureInfo>
    {
        new CultureInfo("fr"),
        new CultureInfo("fr-FR"),
        new CultureInfo("en"),
        new CultureInfo("en-US")
    };

    var localizationOptions = new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture("en"),
        // formatted numbers, dates, etc
        SupportedCultures = supportedCultures,
        // UI strings that we have localized
        SupportedUICultures = supportedCultures,
    };
    app.UseRequestLocalization(localizationOptions);
}

How I use localization in my controller

...
private readonly IStringLocalizer<CustomerOrdersController> _localizer;
...
_localizer["R001"]

Task on azure-pipelines

  - task: DotNetCoreCLI@2
    inputs:
      command: 'build'
      projects: '$(build.sourcesDirectory)/src/$(projectName)/$(projectName).csproj'
      arguments: '-c "$(buildConfiguration)" -f "netcoreapp3.1" -r "ubuntu.19.10-x64"'
Quyết
  • 38
  • 1
  • 7

1 Answers1

0

Finally, I manage to solve the problem myself.

It seems that the target namespace is incorrect when building the solution, so changing Custom Tool NameSpace property of the resource files (.resx) to CustomerOrder has solved the issue.

In VS 2019:

  1. Right-click on .resx file, choose Properties.
  2. Change the Custom Tool NameSpace property to the target project namespace (in my case CustomerOrder is the target project).

Or an easier way, add the below configuration to the .csproj file (the project that contains the resource files, in my case CustomerOrder.Api.csproj)

<ItemGroup>
  <EmbeddedResource Update="Resources\**">
    <CustomToolNamespace>CustomerOrder</CustomToolNamespace>
  </EmbeddedResource>
</ItemGroup>
Quyết
  • 38
  • 1
  • 7