0
votes

I am trying to inject: @inject IStringLocalizer<IndexModel> localizer in a partial view of this page (Index.cshtml.cs) but it doesn't work. The service works in Index.cshtml Main page but when I load the partial using:

$('#formID').load('index?handler=form&[email protected]&[email protected]')

and through an action method that I defined in the Index.cshtml.cs:

 public PartialViewResult OnGetForm(string first, string last)
    {
        //ViewData["Form"] = localizer["Form"];           
        //Input.Localizer = localizer;
        Input.FirstName = first;
        Input.LastName = last;
        return Partial("IndexPartials/Form", Input);            
    }

It only shows the default language, I have a "select element" in the layout that changes between three languages, And while the translation is changing in the main page the ajax loaded partials are showing the default language.

I tried different things: 1- Injecting IStringLocalizer service to Index.cshtml.cs file and using it through viewbag or directly through a model property. 2- Injecting this service directly in the action method and send it as above to partial. 3- injecting this service directly to the partial.

None of them works, all showing the default language (or maybe the key since key and default translation are the same), I fear that the other services that I will create won't work either in ajax partials. How to use localization and services in Ajax loaded partials (that are loaded through action methods)?

1

1 Answers

0
votes

I sent culture value to ajax action method:

 $('#formID').load('index?handler=form&[email protected]&[email protected]&[email protected]')

and added a culture parameter to Ajax action method:

public PartialViewResult OnGetForm(string culture, string first, string last)
    {
        //ViewData["Form"] = localizer["Form"];           
        //Input.Localizer = localizer;
        Input.FirstName = first;
        Input.LastName = last;
        return Partial("IndexPartials/Form", Input);            
    }

And now it is working.