dotnet/AspNetCore.Docs

Dummy class namespace in Blazor localization shared resource section

mobinseven opened this issue ยท 10 comments

Description

I think the dummy class namespace should be set something other than what is written in the documentation. Because the compiler detects an already existing class with the same name.

The dummy class:

namespace BlazorSample.Localization;

public class SharedResource
{
}

And the class generated from .resx:

namespace BlazorSample.Localization {
    using System;
...
    public class SharedResource {
        
        private static global::System.Resources.ResourceManager resourceMan;
...

Which throws:

Localization\SharedResource.Designer.cs(25,18,25,32): error CS0101: The namespace 'BlazorSample.Localization' already contains a definition for 'SharedResource'

If you change the namespace of the dummy class to the root namespace all will be well.

BlazorSample.zip

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-8.0#shared-resources

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/globalization-localization.md

Document ID

d6f07538-228e-9f96-680f-6c324caf11d6

Article author

@guardrex

Related Issues

๐Ÿ‚๐ŸŽ Happy Holidays! โ„๏ธโ›„

Stand-by! ... A green dinosaur ๐Ÿฆ– will be along shortly to assist.

Hello @mobinseven ... I can't reproduce that error here, and I can't open a ZIP file per our security guidelines. Can you place that app up on GitHub in a repo for me to look at?

It's unrelated to your issue, but I do see that it would be nice to state again that the _Import file should have an @using directive for the package (Microsoft.Extensions.Localization), so I'll at least get that fixed.

Can you place that app up on GitHub in a repo for me to look at?

Hello @guardrex

Here you are:

mobinseven/BlazorSample

Best regards.

I see ... so it's the SharedResource.Designer.cs file being there that's causing the exception. As far as I can tell, the "dummy class" is supposed to replace that. Let's ask if Hisham knows, as I'm not a loc guru.

@hishamco ... Here's the situation ...

We have the section on shared resources here ...

https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-8.0#shared-resources

... and we tell devs to use a "dummy" SharedResources class (SharedResources.cs) that just looks like this ...

namespace BlazorSample.Localization;

public class SharedResource
{
}

... and then they add their shared resource files (e.g., SharedResources.resx, SharedResources.fa.resx, etc.).

When @mobinseven was working on setting up shared resources in a test app, he ended up with VS adding a SharedResources.Designer.cs file in the Localization folder SxS with the SharedResource class. The added designer file broke ๐Ÿ’ฅ on a namespace collision with the "dummy" file's class. Here's the file that his VS added to his app ...

https://github.com/mobinseven/BlazorSample/blob/main/Localization/SharedResource.Designer.cs

I think we don't want that SharedResource.Designer.cs file there at all. Isn't the "dummy" class supposed to act as the SharedResource class?

If so, then I need to add a remark of some sort about NOT using designer files ... or explaining these steps in a way that will result in VS not adding the designer file.

btw ... @mobinseven ... you can delete the SharedResource.Designer.cs file. Add Loc services (builder.Services.AddLocalization();) and continue on with that section's guidance in a component ...

@inject IStringLocalizer<Localization.SharedResource> Loc

...

@Loc["Login"]

I do see tho that I need to add a remark about confirming that ...

@using Microsoft.Extensions.Localization

... is in the _Imports file of the app to light up the IStringLocalizer API in the component. That's a separate thing that I'll take care of along the way.

AFAIK this class is just a marker

@mobinseven are you using docs sample or your own sample, so we can investigate

It's his own sample. He's just following the section doing what the section is telling him to do.

What I don't understand is why VS added the designer file.

Whatever causes it, the guidance for adding the shared resource class tell devs ...

  • Create a dummy class with an arbitrary class name. In the following example:
    • The app uses the BlazorSample namespace, and localization assets use the BlazorSample.Localization namespace.
    • The dummy class is named SharedResource.
    • The class file is placed in a Localization folder at the root of the app.

... and I can add a bullet to that ...

  • Create a dummy class with an arbitrary class name. In the following example:
    • ...
    • ...
    • ...
    • Don't use an autogenerated designer file (for example, SharedResources.Designer.cs). The dummy class is meant to act as the shared resource class. The presence of a designer file results in a namespace collision.
  • Don't use an autogenerated designer file (for example, SharedResources.Designer.cs). The dummy class is meant to act as the shared resource class. The presence of a designer file results in a namespace collision.

I have found out that changing SharedResource access modifier in the Resource Explorer - out of habit, I guess - made VS to generate the SharedResources.Designer.cs. The access modifier must be kept at No code generation (the default value).

Ok ... that's cool.

I'll make these updates to the article when I get back in a few hours.

Leave this issue open, as it will close automatically when the PR merges later.

Thanks @hishamco ... bit of a false alarm ... sorry to have bothered you with this.

Thanks @hishamco ... bit of a false alarm ... sorry to have bothered you with this.

Feel free to ping me anytime when there's a localization issue :)