1、前言
在SharePoint 2010版本,在首頁面直接"選擇顯示語言"的菜單(如下圖所示),如下圖 :
在sharepoint2013和sharepoint2016並非如此。
-
這里首先需要安裝語言包,SharePoint2016語言包下載地址:
https://www.microsoft.com/en-us/download/details.aspx?id=51492
SharePoint2013語言包下載地址:
https://www.microsoft.com/zh-cn/download/details.aspx?id=37140
注意:下載安裝完畢后都要進行重新配置
-
在【網站設置】-【語言設置】設置備用語言,這里為英語,如下圖
-
進行【關於我】進行【語言區域設置】如下圖:
2、原理
上面說到SharePoint2010的語言切換根據記錄cookie的方式可以快速進行切換,只要記錄cookie的值lcid值,對於SharePoint2013和SharePoint2016的語言采用此方式不行,根據IE語言版本來確定顯示哪個語言?,因此我們這里使用Fiddler工具進行http頭文件的獲取分析,主要分析:Accept-Language
說明:
Accept-Language:瀏覽器所希望的語言種類,當服務器能夠提供一種以上的語言版本時要用到;根據http頭的情況,用代碼修改Accept-Language的設置來模擬設置語言版本的切換
-
我們打開英文站點:http://sp2016 用Fiddler抓取分析,如下圖:
分析結果:
-
我們打開中文站點:http://sp2016(切換后)的Fiddler抓取圖,如下圖:
既然知道這個原理,切換代碼把每次要切換的語言版本保存到cookie值里,那么我們第一次系統用代碼切換前cookie值為空設置一個默認為中文,使用httpModule方式來分析http頭的Accept-Language的值,如果包含en-US就設置為英文版本,如果包含zh-CN就設置為中文。明白原理寫代碼就方便多了。
3、代碼
-
HttpModule.cs源碼如下:
using System; using System.Web; using System.Web.UI; using Microsoft.SharePoint; using System.Threading; using Microsoft.SharePoint.Utilities; namespace CSSharePointLangSwitcher.LangSwitcherPage { public class HTTPSwitcherModule : IHttpModule { /// <summary> /// You will need to configure this module in the web.config file of your /// web and register it with IIS before being able to use it. For more information /// see the following link: http://go.microsoft.com/?linkid=8101007 /// </summary> #region IHttpModule Members public void Dispose() { //clean-up code here. } /// <summary> /// Init event /// </summary> /// <param name="context"></param> public void Init(HttpApplication context) { // Below is an example of how you can handle Request event and provide // custom logging implementation for it context.PreRequestHandlerExecute += context_PreRequestHandlerExecute; } #endregion /// <summary> /// Assuming the selected language is stored in a cookie. Firstly, get the selected /// language from cookie. Then add the selected language to the request header. /// Finally, use the selected language for the current culture. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void context_PreRequestHandlerExecute(object sender, EventArgs e) { // Get current application. HttpApplication httpApp = sender as HttpApplication; // Get all HTTP-specific information about current HTTP request. HttpContext context = httpApp.Context; // Current language. string strLanguage = string.Empty; // The key of current selected language in the cookies. string strKeyName = "LangSwitcher_Setting"; try { // Set the current language. if (httpApp.Request.Cookies[strKeyName] != null) { strLanguage = httpApp.Request.Cookies[strKeyName].Value; } else { strLanguage = "zh-cn"; } var lang = context.Request.Headers["Accept-Language"]; if (lang != null) { if (!lang.Contains(strLanguage)) context.Request.Headers["Accept-Language"] = strLanguage + "," + context.Request.Headers["Accept-Language"]; var culture = new System.Globalization.CultureInfo(strLanguage); // Apply the culture. SPUtility.SetThreadCulture(culture, culture); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } } }
-
切換代碼如下:
/// <summary> /// Save current selected language to cookie. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_Click(object sender, EventArgs e) { if (ddlLanguages.SelectedIndex > 0) { // Selected language. string strLanguage = ddlLanguages.SelectedValue; // Set the Cookies. HttpCookie acookie = new HttpCookie(strKeyName); acookie.Value = strLanguage; acookie.Expires = DateTime.MaxValue; Response.Cookies.Add(acookie); Response.Redirect(Request.RawUrl); } }
4、效果
-
測試切換效果,先測試切換到中文,如下:
切換后,我們訪問下http://sp2016 為中文效果,如下圖:
-
測試切換效果,先測試切換到英文,如下:
切換后,我們訪問下http://sp2016 為英文效果,如下圖:
源碼可以去我QQ群下載:203613636 謝謝