locked
Using language Resource files in SharePoint-hosted app RRS feed

  • Question

  • What is the best practice to build a multilinqual SharePoint-hosted app?

    I discoverd there is a possibility to add Resource files under Supported locales in the appManifest.xml, but how can we use this resources in our app page, since we can only use client-side javascript code?

    Another way to do it, and I got this working, is by using javascript files. You first have a Resource.js that gets the current culture, en than uses the corresponding language file, like Resource.en-US.js for United States (English).

    Resource.js:

    // Init vars
    var defaultLanguage = 'en-us';
    var languagesArray = ['en-us', 'fr-fr'];
    
    // Read a page's GET URL variables and return them as an associative array.
    function getUrlVars() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = decodeURIComponent(hash[1]);
        }
        return vars;
    }
    
    var queryString = getUrlVars();
    var language = queryString["SPLanguage"];
    if (language == null) {
        language = defaultLanguage;
    }
    else {
        language = language.toLowerCase();
        var langExists = false;
    
        //Checks if this language is available
        for (var i = 0; i < languagesArray.length; i++) {
            var testedLang = languagesArray[i];
            if (testedLang == language) {
                langExists = true;
                break;
            }
        }
    
        //Try to find the regional language
        if (langExists == false) {
            var regionalLng = language.substring(0, 2);
            for (var i = 0; i < languagesArray.length; i++) {
                var testedLang = languagesArray[i].substring(0, 2);
                if (testedLang == regionalLng) {
                    langExists = true;
                    language = languagesArray[i];
                    break;
                }
            }
        }
    
        //Finally use the default language
        if (langExists == false) {
            language = defaultLanguage;
        }
    }
    
    var resourcesFile = '../Scripts/Resources.' + language + '.js';
    document.writeln("<script src=\"" + resourcesFile + "\" type=\"text\/javascript\" language=\"javascript\"><\/script>");

    Resource.en-US.js:

    var Resource_Title = "Title";
    var Resource_Description = "Description";

    Or is there a better way to do this?

    Thanks in advance!

    Wednesday, January 9, 2013 1:55 PM

Answers

All replies

  • That's a pretty interesting solution you came up with.  That certainly works.  I've wondered if there is a way to make reading from the resources xml file in the project but I haven't tried exploring that route yet.

    Corey Roth - SharePoint Server MVP blog: www.dotnetmafia.com twitter: @coreyroth | SP2 Apps

    Wednesday, January 9, 2013 3:13 PM
    Answerer
  • Regarding your question about the supported locales option in the app manifest, then Ricky Kirkham from the SharePoint 2013 documentation team sheds some light on that feature here:

    http://blogs.msdn.com/b/officeapps/archive/2012/10/12/locale-support-information-is-required-for-all-apps-in-the-sharepoint-store.aspx

    Seems that it will only be implemented in the final version of SharePoint 2013 and is apparently used for informing the App Store about your app's supported languages, so it's not directly used to localize your app, as far as I understand it.

    Also although the feature is implemented in my Visual Studio 2012 and SP 2013 SDK, the <SupportedLocales> tags are not accepted yet. <SupportedLanguages> are though.

    Monday, February 11, 2013 3:23 PM
  • Hi all,

    This article describes How to: Localize apps for SharePoint and how to use the resource files.

    Best

    Yina

    Friday, March 1, 2013 1:02 AM