SharePoint2013與SharePoint2016語言切換原理以及如何使用代碼進行語言切換


1、前言

SharePoint 2010版本,在首頁面直接"選擇顯示語言"的菜單(如下圖所示),如下圖 :

在sharepoint2013和sharepoint2016並非如此。

  1. 這里首先需要安裝語言包,SharePoint2016語言包下載地址:

https://www.microsoft.com/en-us/download/details.aspx?id=51492

SharePoint2013語言包下載地址:

https://www.microsoft.com/zh-cn/download/details.aspx?id=37140

 

注意:下載安裝完畢后都要進行重新配置

 

  1. 在【網站設置】-【語言設置】設置備用語言,這里為英語,如下圖

     

  2. 進行【關於我】進行【語言區域設置】如下圖:

     

     

     

2、原理

上面說到SharePoint2010的語言切換根據記錄cookie的方式可以快速進行切換,只要記錄cookie的值lcid值,對於SharePoint2013和SharePoint2016的語言采用此方式不行,根據IE語言版本來確定顯示哪個語言?,因此我們這里使用Fiddler工具進行http頭文件的獲取分析,主要分析:Accept-Language

說明:

Accept-Language:瀏覽器所希望的語言種類,當服務器能夠提供一種以上的語言版本時要用到;根據http頭的情況,用代碼修改Accept-Language的設置來模擬設置語言版本的切換

  1. 我們打開英文站點:http://sp2016 用Fiddler抓取分析,如下圖:

    分析結果:

  2. 我們打開中文站點:http://sp2016(切換后)的Fiddler抓取圖,如下圖:

既然知道這個原理,切換代碼把每次要切換的語言版本保存到cookie值里,那么我們第一次系統用代碼切換前cookie值為空設置一個默認為中文,使用httpModule方式來分析http頭的Accept-Language的值,如果包含en-US就設置為英文版本,如果包含zh-CN就設置為中文。明白原理寫代碼就方便多了。

3、代碼

  1. 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);

}

}

}

}

 

 

 

  1. 切換代碼如下:
    /// <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、效果

  1. 測試切換效果,先測試切換到中文,如下:

切換后,我們訪問下http://sp2016 為中文效果,如下圖:

  1. 測試切換效果,先測試切換到英文,如下:

切換后,我們訪問下http://sp2016 為英文效果,如下圖:

源碼可以去我QQ群下載:203613636 謝謝

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM