0

jQuery UI and validation are strongly dependent upon regional settings. Consider for example a date validation or a number validation, it cannot work irrespective of the current locale. For this reason, the Globalize.js library can be used with jQuery to achieve internationalization and localization.

So, the question is: considering that numbers, currencies, and dates in SharePoint are displayed according to the locale setting, which can differ from the default site language or current user display language, how can I globalize jQuery in SharePoint 2010?

What I tried so far is to leverage the ScriptManager EnableScriptLocalization property, like this:

<asp:ScriptManager id="ScriptManager" runat="server" EnablePageMethods="false" EnablePartialRendering="true" EnableScriptGlobalization="false" EnableScriptLocalization="true">
  <Scripts>
    <asp:ScriptReference Path="<%$SPUrl:~Site/SiteAssets/js/test.js%>" ResourceUICultures="it-IT"></asp:ScriptReference>
  </Scripts>
</asp:ScriptManager>

Unfortunately, what seems to influence the ScriptManager is the display language, not the locale, so this is basically useless because, once again, what really matters is the locale.

I need to somehow detect the current user's locale setting. Does anyone have any clue?

6
  • Why all the downvotes in this post? Thats not really motivating ...
    – Remko
    Commented Feb 13, 2014 at 9:25
  • @Remko THANK YOU for the support. I can imagine why the downvotes. I just wrote about it on meta (meta.sharepoint.stackexchange.com/a/1047/8642), if you're willing to have a look.
    – MdMazzotti
    Commented Feb 13, 2014 at 9:34
  • +1, question seems legit to me. Commented Feb 13, 2014 at 10:10
  • @MuhammadRaja can you upvote it then please? I would appreciate it
    – MdMazzotti
    Commented Feb 13, 2014 at 10:16
  • 1
    @MdMazzotti +1 means upvote.. and I can't upvote a question twice :) Commented Feb 13, 2014 at 10:50

3 Answers 3

2

An interesting question! I assume you want to load the locale in javascript (in order to use it with Globalize.js). Regardless the solution I see some problems:

You have to choose the best for your situation. If I had this issue, I'd inject the language that user has chosen as their display language:

System.Globalization.CultureInfo.CurrentUICulture as described at The SI SharePoint Blog

enter image description here

This solution will show your globalized strings in the language that a user has chosen and this will be the same locale as in the rest of the SharePoint site.

2
  • I'm definitely going to trust the SPRegionalSettings. I think it's reasonable, because users have the option to change their preferred locale, overriding the default one set for the site. This guarantee that wherever a user is and from whatever browser, her experience is consistent, irrespective of the browser settings. Your answer is the best so far, but I would have hoped for better alternatives. I think I've come up with two nice workarounds, that I'm going to show by answering my own question, if I don't get more answers from the community
    – MdMazzotti
    Commented Feb 12, 2014 at 21:25
  • Good. I am glad you've found some workarounds and that you are willing to share it. Commented Feb 12, 2014 at 22:26
0

I believe the DOM element _SPPageContextInfo.currentLanguage will give you the current users locale.

Edit: Looks like the only way would be to create a user control that injects SPContext.Current.Web.CurrentUser.RegionalSettings.LocaleID into the DOM.

3
  • Unfortunately, no, it doesn't. currentLanguage is the user display language, not the locale.
    – MdMazzotti
    Commented Feb 12, 2014 at 19:33
  • Yep, seems to be the case, I've tried toggling my regional settings and am not seeing any of the locale ids change in the DOM. Commented Feb 12, 2014 at 19:44
  • edited with another option Commented Feb 12, 2014 at 19:45
0

How to localize JavaScript files in SharePoint

ScriptLink comes to the rescue! ScriptLink that is used to for registering resources, such as scripts contains Localizable property:

Provides properties and methods for registering resources, such as scripts, on a page so that they can be requested when the page is rendered.

For example, the declaration:

 <SharePoint:ScriptLink language="javascript" name="strings.js" runat="server" localizable="true" />

Note: localizable attribute is set to True

is intended for loading the localized JavaScript file:

/_layouts/15/1033/strings.js

Note: localized JavaScript file is loaded from folder 1033

The same SharePoint built-in capabilities for resource localization, could be applied for custom JavaScript files.


How to get Locale on the Server side

var lcid = System.Threading.Thread.CurrentThread.CurrentUICulture.LCID;

How to get Locale on the client side

a)Get the current locale being used by the user (PageContextInfo structure):

 var lcid = _spPageContextInfo.currentLanguage;

b)Gets the locale identifier (LCID) for the default language of the web site (PageContextInfo structure):

var id =_spPageContextInfo.webLanguage;

c)Gets the locale identifier (LCID) for the default language of the web site via CSOM:

function getWebLocale(OnSuccess,OnError) {
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    context.load(web);
    context.executeQueryAsync(
        function() {
           OnSuccess(web.get_language());
        },
        OnError
       );   
 }

2
  • Sorry but once again, this is not correct. _spPageContextInfo.currentLanguage does not contain the user locale, it contains the user display language. It's a totally different thing. Also your other approach of getting the web.get_language() is wrong, as this returns the site default display language, not the default locale.
    – MdMazzotti
    Commented Feb 12, 2014 at 21:16
  • In fact, web.get_language() returns locale identifier (LCID) for the default language of the web. Anyway, the answer has been updated Commented Feb 12, 2014 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.