turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 156 forks source link

PartialView rendered by Ajax #265

Closed dbvcode closed 8 years ago

dbvcode commented 8 years ago

Hello guys,

First of all I don't know if I should be posting here. Please advise otherwise me if I shouldn't. Second I am a newbie in programming, specially in MVC. Third, here it goes: I am loading a partial view from the _Layout.cshtml like this:

$("#boardContent").load('@(Url.Action("DASHBOARD","Account"))');

The problem that I have is every other content in the page gets translated when I switch language, but this Partial View does not.

I assume I am missing something. Could anyone help me? And on that note, is there a documentation for the project? :D

Thanks, Rares

turquoiseowl commented 8 years ago

In general, when building web pages using AJAX it might be best to look at each request/response in the Network tab of you web browser debugger. Then if you have one request for a resource that IS being translated okay, and one for another that isn't, to look for obvious differences between them, especially in the URLs, request headers and response headers.

Feel free to come back with details of such requests along with their URLs and headers.

The only documentation I know of is the readme.md file. Thanks.

dbvcode commented 8 years ago

Thanks for your quick response and the debugging tip. :)

The text in the _Layout.cshtml gets translated after language change and page refresh. But the page loaded by AJAX comes up without the [[[ ]]] brackets.

Is it safe to assume that the page is not being translated?

Thanks in advance

turquoiseowl commented 8 years ago

If the nugget markup (e.g. [[[ and ]]]) is being removed then it IS being translated.

dbvcode commented 8 years ago

Why is it not translated to the selected language? How can I know? I assume it translates it to default(english in my case).

turquoiseowl commented 8 years ago

Please refer to the suggestion in my last message but one. Thanks.

dbvcode commented 8 years ago

Hi again, this is my request:

Request URL:http://localhost:50498/Account/DASHBOARD Request Method:POST Status Code:200 OK Remote Address:[::1]:50498 Response Headers view source Cache-Control:private, s-maxage=0 Content-Encoding:gzip Content-Length:2036 Content-Type:text/html; charset=utf-8 Date:Thu, 05 May 2016 05:33:49 GMT Server:Microsoft-IIS/10.0 Vary:Accept-Encoding X-AspNet-Version:4.0.30319 X-AspNetMvc-Version:5.0 X-Powered-By:ASP.NET X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcRGFkaVxEb2N1bWVudHNcU3luY2Z1c2lvbiBNVkNcUHJvamVjdHNcdGVzdFxBY2NvdW50XERBU0hCT0FSRA==?= Request Headers view source Accept:/ Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Content-Length:0 Cookie:__RequestVerificationToken=OGwPqP7U7uTJsyqbvW_sFPrYTCRlgrcovWQRRAhnpU7r2CIIjdp7bhCTDFCNUn3QukwfeQ7Eol41NTe7cLEH2R9eyhQXF8G3T9-ZKoiD2lc1; ASP.NET_SessionId=zkkbwzzzm4sknfyqwlpwzxw4; .ASPXAUTH=37DA4D90C01C0726B6109B3D7C7ABBBFF1031474936AB3E55B8715893F78F2892448F2E43F3CB26BC4680BC046A2A425A5F6F567EF4F06EF6422E8C09C86FEBA12DD73AD7FD77731FD243B69329B42C15784F01CBEF74B2532C99100A1077656; i18n.langtag=ro Host:localhost:50498 Origin:http://localhost:50498 Referer:http://localhost:50498/ro/Account/Index User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36 X-Requested-With:XMLHttpRequest

And this is the respone:

...

...

...

All the above text was padded like this: [[[ ]]] If I request http://localhost:50498/ro/Account/DASHBOARD it gets me the translated version. But as it seems it does not translate based on the cookie but on the url path. Any advice on how I can tackle this?

Thanks

turquoiseowl commented 8 years ago

Ah, yes, you will need to patch-in the Principal Application Language into the URL which you are generating on the fly.

There is a helper for doing that called SetLangTagInUrlPath. An example of its use is in the README, also https://github.com/turquoiseowl/i18n/issues/218.

Note that normally you don't need to worry about this because i18n is able to auto-detect and patch URLs that are in expected places like HREF attributes etc.. This is in keeping with the core concept of i18n which is that the webapp itself can be almost entirely unaware of language issues. However, this is one of the rare cases when your code needs to interact with it.

turquoiseowl commented 8 years ago

See also https://github.com/turquoiseowl/i18n/issues/251

dbvcode commented 8 years ago

Hi.

In the end I am using this workaround that does it's job well: $("#boardContent").load('/'+'@Context.GetPrincipalAppLanguageForRequest().ToString()'+'/Account/DASHBOARD');

Thanks