Friday, November 6, 2009

Variations - Taking locale from User Profile


There are various posts/ examples which explains how to set up variations in SharePoint to implement a multilingual site using variations. When variations are set up with multiple languages, users are redirected to the appropriate site with the language of your choice as defined in the browser locale. However in certain intranet scenarios, companies would like the employees to view the site in the same language irrespective of their location or the browser locale. In which case, it may be better to have the language defined in the user profile database and sourced from a central location. However the question is how we could change the default behavior of SharePoint to redirect to the correct variation based on the user profile entry. The trick is to use modify the VariationRootLanding.ascx ( http://msdn.microsoft.com/en-us/library/ms562040.aspx)

GetUserLanguages() method in VariationRootLanding.ascx retrieves the language from the brwser locale. You may replace the function with the following code to redirect to the language site mentioned in the user profile.

private string[] GetUserLanguages()

{

string[] userPofilePrefLanguages;

userPofilePrefLanguages = new string[1];

ServerContext context = ServerContext.GetContext(SPContext.Current.Site);

UserProfileManager profileManager = new UserProfileManager(context);

UserProfile user_profile = profileManager.GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

if (user_profile["UserPreferedLocale"].Value != null)

{

userPofilePrefLanguages[0] = user_profile["UserPreferedLocale"].Value.ToString();

}

if (null != userPofilePrefLanguages)

{

int qualityIndexPos = -1;

for (int i = 0; i < userPofilePrefLanguages.Length; i++)

{

qualityIndexPos = userPofilePrefLanguages[i].IndexOf(QualityValuePrefix, StringComparison.Ordinal);

if (qualityIndexPos > 0)

{

userPofilePrefLanguages[i] = userPofilePrefLanguages[i].Substring(0, qualityIndexPos);

}

}

}

return userPofilePrefLanguages;

}

0 comments: