I try to use shared resources files in view. My Startup.cs has code:
services.AddLocalization(opts => {
opts.ResourcesPath = "Resources";
});
And i add this code to a view
@inject IHtmlLocalizer<ShareResource> SharedLocalizer
@SharedLocalizer["Log in"]
But this do not work. Localizer try find ShareResource file on this path project.Resources.Resources
If i delete opts.ResourcesPath = "Resources"; from startup, IHtmlLocalizer work correct but other (Controller) localizer do not work. Please help me. And sorry for my english.
@ryanbrandenburg @hishamco any thoughts on this?
@vov4ik08 what namespace is ShareResource in? That may make a difference here.
@dougbu For ShareResource auto generate class with namespace project.Resources
@vov4ik08 if your SharedResource places in Resources folder I'm not sure why it's auto generated?!! Anyhow do you have a repo to reproduce your issue, perhaps it's a namespace issue as @dougbu said before
Hopefully it is not the exact same issue https://github.com/aspnet/Localization/issues/268
@vov4ik08 if you haven't already please read this section of the localization docs. This kind of issue is usually caused by resx files not being named in accordance with that guidance.
If after consulting that you're still runnning into trouble, uploading a reproduction repo like @hishamco said is generally the fastest way for us to help you since it can be difficult to accuratly convey the structure of your program.
@hishamco @ryanbrandenburg
I create file ShareResource.resx in folder Resources, then run generator code and create file ShareResource.Designer.cs with name space project.Resources. After than i add to Stratup.cs this:
services.AddLocalization(opts => {
opts.ResourcesPath = "Resources";
});
and add to view inject
@inject IHtmlLocalizer
@SharedLocalizer["Log in"]
Translation do not work. If i debug SharedLocalizer i see it tries to use the file project.Resources.Resources.ShareResource and if remove from Stratup.cs this line opts.ResourcesPath = "Resources"; it tries to use the file project.Resources.ShareResource.
I will try to reproduce your issue, but it would be nice if you create a repo for the exact sample, because may be there's some difference that let it work with me
@hishamco https://github.com/vov4ik08/TestLocalizedProject . Use Index.cshtml Thank you.
If i delete opts.ResourcesPath = "Resources"; from startup, IHtmlLocalizer work correct but other (Controller) localizer do not work. Please help me. And sorry for my english.
@vov4ik08 I tried to remove the ResourcePath but nothing changed?!! anyhow to resolve your issue just rid off auto generated class and replace it with
namespace Project
{
public class ShareResource
{
public string TEST { get; set; }
}
}
and it should work properly
@hishamco Thank you for answer. I thought that it is possible and in another way. Thanks for the help.
I found another solution. I have gbolal shared resources file - ShareResource.resx and shared resources for fr ShareResource.ru.resx, in code i use translate as @ShareResource.ResourceManager.GetString("Log in")
ShareResource.resx and shared resources for fr ShareResource.ru.resx, in code i use translate as @ShareResource.ResourceManager.GetString("Log in")
You mean Russian (Not Fr) culture.
Using ShareResource.ResourceManager.GetString unnecessary while the built-in abstraction call it in your behalf, have a look to this https://github.com/aspnet/Localization/blob/f650de8cdf5214bcbb2e88210baac0f99ce3d053/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs#L192
Yes, but if do not off auto generated class, i have ShareResource.resx and ShareResource.ru.resx ShareResource.fr.resx. ShareResource.resx - default. In view file i use @ShareResource.ResourceManager.GetString("Log in") , because i can not use IHtmlLocalization
Still wonder why you insist to use the ResourceManager in the auto generated class?!! As I said before IStringLocalizer, IHtmlLocalizer and IViewLocalizer did the same job in your behalf. Just inject them and use them wherever you want
For example:
I have two views:
users/Login.cshtml and account/Singup.chhtml
And i can translate Title for button "Submit"
If use IViewLocalizer , i can create two resource file users/Login.resx and account/Singup.resx
and duplicate string "Submit" to two files
What if you create a shared resource like what you did before?
this @inject IHtmlLocalizer
@inject IStringLocalizer<SharedResource> Localizer
You can use the IHtmlLocalizer in case you have an HTML content
Yes, but if i write this
@inject IHtmlLocalizer
project.Resources.Resources.SharedResource, not project.Resources.SharedResource
Why?!! I already get the correct resource value, after modifying your code previously
Please commit your changes to TestLocalizedProject
You can create your a shared resource like the following
namespace Project
{
public class SharedResource
{
public string Submit { get; set; }
}
}
Yes, you are right. I commit changes to test project. Thank you so much.
All the best
why do you need to create the class"sharedResource"?
Most helpful comment
why do you need to create the class"sharedResource"?