Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

Must use Type.FullName for resource files found using default IStringLocalizer<T> #296

Closed
dougbu opened this issue Oct 20, 2016 · 3 comments

Comments

@dougbu
Copy link
Contributor

dougbu commented Oct 20, 2016

Behind our default IStringLocalizer<T>, ResourceManagerStringLocalizerFactory removes a prefix matching IHostingEnvironment.ApplicationName or AssemblyName.Name (in some cases) from T's FullName before a search of the containing assembly for requested resources. This does not help users who happen to have a different root namespace from the containing assembly / application name. They must use T's entire FullName e.g.
capture

One important case occurs when Visual Studio creates a new project from a template and the user chooses a name that's invalid as a C# identifier. The "safe project name" used as the root namespace is created using a simple algorithm but will not match the application / assembly name.

While redoing the "safe project name" algorithm in our code may be an ugly hack, we could e.g. extend [ResourceLocation] to also specify the namespace prefix to remove when searching for resources in the containing assembly based on a Type.


Found while working with sample application at dougbu/Localized.1.1-preview. Screenshot above comes from VS with that solution open. (Took longer than it should for me to find the workaround.)

@ryanbrandenburg
Copy link
Contributor

In order to fix this without adding new information storage a la [ResourceLocation] we would need to apply whatever transformation is used to turn the Project name into a legal C# namespace to baseNamespace here. If we did that we would successfully trim the namespace from the front of typeInfo.FullName and have the correct resource Prefix, but I understand that there's some trepidation about putting that info in here.

@BrainCrumbz
Copy link

BrainCrumbz commented Jan 4, 2017

@dougbu we've just been bitten by this, or at least that's how it looks like.

We have ProjectName == DefaultNameSpace != ProjectFolder. When working with IStringLocalizer<> from a class (e.g. controller or other), we had to recreate the type FullName under ProjectFolder\Resources\, i.e. starting from the DefaultNameSpace. So we had a scenario like the one pictured in your screenshot.

We also decided to try a variation on it, exchanging a long filename for a deeper path, so instead of:

ProjectFolder\Resources\DefaultNameSpace.FeatureA.Whatever.Controllers.MyClass.fr.resx

we went with:

ProjectFolder\Resources\DefaultNameSpace\FeatureA\Whatever\Controllers\MyClass.fr.resx

which still works and it's easier on the eyes.

One strange (?) thing though: in the same project, resources for Razor views are localized just fine, in the way one would expect. So that we have views organized in custom locations like:

ProjectFolder\FeatureA\Whatever\MyView.cshtml

and their resources are at:

ProjectFolder\Resources\FeatureA\Whatever\MyView.fr.resx

HTH

EDIT Actually I just noticed from your screenshot that resources for _Layout view were listed as well without FullName, so this is known.

@aspnet-hello
Copy link

This issue was moved to dotnet/aspnetcore#2647

@aspnet aspnet locked and limited conversation to collaborators Jan 2, 2018
@aspnet-hello aspnet-hello removed this from the Backlog milestone Jan 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants