public class GlobalizationHelper
{
/// <summary>
/// Selected Language Chane in All site Collection
/// </summary>
/// <param name="language"></param>
public static void SelectedLangugae(string language)
{
HttpContext.Current.Session.Add("Lang", language);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
site.AllowUnsafeUpdates = true;
foreach (SPWeb web in site.AllWebs)
{
web.AllowUnsafeUpdates = true;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo((language));
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo((language));
// Add support for any installed language currently not supported.
SPLanguageCollection installed = SPRegionalSettings.GlobalInstalledLanguages;
System.Collections.Generic.IEnumerable<System.Globalization.CultureInfo> supported = web.SupportedUICultures;
foreach (SPLanguage lang in installed)
{
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(lang.LCID);
web.AddSupportedUICulture(culture);
}
web.Update();
web.AllowUnsafeUpdates = false;
}
site.AllowUnsafeUpdates = false;
}
});
}
/// <summary>
/// Change Culture with Language based
/// </summary>
/// <param name="culture"></param>
public static void ChangeCulture(string culture)
{
switch (culture)
{
case "en-US": SelectedLangugae(culture);
break;
case "en-GB": SelectedLangugae(culture);
break;
case "zh-CN": SelectedLangugae(culture);
break;
case "nl-NL": SelectedLangugae(culture);
break;
case "fr-FR": SelectedLangugae(culture);
break;
case "de-DE": SelectedLangugae(culture);
break;
case "it-IT": SelectedLangugae(culture);
break;
case "ja-JP": SelectedLangugae(culture);
break;
case "ko-KR": SelectedLangugae(culture);
break;
case "es-MX": SelectedLangugae(culture);
break;
case "pt-PT": SelectedLangugae(culture);
break;
case "ru-RU": SelectedLangugae(culture);
break;
default: SelectedLangugae("en-US");
break;
}
}
public static string GetUserPreferredLanguage()
{
string Language = "en-US";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spsite = new SPSite(SPContext.Current.Web.Url))
{
SPUser oUser = SPContext.Current.Web.CurrentUser;
SPServiceContext spContext = SPServiceContext.Current;
UserProfileManager profileManager = new UserProfileManager(spContext);
string username = CPUtility.GetUserProfileAccountName(oUser.LoginName);
if (profileManager.UserExists(username))
{
UserProfile profile = profileManager.GetUserProfile(username);
//Custom Fields
Language = Convert.ToString(profile["Language"].Value);
}
}
});
return Language;
}
}
---------------------------------------------------------------------------------------------
public class BasePage : LayoutsPageBase
{
protected override void InitializeCulture()
{
string currentCulture = "en-US";
if (HttpContext.Current.Session["lang"] != null)
{
currentCulture= Convert.ToString(Session["lang"]);
}
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(
currentCulture
);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(currentCulture);
base.InitializeCulture();
}
}
public class BasePageUC : UserControl
{
protected override void FrameworkInitialize()
{
string currentCulture = "en-US";
if (HttpContext.Current.Session["lang"] != null)
{
currentCulture = Convert.ToString(Session["lang"]);
}
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(
currentCulture
);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(currentCulture);
base.FrameworkInitialize();
}
}
------------------------------------------------------------------------------------------------
private CultureInfo cultureInfo;
public string GetCultureDateFormat()
{
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return cultureInfo.DateTimeFormat.ShortDatePattern;
}
public string GetCultureDateTimeFormat()
{
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return cultureInfo.DateTimeFormat.ShortDatePattern + " " + cultureInfo.DateTimeFormat.LongTimePattern;
}
public string ConvertCultureDateFormat(string dateTime)
{
DateTime dt = Convert.ToDateTime(dateTime, DateTimeFormatInfo.InvariantInfo);
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return dt.ToString(cultureInfo.DateTimeFormat.ShortDatePattern, cultureInfo);
}
public string RevertCultureDateFormat(string dateTime)
{
DateTime dt = Convert.ToDateTime(dateTime, DateTimeFormatInfo.InvariantInfo);
cultureInfo = new CultureInfo("en-US");
return dt.ToString(cultureInfo.DateTimeFormat.ShortDatePattern, cultureInfo);
}
public string ConvertCultureDateTimeFormat(string dateTime)
{
DateTime dt = Convert.ToDateTime(dateTime, DateTimeFormatInfo.InvariantInfo);
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return dt.ToString(cultureInfo.DateTimeFormat.ShortDatePattern + " " + cultureInfo.DateTimeFormat.LongTimePattern, cultureInfo);
}
public string RevertCultureDateTimeFormat(string dateTime)
{
DateTime dt = Convert.ToDateTime(dateTime, DateTimeFormatInfo.InvariantInfo);
cultureInfo = new CultureInfo("en-US");
return dt.ToString(cultureInfo.DateTimeFormat.ShortDatePattern + " " + cultureInfo.DateTimeFormat.LongTimePattern, cultureInfo);
}
public string ConvertCultureNumericFormat(string strNumber)
{
double decValue = Double.Parse(strNumber, CultureInfo.InvariantCulture);
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return decValue.ToString(cultureInfo);
}
public string RevertCultureNumericFormat(string strNumber)
{
double decValue = Double.Parse(strNumber, CultureInfo.InvariantCulture);
cultureInfo = new CultureInfo("en-US");
return decValue.ToString(cultureInfo);
}
{
/// <summary>
/// Selected Language Chane in All site Collection
/// </summary>
/// <param name="language"></param>
public static void SelectedLangugae(string language)
{
HttpContext.Current.Session.Add("Lang", language);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
site.AllowUnsafeUpdates = true;
foreach (SPWeb web in site.AllWebs)
{
web.AllowUnsafeUpdates = true;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo((language));
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo((language));
// Add support for any installed language currently not supported.
SPLanguageCollection installed = SPRegionalSettings.GlobalInstalledLanguages;
System.Collections.Generic.IEnumerable<System.Globalization.CultureInfo> supported = web.SupportedUICultures;
foreach (SPLanguage lang in installed)
{
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(lang.LCID);
web.AddSupportedUICulture(culture);
}
web.Update();
web.AllowUnsafeUpdates = false;
}
site.AllowUnsafeUpdates = false;
}
});
}
/// <summary>
/// Change Culture with Language based
/// </summary>
/// <param name="culture"></param>
public static void ChangeCulture(string culture)
{
switch (culture)
{
case "en-US": SelectedLangugae(culture);
break;
case "en-GB": SelectedLangugae(culture);
break;
case "zh-CN": SelectedLangugae(culture);
break;
case "nl-NL": SelectedLangugae(culture);
break;
case "fr-FR": SelectedLangugae(culture);
break;
case "de-DE": SelectedLangugae(culture);
break;
case "it-IT": SelectedLangugae(culture);
break;
case "ja-JP": SelectedLangugae(culture);
break;
case "ko-KR": SelectedLangugae(culture);
break;
case "es-MX": SelectedLangugae(culture);
break;
case "pt-PT": SelectedLangugae(culture);
break;
case "ru-RU": SelectedLangugae(culture);
break;
default: SelectedLangugae("en-US");
break;
}
}
public static string GetUserPreferredLanguage()
{
string Language = "en-US";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spsite = new SPSite(SPContext.Current.Web.Url))
{
SPUser oUser = SPContext.Current.Web.CurrentUser;
SPServiceContext spContext = SPServiceContext.Current;
UserProfileManager profileManager = new UserProfileManager(spContext);
string username = CPUtility.GetUserProfileAccountName(oUser.LoginName);
if (profileManager.UserExists(username))
{
UserProfile profile = profileManager.GetUserProfile(username);
//Custom Fields
Language = Convert.ToString(profile["Language"].Value);
}
}
});
return Language;
}
}
---------------------------------------------------------------------------------------------
public class BasePage : LayoutsPageBase
{
protected override void InitializeCulture()
{
string currentCulture = "en-US";
if (HttpContext.Current.Session["lang"] != null)
{
currentCulture= Convert.ToString(Session["lang"]);
}
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(
currentCulture
);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(currentCulture);
base.InitializeCulture();
}
}
public class BasePageUC : UserControl
{
protected override void FrameworkInitialize()
{
string currentCulture = "en-US";
if (HttpContext.Current.Session["lang"] != null)
{
currentCulture = Convert.ToString(Session["lang"]);
}
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(
currentCulture
);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(currentCulture);
base.FrameworkInitialize();
}
}
------------------------------------------------------------------------------------------------
private CultureInfo cultureInfo;
public string GetCultureDateFormat()
{
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return cultureInfo.DateTimeFormat.ShortDatePattern;
}
public string GetCultureDateTimeFormat()
{
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return cultureInfo.DateTimeFormat.ShortDatePattern + " " + cultureInfo.DateTimeFormat.LongTimePattern;
}
public string ConvertCultureDateFormat(string dateTime)
{
DateTime dt = Convert.ToDateTime(dateTime, DateTimeFormatInfo.InvariantInfo);
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return dt.ToString(cultureInfo.DateTimeFormat.ShortDatePattern, cultureInfo);
}
public string RevertCultureDateFormat(string dateTime)
{
DateTime dt = Convert.ToDateTime(dateTime, DateTimeFormatInfo.InvariantInfo);
cultureInfo = new CultureInfo("en-US");
return dt.ToString(cultureInfo.DateTimeFormat.ShortDatePattern, cultureInfo);
}
public string ConvertCultureDateTimeFormat(string dateTime)
{
DateTime dt = Convert.ToDateTime(dateTime, DateTimeFormatInfo.InvariantInfo);
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return dt.ToString(cultureInfo.DateTimeFormat.ShortDatePattern + " " + cultureInfo.DateTimeFormat.LongTimePattern, cultureInfo);
}
public string RevertCultureDateTimeFormat(string dateTime)
{
DateTime dt = Convert.ToDateTime(dateTime, DateTimeFormatInfo.InvariantInfo);
cultureInfo = new CultureInfo("en-US");
return dt.ToString(cultureInfo.DateTimeFormat.ShortDatePattern + " " + cultureInfo.DateTimeFormat.LongTimePattern, cultureInfo);
}
public string ConvertCultureNumericFormat(string strNumber)
{
double decValue = Double.Parse(strNumber, CultureInfo.InvariantCulture);
cultureInfo = new CultureInfo(HttpContext.Current.Session["userCulture"].ToString());
return decValue.ToString(cultureInfo);
}
public string RevertCultureNumericFormat(string strNumber)
{
double decValue = Double.Parse(strNumber, CultureInfo.InvariantCulture);
cultureInfo = new CultureInfo("en-US");
return decValue.ToString(cultureInfo);
}