3

I am developing an application in Asp.net core that uses some translations depending on the language of the user.

In my controller, I am trying to load the error message from a .resx file. I have multiple ones depending on the language:

 Translations.nl.resx
 Translations.en.resx
 Translations.fr.resx
 Translations.resx (default)

I have configured this in the Startup.cs adding the following:

services.AddLocalization();

And in the constructor of my controller i have added:

IStringLocalizer<Translations> localizer

If i build my application in the local machine in Debug or Release mode my translations work as expected. It also works if i build the application with docker-compose but in debug mode.

If i build my application with docker-compose in release mode it is not able to find the expected translation.

My Dockerfile looks like this:

 FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
 WORKDIR /app
 EXPOSE 60604
 EXPOSE 44327

 FROM microsoft/dotnet:2.1-sdk AS build
 WORKDIR /src

 COPY MemberDataApi/NuGet.linux.config .
 COPY MemberDataApi/MemberDataApi.csproj MemberDataApi/
 VOLUME /nuget/myrepo/

 COPY MemberDataApi/NugetPackage/ /nuget/myrepo/
 RUN dotnet restore MemberDataApi/MemberDataApi.csproj
 COPY . .
 WORKDIR /src/MemberDataApi
 RUN dotnet build MemberDataApi.csproj -c Release -o /app --no-restore

 FROM build AS publish
 RUN dotnet publish MemberDataApi.csproj -c Release -o /app --no-restore


 FROM base AS final
 WORKDIR /app
 COPY --from=publish /app .
 ENTRYPOINT ["dotnet", "MemberDataApi.dll"]

Can i anybody help me with this? Any idea of what is missing on the container or what is wrong

rdoniz
  • 71
  • 7

2 Answers2

0

I had the same issue and spent hours tyring to fix something that wasn't broken because everything was working perfectly fine locally and thought it was due to containerization.

Actually I had to add those lines in my Configure(IApplicationBuilder app, IWebHostEnvironment env) method in Startup.cs:

var supportedCultures = new string[] { "en-US", "fr-FR" };
app.UseRequestLocalization(options =>
    options
        .AddSupportedCultures(supportedCultures)
        .AddSupportedUICultures(supportedCultures)
        .SetDefaultCulture("fr-FR")
);

You must precise which cultures your application can handle.

You can find more here.

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
Nonox
  • 1
  • 1
0

We had the same issue. It always works on my windows machine but doesn't work on linux server after deployment. It turns out we had to include both:

.AddSupportedCultures(supportedCultures);
.AddSupportedUICultures(supportedCultures);