-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
.Net 7 Razor Class Library Localization #44655
Comments
Thanks for contacting us, @Julien-Marpault. If I'm understanding the problem correctly, this is similar to aspnet/Razor#2404. For a possible workaround, please see the suggestions in aspnet/Localization#328. Could you please try this and let us know if covers your scenario? Thanks. |
Hi @Julien-Marpault. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
I tried but it doesn't work. I made a custom implementation of IViewLocalizer with this method changing: public void Contextualize(ViewContext viewContext)
{
if (viewContext == null)
{
throw new ArgumentNullException(nameof(viewContext));
}
// Given a view path "/Views/Home/Index.cshtml" we want a baseName like "MyApplication.Views.Home.Index"
string? path = viewContext.ExecutingFilePath;
if (string.IsNullOrEmpty(path))
{
path = viewContext.View.Path;
}
Debug.Assert(!string.IsNullOrEmpty(path), "Couldn't determine a path for the view");
if (viewContext.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
{
string? currentApplicationName = new AssemblyName(controllerActionDescriptor.ControllerTypeInfo.Assembly.FullName ?? string.Empty).Name;
_applicationName = currentApplicationName ?? _hostingEnvironment.ApplicationName; ;
}
string baseName = BuildBaseName(path);
_localizer = _localizerFactory.Create(baseName, _applicationName);
} but it doesn't work. |
I moved y resources files to my main project to keep going on. LoginViewModel: public class LoginViewModel
{
[Required(ErrorMessage = "Required")]
[EmailAddress(ErrorMessage = "EmailAddress")]
public string Email { get; set; }
public string? ReturnUrl { get; set; }
} Program.cs builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services
.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization(); But... It doesn't work :) |
@Julien-Marpault It looks like the name of that .resx file doesn't match the name of the model (see the extra "s" at the end). Could you try following the naming conventions suggested here to see if it resolves your issue? |
It was a mistake when I wrote the ticket. IViewLocalizer custom implementation plus assembly decorators. |
Glad you were able to resolve the issue, @Julien-Marpault! I'm going to close this issue out, but please feel free to comment with your blog post link for others who run into this. |
I have a project with Controllers, Views etc. in a separate Razor Class Library.
In the application project I added in Program.cs
and
In my Login View I Added the ViewLocalizer
login.fr.resx file contains this:
Button_Validate Valider
Greetings Bienvenue !
Model_Email Email
Model_Email_Address E-mail invalide
Model_Email_Required E-mail requis
Title Connectez-vous ou créez un compte
But there's no way to have my string localized.
If I move the resource folder to the application project, it's working, but I want to distribute a default Localization with the Library
I read documentation an stackoverflow, but I've already done what is explained.
Thanks for your help.
The text was updated successfully, but these errors were encountered: