Question

For a project I'm developing a multi-lingual site. The site contains an App part that should display localized content, depending on which variation label site the user is browsing the site. Consider I have the following two sites:

- http://example.com/en
- http://example.com/nl

Both contains the same app. The app's querystring receives the {StandardTokens} (specified in the AppManifest.xml). When the app is loaded inside SharePoint, the following iframe href is always generated:

https://example.com/nl/_layouts/15/appredirect.aspx?redirect_uri=http%3A%2F%2Fapp.example.com%2F%2FPart%2FIndex%2FRRI-701000%3FSPHostUrl%3Dhttps%253A%252F%252Fexample%252Ecom%252Fnl%26SPHostTitle%3DNederlands%26SPAppWebUrl%3D%22%22%26SPLanguage%3Den%252DUS%26SPClientTag%3D1%26SPProductNumber%3D15%252E0%252E4667%252E1000%26SenderId%3D8AC9AB2C0&client_id=i%3A0i%2Et%7Cms%2Esp%2Eext%7Ccea0598e%2D5ba0%2D4822%2Dbf40%2Dc81547563c23%408d20b762%2D22a6%2D440d%2D976b%2D3982cf425005

The URL decoded redirect_uri querystring parameter is:

http://app.example.com?SPLanguage=en%2DUS&SPHostUrl=https%3A%2F%2Frami%2Dyokota%2Ecom%2Fnl&SPHostTitle=Nederlands&SPAppWebUrl=""&SPClientTag=1&SPProductNumber=15%2E0%2E4667%2E1000&SenderId=8AC9AB2C0

As you can see above the standardly generated SPLanguage querystring parameter is always "en_US", regardless of the language the user has chosen on the site. MSDN however indicates that SPLanguage should pass "the current language/culture of the host web of an app for SharePoint."

Am I doing something wrong or isn't the SPLanguage querystring parameter meant to pass the current selected language to the app?

Was it helpful?

Solution

After spending hours to figure out why the SPLanguage didn't change, writing down my problem seemed to do the trick. The simple answer is that the SPLanguage querystring parameter displays the "Language" attribute of the selected variation. This "Language" column specifies the language pack that is used for the specified label. In my case though (a public facing website), I want the language pack to be the same for all the variations, so the administrators don't stumble upon a French version of SharePoint (for example) when they have to publish a French article on the site.

So in my case, the variation settings look like this:

My variations setup

This creates four sites, all with an English administration panel, but with different locales (content languages). To pass the locale I think the only option is to use javascript and change the SPLanguage=en_US based on the label name.

In case anyone runs into the same issue, this is how you could solve it:

<script type="text/javascript">
//-------------------------
// This block will make our MVC apps use the labels language.
//-------------------------
$(function() {
    //get the url from iframe on page
    var src = $("iframe[src*='appredirect']").attr("src");

    if (src != null) {
        var iframeUrl = src.substring(0, src.indexOf("?"));
        var queryString = getQueryString(src);
        var redirectUrl = queryString["redirect_uri"];
        var clientID = queryString["client_id"];

        //Modify the redirectUrl with our language.
        redirectUrl = GetModifiedRedirectUrl(redirectUrl);

        //Piece together the new Url.
        var newUrl = iframeUrl + "?redirect_uri=" + redirectUrl + "&client_id=" + clientID;

        //update the SRC in the IFRAME
        $("iframe[src*='appredirect']").attr("src", newUrl);
    }
});

//This function takes the old RedirectUrl and modifies it to our needs (updating the SPLanguage and insert the MvcUrl).
function GetModifiedRedirectUrl(originalRedirectUrl) {
    //First decode because the URL is passed encoded in a querystring.
    var decodedUrl = decodeURIComponent(originalRedirectUrl);

    //Now split the decoded redirect Url
    var urlFragments = decodedUrl.split("?");
    var baseUrl = urlFragments[0];
    var queryString = UpdateQueryStringLanguage(urlFragments[1]);

    //Piece together the new URL.
    var newUrl = baseUrl + "?" + queryString;

    return encodeURIComponent(newUrl);
}

//This function alters the SPLanguage parameter to the label the user is navigation.
//In our setup every variation has the same language pack specified in SharePoint. This results in SPLanguage will always equal "en_US".
function UpdateQueryStringLanguage(queryString) {
    //Get the label (language) the user is browsing.
    var currentLabel = window.location.href.split("/")[3];

    //Replace the generated language with our language.
    return queryString.replace("SPLanguage=en%2DUS", "SPLanguage=" + currentLabel);
}

function getQueryString(url) {
    //split up the pieces of the URL
    var vars = [], hash;

    var hashes = url.slice(url.indexOf("?") + 1).split("&");

    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split("=");
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}
</script>

For me, this is a workaround, but I would rather choose this than having our administrators need to learn to use SharePoint in different languages. If someone has a better solution, please feel free to share it!

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top