dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
34.81k stars 9.84k forks source link

Broken localization after "Publish" from resources from the "Shared" library in the Blazor WASM client #56560

Open thepra opened 4 days ago

thepra commented 4 days ago

Is there an existing issue for this?

Describe the bug

My current project is structured by the template: MyApp.Server(WebApi standalone server), MyApp.Shared and MyApp.Client(Blazor wasm project).

Now I've put some shared localization resources between the client and the server in the "Shared" library in such configuration: immagine (with public classes exposed)

in .NET 7 this setup did work both at development and publish(prod) times, now in .NET 8 only when developing the translations are picked up correctly and after publishing the string translations are not picked up at all, with the result that the errors and field names are not translated. (Plus the client itself has it's own different resource files)

In the client I merged the different resources into a single singleton service class, like this:

public sealed class CoalescingStringLocalizer
{
    readonly IStringLocalizer<PagesLoc> _pLocalizer;
    readonly IStringLocalizer<FieldsNameResource> _fLocalizer;
    readonly IStringLocalizer<ErrorsResource> _eLocalizer;

    public CoalescingStringLocalizer(
    IStringLocalizer<PagesLoc> pLocalizer,
    IStringLocalizer<FieldsNameResource> fLocalizer,
    IStringLocalizer<ErrorsResource> eLocalizer) =>
        (_pLocalizer, _fLocalizer, _eLocalizer) =
        (pLocalizer, fLocalizer, eLocalizer);

    internal LocalizedString this[string name]
    {
        get
        {
            if (_pLocalizer[name].ResourceNotFound)
                if (_fLocalizer[name].ResourceNotFound)
                    if (_eLocalizer[name].ResourceNotFound)
                        return new(name, name, false);
                    else
                        return _eLocalizer[name];
                else
                    return _fLocalizer[name];
            else
                return _pLocalizer[name];
        }
    }

    internal LocalizedString this[string name, params object[] arguments]
    {
        get
        {
            if (_pLocalizer[name].ResourceNotFound)
                if (_fLocalizer[name].ResourceNotFound)
                    if (_eLocalizer[name].ResourceNotFound)
                        return new(name, string.Format(name, arguments), false);
                    else
                        return _eLocalizer[name, arguments];
                else
                    return _fLocalizer[name, arguments];
            else
                return _pLocalizer[name, arguments];
        }
    }
}

It works as it should for the PagesLoc resources but the other FieldsNameResource and ErrorsResource resources are not working.

Expected Behavior

IStringLocalizer should work for resources coming from external library projects.

Steps To Reproduce

^ In the Describe the bug I put it all

Exceptions (if any)

No response

.NET Version

8.0.302

Anything else?

.NET SDK: Version: 8.0.302 Commit: ef14e02af8 Workload version: 8.0.300-manifests.5273bb1c MSBuild version: 17.10.4+10fbfbf2e Ambiente di runtime: OS Name: Windows OS Version: 10.0.19045 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.302\ Carichi di lavoro .NET installati: [wasm-tools] Origine dell'installazione: VS 17.10.35013.160 Versioni del manifesto: 8.0.5/8.0.100 Percorso del manifesto: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.current\8.0.5\WorkloadManifest.json Tipo di installazione: FileBased Host: Version: 8.0.6 Architecture: x64 Commit: 3b8b000a0e .NET SDKs installed: 8.0.302 [C:\Program Files\dotnet\sdk] .NET runtimes installed: Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation] Environment variables: Not set global.json file: Not found Learn more: https://aka.ms/dotnet/info Download .NET: https://aka.ms/dotnet/download

dotnet-policy-service[bot] commented 3 days ago

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimal repro project that illustrates the problem without unnecessary code. Please share with us in a public GitHub repo because we cannot open ZIP attachments, and don't include any confidential content.

dotnet-policy-service[bot] commented 3 days ago

Hi @thepra. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.