Monday, April 1, 2013

SharePoint-13 Multilingual for visual webparts

I had a requirement to create a multilingual visual webpart.The webpart has to be displayed in the language which is described by the browser.

Here is the way to achieve it in sharepoint 13 using the VS12

1)Create a empty sharepoint 13 solution Farm solution .
2) Add a sharepoin 13 project into it .
3)Add a mapped foder "Resources" to the project .
4)Now you gonna add three resources one for english "testMutliLing.en-US.resx" and one for german
    "" and one default  "testMutliLing.resx into the mapped folder.
5)In the designer of resource file add the key and value for both english and german resource file .

6)Now open the ascx file and add "label"
7)In the page load event add the following code

int id=1033;
//to get the default broweser language either "en-US" or "de-DE" etc etc

string a = HttpContext.Current.Request.UserLanguages[0].ToString();
if (a.Contains("en-US")){
id = 1033;

id = 1031;
//german =1031 //english =1033
Label.Text =
SPUtility.GetLocalizedString("$Resources:testMutliLing,String1", "testMutliLing",(uint)id);

The en-Us is equal to 1033 you get the different language codes for different languages if you browse for it in internet.

or you can even use

Label.Text = SPUtility.GetLocalizedString("$Resources:testMutliLing,String1", "testMutliLing",(uint)CultureInfo.CurrentUICulture.LCID);

Deploy the solution add the webpart to the page .

It shows the english version picking the value for "String1"  from english resource file.

to check for german .In IE goto internet options-->Languages-->add german de-DE and make it as default language by moving it to the top .say ok and refresh the page

Now the text for Lable is picked by "String1"  from the german resource file.

Also if you want to have multilingual markups you can use as below

<asp:Label ID="lbltest" runat="server" Text="<%$Resources:testMutliLing, String1; %>"></asp:Label>

1 comment:

  1. you can even use
    "(uint)CultureInfo.CurrentUICulture.LCID" to get the default broweser language