JavaScript Localization in SharePoint
By Anatoly Mironov
Yesterday Waldek Mastykarz published a cool post: Globalizing JavaScript in SharePoint 2013. This is a very cool technique to localize your client code in javascript and reuse your resx files in Server Side and Client Side. This is actually not new for SharePoint 2013 despite it has become more needed with the huge client focus in the new SharePoint. I have used this in SharePoint 2010 for a long time. In my blog post: ScriptResx.ashx in SharePoint I told about that technique. What I didn’t know that you can define your javascript namespace directly in the resx file. Waldek wrote in his comment that SP.Publishing.Resources.en-US.resx automatically are SP.Publishing.Resources in javascript. That was not the case for my own localization files. A simple look at SP.Publishing.Resources.en-US.resx helped: [sourcecode language=“xml”] true SP.Publishing.Resources [/sourcecode] This results in: [sourcecode language=“javascript”] _EnsureJSNamespace(‘SP.Publishing’); [/sourcecode] So what we have to do for our custom resx file is to add classFullName resheader: [sourcecode language=“xml”] Takana.Res [/sourcecode]
Comments from Wordpress.com
thomasdah - Feb 1, 2013
Here some additional info about localization with angular: http://stackoverflow.com/a/15056198/1433691
[…] JavaScript Localization in SharePoint […]
Thank you Thomas. The combination SharePoint + Angular is awesome and scriptresx localization can help localizing the new sharepoint apps.
Great post - as always :)
Tack Johan!
The problem is that i can’t use the …to reference because i’m trying to load directly from javascript thanks!
Not work! I’ve used now RegisterSod(“test.sp.publishing.resources.resx”, “/_layouts/ScriptResx.ashx?name=test.sp.publishing.resources&culture=en-US”); If paste url in Chrome i can see correctly: “_EnsureJSNamespace(‘TEST.SP.Publishing’); TEST.SP.Publishing.Resources={..” But just execute RegisterSod(…..); Try to use TEST.SP.Publishing.Resources and console return “ReferenceError: TEST is not defined” Thnks!
Allright. So there is no problem with the script reference. It is just SP.SOD handling. The easiest way is not to use SP.SOD just reference your script like When you want to take an advantage of SP.SOD you must check the documentation for SP.SOD: RegisterSod doesn’t load your script.
Hi! Great post! I’m try to use my own localization file, i register it using SP.SOD.registerSod(“test.sp.publishing.resources.resx”, “/_layouts/ScriptResx.ashx?name=test.sp.publishing.resources&culture=en”); I´m using the namespace TEST.SP.Publishing, but i can’t access it from javascript. Something wrong? Thanks!
Hi Daniel! Try to get it to work without SP.SOD first. Just paste your url in Chrome and see what you get. I think, culture has to be defined like “en-US”, not just (“en”).
Hi, Kannan. Every time you update the resource file in Visual Studio, it resets the classFullName. Unfortunately, you have to update it every time.
Hi Anatoly, I defined a resource file with scriptResx and classFullName elements but when I run the url from browser its blank. Any Ideas how to troubleshoot this…