Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.
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

Description

@dougbu

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.)

Activity

  1. ryanbrandenburg commented on Oct 25, 2016

    @ryanbrandenburg
    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.

  2. added this to the Backlog milestone on Jan 3, 2017
  3. BrainCrumbz commented on Jan 4, 2017

    @BrainCrumbz

    @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.

  4. aspnet-hello commented on Jan 2, 2018

    @aspnet-hello

    This issue was moved to dotnet/aspnetcore#2647

  5. locked and limited conversation to collaborators on Jan 2, 2018
  6. removed this from the Backlog milestone on Jan 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions