SharePoint 2013 Apps - SPLanguage URL Token is based on Locale
When developing SharePoint Apps for multilingual sites, you usually need to know what language to use on your site based on the SharePoint UI language. By default, SharePoint passing in a SPLanguage URL query string to indicate the language (i.e. en-US).
http://localhost:2782/Pages/Default.aspx?SPHostUrl=http%3A%2F%2Fdev%2Eironman%2E%2Ecom%2Fsites%2Ftest4&SPLanguage=fr%2DCA&SPClientTag=4&SPProductNumber=15%2E0%2E4481%2E1005
However, the SPLanguage value is not based on the language setting of the host web or user profile language preference (in case of MUI). After a bit of testing, it turns out that the SPLanguage is based on the locale of the host web or user profile locale settings. It is very weird ... I think. And it's not really reflecting the UI language that an user seeing on the SharePoint site.
To make it all working, it's better to write code to determine the actual language to display in your app. The logic that I use (for MUI) is
You can get host web supported language by using the code
clientContext.Load(clientContext.Web, w => w.SupportedUILanguageIds);
clientContext.ExecuteQuery();
following code help returning user profile preference language (my site only support English and French)
public static int GetMUILanguageFor(string accountName, string spHostUrl)
{
using (ClientContext clientContext = new ClientContext(spHostUrl))
{
PeopleManager peopleManager = new PeopleManager(clientContext);
var prop = peopleManager.GetUserProfilePropertyFor(accountName, "SPS-MUILanguages");
clientContext.ExecuteQuery();
if (prop.Value != null)
{
string[] languages = prop.Value.Split(',');
if (languages.Length > 0)
{
if (languages[0].ToLower().StartsWith("fr-"))
return 1036;
}
}
}
return 1033;
}
One good resource for localizing for SharePoint app is http://msdn.microsoft.com/en-us/library/office/fp179919.aspx.
*Updated: Dec 19, 2013. Apparently, the issue was fixed in one of the April (2013) update and the SPLanguage is now providing the correct value based on the language now. I'll still have to test out. Cheers.
http://localhost:2782/Pages/Default.aspx?SPHostUrl=http%3A%2F%2Fdev%2Eironman%2E%2Ecom%2Fsites%2Ftest4&SPLanguage=fr%2DCA&SPClientTag=4&SPProductNumber=15%2E0%2E4481%2E1005
However, the SPLanguage value is not based on the language setting of the host web or user profile language preference (in case of MUI). After a bit of testing, it turns out that the SPLanguage is based on the locale of the host web or user profile locale settings. It is very weird ... I think. And it's not really reflecting the UI language that an user seeing on the SharePoint site.
To make it all working, it's better to write code to determine the actual language to display in your app. The logic that I use (for MUI) is
- If host web supports the preferred language selected by user in user profile, display the preferred language
- If host web doesn't support, displays the default host web language
You can get host web supported language by using the code
clientContext.Load(clientContext.Web, w => w.SupportedUILanguageIds);
clientContext.ExecuteQuery();
following code help returning user profile preference language (my site only support English and French)
public static int GetMUILanguageFor(string accountName, string spHostUrl)
{
using (ClientContext clientContext = new ClientContext(spHostUrl))
{
PeopleManager peopleManager = new PeopleManager(clientContext);
var prop = peopleManager.GetUserProfilePropertyFor(accountName, "SPS-MUILanguages");
clientContext.ExecuteQuery();
if (prop.Value != null)
{
string[] languages = prop.Value.Split(',');
if (languages.Length > 0)
{
if (languages[0].ToLower().StartsWith("fr-"))
return 1036;
}
}
}
return 1033;
}
One good resource for localizing for SharePoint app is http://msdn.microsoft.com/en-us/library/office/fp179919.aspx.
*Updated: Dec 19, 2013. Apparently, the issue was fixed in one of the April (2013) update and the SPLanguage is now providing the correct value based on the language now. I'll still have to test out. Cheers.
Comments