Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please add more info about shared files #28674

Closed
Edelbitter opened this issue Mar 14, 2023 · 22 comments · Fixed by #28732
Closed

Please add more info about shared files #28674

Edelbitter opened this issue Mar 14, 2023 · 22 comments · Fixed by #28732
Labels
doc-enhancement Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

@Edelbitter
Copy link

Hi everyone,

It would be very helpful if the documentation contained explicit information how to use "SharedResources", i.e. one file (per culture) for all localized strings. It is unclear where to place the files and what to name them, in order for IStringLocalizer (etc.) to find the strings.
I could not figure it out and found my answer here
https://stackoverflow.com/questions/42647384/asp-net-core-localization-with-help-of-sharedresources
but this information should be included in the documentation.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

@dotnet-bot dotnet-bot added ⌚ Not Triaged Source - Docs.ms Docs Customer feedback via GitHub Issue labels Mar 14, 2023
@Rick-Anderson
Copy link
Contributor

@hishamco PLEASE review

@hishamco
Copy link
Member

What about this one https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization/make-content-localizable?view=aspnetcore-7.0#shared-resources

If I remembered we already have a sample for shared resources, please check the samples in the repo

1 similar comment
@hishamco

This comment was marked as outdated.

@Edelbitter
Copy link
Author

I've looked at the samples here https://github.com/aspnet/Entropy/tree/master/samples with 'localization' in the name, and could not find anything.

I saw the section about SharedResources, that's how I knew it was possible to use them. But there is no information about how to structure the folders and files and namespaces an that in services.AddLocalization, no Path should be entered.

@hishamco
Copy link
Member

The SharedResource is just a dummy class

I've looked at the samples here https://github.com/aspnet/Entropy/tree/master/samples with 'localization' in the name, and could not find anything.

Have a look to this

@Edelbitter
Copy link
Author

Now I'm completely confused why I had to remove the Path to get it to work but ok. The sample has an unused file, btw: Resources.SharedResource.fr.resx. Thanks

@hishamco
Copy link
Member

Now I'm completely confused why I had to remove the Path to get it to work but ok.

Which path do you mean?

The sample has an unused file

Don't blow my mind :)

private readonly IStringLocalizer<SharedResource> _sharedLocalizer;

As I told you it's just a marker or dummy class

@Edelbitter
Copy link
Author

I know it's a dummy class, that was the first thing I implemented.

This Path
services.AddLocalization(options => options.ResourcesPath = "Resources");

When I set it to "Resources" (which is where my .resx files actually are), it searched at
".Resources..Resources".
(That's what it said when I looked at the whole object returned by _localizer["key"])

My SharedResources class is in the Resources directory, too (but in the project namespace) but I had the same problem when I put it into the project root.

Not setting a ResourcesPath is suggested in the stackoverflow thread so I tried that and it worked. But it seems that's not the only way to make it work, as shown by the sample.

@hishamco
Copy link
Member

When you set the path as it was shown, it should look for the resources inside that folder, unless you did something wrong with the namespaces

I don't know if you are working on another project, but I'm pretty sure it might be namespace issue. Could you please share a minimal repo to reproduce the issue?

@Edelbitter
Copy link
Author

@hishamco
Copy link
Member

Which of the resources doesn't work probably? Just to avoid check everything in the project

@Edelbitter
Copy link
Author

Both. It's hardcoded to use French but the result is the same if English is used.

@hishamco
Copy link
Member

I just found two issues here:

  1. Try to avoid using special characters in the project names, so replace loca-demo with loca-demo
  2. Move the SharedResources to the root

Two of the craziest issues in localization are resource naming and namespaces :)

@Edelbitter
Copy link
Author

Interesting. The actual customer project has a hyphen in the project name, too.
But it's pretty weird how that causes problems or doesn't, depending on where the resources are.

Move just the dummy class or all of the files?

When moving just the class, I get this
image

@hishamco
Copy link
Member

Move just the dummy class or all of the files?

Just the SharedResources out of the Resources folder

@Edelbitter
Copy link
Author

This is what I did. It's not working.
image

@hishamco
Copy link
Member

Please update your repo, then give me access write to it, so I can commit directly

@hishamco
Copy link
Member

@Edelbitter check your repo now, as I told you before naming is critical in localization :)

You might check the troubleshooting section in our docs. @Rick-Anderson is there anything to add here or we can close this one

@Edelbitter
Copy link
Author

I'll remember never to use hyphens for anything! Thank you for your time :)

@hishamco
Copy link
Member

Not only hyphens, any invalid symbol :)

@akmalnodirov
Copy link

@hishamco that helped me too, I spent more than 4 hours before, your solution about moving the base file to the root folder)

@hishamco
Copy link
Member

Exactly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants