CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

JavaScript Localization in SharePoint

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:

scriptresx

  <!-- 
    Whether this .resx could be read by scriptResx.ashx handler. Only a file
    marked with scriptResx:true could be returned to client.
  -->
  <resheader name="scriptResx">
    <value>true</value>
  </resheader>
  <!-- the full name of the JavaScript class.  -->
  <resheader name="classFullName">
    <value>SP.Publishing.Resources</value>
  </resheader>

This results in:

_EnsureJSNamespace('SP.Publishing');

So what we have to do for our custom resx file is to add classFullName resheader:

  <resheader name="classFullName">
    <value>Takana.Res</value>
  </resheader>
Advertisements

12 responses to “JavaScript Localization in SharePoint

  1. Pingback: ScriptResx.ashx in SharePoint « Share… What?

  2. Johan Frisell 2013-02-07 at 11:43

    Great post – as always 🙂

  3. Daniel 2013-02-18 at 13:48

    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!

    • Anatoly Mironov 2013-02-18 at 13:52

      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”).

  4. Daniel 2013-02-18 at 14:05

    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!

    • Anatoly Mironov 2013-02-18 at 14:10

      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 <script src=”/_layouts/ScriptResx.ashx?name=test.sp.publishing.resources&culture=en-US”></script>

      When you want to take an advantage of SP.SOD you must check the documentation for SP.SOD: RegisterSod doesn’t load your script.

      • Daniel 2013-02-18 at 15:54

        The problem is that i can’t use the …to reference because i’m trying to load directly from javascript

        thanks!

  5. thomasdah 2013-02-25 at 14:54

    Here some additional info about localization with angular:
    http://stackoverflow.com/a/15056198/1433691

    • Anatoly Mironov 2013-03-19 at 10:21

      Thank you Thomas. The combination SharePoint + Angular is awesome and scriptresx localization can help localizing the new sharepoint apps.

  6. Kannan 2015-02-02 at 04:52

    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…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager talking Office 365, SharePoint and Azure

My view and thoughts on Productivity and more.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

RealActivity - Real-time and trustworthy

Blog site of founder, RealActivty - Paul J. Swider

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points!!

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode, ya mama

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

%d bloggers like this: