SharePoint 的发展越来越快, 现在很多中国的企业也用起了SharePoint, 如果是跨国公司, 这是在开发SharePoint的时候, 就要注意多语言的问题, 这里简单分享下如果实现多语言的用户界面.
- 创建一个空SharePoint 项目,创建2个Resources file.
SPDemo.resx
SPDemo.zh-CN.resx
Resources 文件的名字可以随便命名,但是要注意后面的语言的后缀 (zh-CN代表中国, es-US代表美国……)
简单给Resource文件添加一些值:
SP_Demo_Order_Amount |
订单数量 |
SP_Demo_Order_Name |
订单名称 |
SP_Demo_Order_Price |
订单价格 |
- 创建一个Application 页面,直接创建一个Application Page,并添加一些Label 和TextBox.
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Order.aspx.cs" Inherits="MultiLanInterface.Layouts.MultiLanInterface.Order" DynamicMasterPageFile="~masterurl/default.master" %> <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server"> </asp:Content> <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"> <div> <table> <tr> <td> <asp:Label ID="lblOrderName" Text="" runat="server"></asp:Label> </td> <td> <asp:TextBox ID="txtOrderName" runat="server"></asp:TextBox> </td> </tr> </table> </div> </asp:Content> <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server"> Application Page </asp:Content> <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" > My Application Page </asp:Content>
2. 确保Resources 文件部署到SharePoint Root/14/Resources/
3. 添加后台代码
using System; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.Utilities; namespace MultiLanInterface.Layouts.MultiLanInterface { public partial class Order : LayoutsPageBase { protected void Page_Load(object sender, EventArgs e) { InitControls(); } void InitControls() { lblOrderName.Text = SPUtility.GetLocalizedString("$Resources:SPDemo,SP_Demo_Order_Name", "$Resources:SPDemo", SPContext.Current.Web.Language); } } }
备注: GetLocalizedString(Source, Resource,language)方法的用法:根据Language值找到跟语言相对应的Resource文件的string值. 比如当前语言是汉语的话,就会找到SPdemo.zh-CN.resx文件.
4. 部署后查看页面就能看到效果了