cloudscribe/cloudscribe.Web.Localization

Use lib for data annotations on models in separate project

Opened this issue · 4 comments

I'm trying to use your lib to localize data annotations on models which exists in a separate project. Does it work? I've tryed, so far only views/ctrls which are in the same project get translated but not the data annotations in a separate project.

the sample app doesn't have any data annotations examples, but it does work, I'm using it in my cloudscribe project, you can see a viewmodel in a class library here and it gets localized from the web app global resources folder , it works as describe here https://docs.asp.net/en/latest/fundamentals/localization.html#dataannotations-localization, even though the viewmodel is in a separate project, and I have verified that if I set my browser language to french it shows me the localized validation error messages. you can see my startup code in that project here

doesn't work for non validation attributes like DisplayName so I don't use those, I just put labels in the views and the views can be localized as needed either by injecting IStringLocalizer in the view or by using a view per language

@joeaudette I found the error which caused it to not translate. Its a edge case where my projects in the solution are called Core, Core.Data, Core.Service etc.

You've implemented this line of code which makes my case fail. It will always go look in Core project. if(assembly.FullName.StartsWith(_hostingEnvironment.ApplicationName))

Implemented my own localizers with help of yours and changed to TrimOnFirstComma(assembly.FullName) == _hostingEnvironment.ApplicationName. Works like a charm!

Thanks for the lib btw, would take a bit longer to figure out without your lib.

glad that helped, I guess what I was trying to do was keep the default behavior for the main web app, are you saying there is a simple fix for your edge case that won't break anything? I'd welcome a pull request if so.
I know they have been doing some work recently to fix issues with localization and class libraries, so I figure after they ship 1.0.1 I'll need to re-evaluate my code and the need for it against their latest code.

@devharis another solution for you would been that you renamed Core -> Core.WebApp and after that you rename the startup and program namespace from Core to Core.WebApp and everything works as intended.

At least this fixed it for me in similar situation.

Also it would be nice if you shared part of your solution.