SharePoint DateTime Format and Regional Settings

Now, how exactly do you control the DateTime string representation and formatting in a SharePoint application?

Normally, in an ASP.NET Web Application, it depends on the Regional Settings (Control Panel -> Regional and Language Options) of the user running the application pool of the site. Unless you override the Culture options in the code.


So when I tried to ‘localize’ the DateTime representation for my SharePoint site, by changing the app pool user’s Regional settings, at first I was surprised it didn’t do the job. DateTime was still shown in the default en-US format.

This means SharePoint overrided the Regional settings of its application pool user. It took me a couple of minutes before I realized that for SharePoint applications, the DateTime formatting is controlled by opening the site -> Site Actions -> Site Settings -> Site Administration (column) -> Regional Settings

There, you have the option to fine tune the regional settings for the site, which will also affect the DateTime formatting.

After setting it to the one you desire, you save the settings and next time you open the site, DateTime will be formatted according to the locale you have selected.

Hope this helps!

Sample SharePoint project localization Part 2 of 2

Hello,

This article assumes you are already familiar with Part 1 and the post preceding it.

Here, we discuss how to localize the content of the SharePoint custom web site project you already have created.


Normally, your ASPX page content would be similar to the following:


We want those ‘hardcoded’ strings to be localization enabled.

1. Create your default resource file.
You can create a subfolder ‘Resources’ in your project and put it there. Give it a specific name, in our case it is SampleLocalization.resx

2. Extract the strings we want to localize, into a resource file.
Let’s assume we want to have the page title, button text and greeting message localized. We need to create a resource entry for each one of them. Your resource file should look similarly to:


3. Modify the Page attribute to contain Culture and UICulture tags.
It should be similar to:

Inherits=”SampleCode.Localized, SampleCode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=[Public Key Token]” Culture=”auto” UICulture=”auto” %>

4. Modify your page to refer to the resources, rather than the hardcoded strings


5. Make a copy of your resource file. This will be our second language. Let’s make it Bulgarian.
Name it ‘SampleLocalization.bg.resx’. Open it for editing and translate the text. For your convenience, here are the strings:

Button_Text – Бутон текст
Greeting – Здравейте, това е тест
Page_Title – Това е заглавието на страницата

Make sure you store it in the Resources folder, next to your SampleLocalization.resx file.

6. web.config modification
Open the web.config of the website that hosts your template page. Find (or add) your globalization node and modify it to contain Culture and UICulture attributes, equaling ‘auto’.


7. Deployment
Do a full deploy of your project. Also, don’t forget to copy your *.resx files (both of them) into the WSS VirtualDirectories App_GlobalResources. The path usually is:

C:\Inetpub\wwwroot\wss\VirtualDirectories\[Port Number]\App_GlobalResources

Open the web page in the browser and see the result. You can set language preference through the settings of your browser.

For IE:
Tools -> Internet Options -> General Tab -> Languages -> Add / Move Up / Move Down

For FF:
Tools -> Options -> Content tab -> Languages (bottom) Choose button -> Add / Move Up / Move Down

Add Bulgarian. Try how it works when Bulgarian is a first preference, or when it is last.

In English:


In Bulgarian:


Hope this is helpful,
Hristo Yankov

Sample SharePoint project localization Part 1 of 2

Hello,

This is a step-by-step tutorial on how to create a localized SharePoint custom web application. In this first part, we will discuss the creation of the sample web application itself, while the second one, will emphasize on the localization.

Prerequisite is a Visual Studio 2008 with WSS extensions 1.3. You can also work with Visual Studio 2005, but you will have to do part of the deployment manually.

1. Start Visual Studio and create new project of type SharePoint.

I entered ‘SPLocalization’ as name for the project.

2. Select “Full Trust”

Note: Check debug tab of Project Setting to ensure it contain correct path to your SharePoint server.

3. Add new item to our project

4. Select ‘Template’ element in SharePoint section. Change file name to ‘LocalizedPage.aspx’

— @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } –>

5. Now you should see project structure like this:

6. Create subfolder LAYOUTS under Template folder

7. Create subfolder SPLocalization under LAYOURS folder

8. Move LocalizedPage.aspx to SPLocalization folder (I use drag and drop the file). Now you should have the following structure:

9. Now let’s create a class that extends SharePoint layout pages. Add new class to your project.

10. Name it ‘LocalizedPage.aspx.cs’ and click “Add” button

<!– @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in 11. Now few other steps …

First add references to SharePoint and SharePoint.WebControls

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

Secondly make your class public, partial and derived from LayoutsPageBase

You should see something like this:

Now, since we have chosen GAC deployment model, we need to deploy our solution to Global Assembly and determine Public Key.

12. In Visual Studio Menu, Selection, Build -> Quick Deploy Solution -> Copy Binary(s)

This will compile our SharePoint project into DLL and deploy it to GAC. After deployment Output window will show that Deployment was successful:

13. Let’s go to Windows Assembly folder and find our library

If you open Properties of our assembly, you will be able to copy its public key token. In my case it was 7a1fde53a908a4fe.

We will need Public Key Token for our aspx.page.

Now, let’s return to our ASPX page.

14. Remove default text and add Page registration tag

<%@Page Language=”C#” MasterPageFile=”~/_layouts/application.master” Inherits=”{ClassName}, {Assembly}, Version=1.0.0.0, Culture=neutral, PublicKeyToken={PublicKeyToken}” %>

Now let’s replace {ClassName} with SPLocalization.LocalizedPage {Assembly} with SPLocalization {PublicKeyToken} with Public Key Token we obtained from GAC.

Now you should see something like this:

Now let’s add some content to the page and test if everything works.

15. Add ASP tag Content with ContentPlaceHolderID equals to PlaceHolderMain. Here is complete code

<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

Test message

<!–asp:Content>

Your Localized.aspx page should look like this:

<%@Page Language=”C#” MasterPageFile=”~/_layouts/application.master” Inherits=”SPLocalization.LocalizedPage, SPLocalization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a1fde53a908a4fe”%>

<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

Test message

<!–asp:Content>

16. Let’s make a full deploy. Visual Studio menu Build -> Deploy Solution.

After deployment is completed, Output window will show that Deploy was successful.

17. Now let’s go and check manually if LocalizedPage.aspx was deployed.

Browse to 12 directory (by default it is “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12”) then browse to TEMPLATE, then to LAYOUTS, then to SPLocalization. You should see our LozalizedPage.aspx was successfully deployed.

Now let’s try to access this page through SharePoint

18. Open SharePoint url of your site with prefix “_layouts/SPLocalization/LocalizedPage.aspx”.

In my case, it is “http://itech-vm-spdev/_layouts/SPLocalization/LocalizedPage.aspx&#8221;

When page is loaded you should see empty area but with our magic text: Test message

Now, let’s add SPLinkButton SharePoint control

19. Change your ASPX page to include Register of Microsoft.SharePoint assembly and replace static text with SPLinkButton control.

Your ASPX page should look like this:

<%@Page Language=”C#” MasterPageFile=”~/_layouts/application.master” Inherits=”SPLocalization.LocalizedPage, SPLocalization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a1fde53a908a4fe”%>

<%@Register Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” Namespace=”Microsoft.SharePoint.WebControls” TagPrefix=”cc1″ %>

<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

<cc1:SPLinkButton Text=”” runat=”server” ID=”myLink” />

<!–asp:Content>

20. Update your LocalizedPage class to include myLink definition. We will also assign Text value in OnLoad method. You class should look like this:

public partial class LocalizedPage : LayoutsPageBase

{

protected SPLinkButton myLink;

protected override void OnLoad(EventArgs e)

{

myLink.Text =”Our test link”;

}

}

21. Let’s do full deploy again

22. Once it is successfully deployed. Open LocalizedPage in the browser again. You should see that now you have a link on the page. This link is provided by SharePoint SPLinkButton control.

In the next article, we will discuss localization of the sample application we created…

Localization of SharePoint Project

By Hristo Yankov

UPDATE: For detailed step by step tutorial, check those posts:

SharePoint localization step-by-step part 1

SharePoint localization step-by-step part 2

Problem: The users require that your custom SharePoint ASP.NET site is available in more than one language. For instance – English and Hungarian.

Solution: ASP.NET localization. Technique is discussed in this article, step by step.

It is recommended that you leave localization to the end of the development, when page content is not going to change (too often). Otherwise, you will find yourself constantly syncing new web content with the resource files.



1. Create resource files.

Create a folder in your project called, for example, ‘Resources’ and add a new .resx file (right click -> Add -> New Item -> General -> Resource file) in it. DON’T give it a generic name like ‘Resources.resx’. Instead, name it similarly to your project – e.g. WarehouseManagement.resx. This will be the resource file for your default language (let’s assume it’s English).

2. Extract site content in the resource file

This is a boring, but relatively easy task to do. You need to extract page contents such as literals, control texts, headers, titles and etc. into the resource file. The actual text is being replaced with Resource reference, using:

<%$ Resources:[Resource File Name Without Extension],

[Resource Name]%>

For example, if you have:

<cc1:sptoolbarbutton id="btnSearch" runat="server" text="Search" 

OnClick="btnSearch_Click"/>

You extract the ‘Search’ button text into the resource file, and let’s assume you give it a key ‘btn_Search‘. Then you edit the button code so that you end up with:

<cc1:sptoolbarbutton id="btnSearchProperty" runat="server"

text="<%$Resources:WarehouseManagement,btn_Search %>"
OnClick="btnSearch_Click"/>

Another example:

<asp:content id="PageTitle" contentplaceholderid="PlaceHolderPageTitle" runat="server">

My Title Goes Here

</asp:content>

You extract ‘My Title Goes Here’ into the Resource file and let’s say you give it a key ‘Page_Title’. Then, you replace the page content with:

<asp:content id="PageTitle"

contentplaceholderid="PlaceHolderPageTitle"
runat="server">

<asp:Literal ID="literal" runat="server"
Text="<%$ Resources:WarehouseManagement,Page_Title %>" />

</asp:content>

Note that you can’t just have ‘‘ standing alone. This code either has to be embedded into another control’s property (Text, HeaderText, ErrorMessage, etc) or it has to be in a placeholder, which in this case is asp:Literal.

You need to do this for every text you want to localize.

Also, every page you are localizing, has to have those two attributes in the ‘Page’ node: Culture=”auto” UICulture=”auto”. Example:

<%@ Page Language="c#" MasterPageFile="~/_layouts/WarehouseManagement.master" Inherits="TestPage, WarehouseManagement, Version=1.0.0.0, Culture=neutral, PublicKeyToken=[token]" Culture=”auto” UICulture=”auto” %>

3. Second language

Once you are done moving your text into the resource file, you can make a copy of your resource file. Since we are localizing to Hungarian, rename the copy of the file to WarehouseManagement.hu.resx. Open the resource file for editing and translate the entries from English to Hungarian.

You have to keep the two resource files in sync. If you delete keys, add new ones or rename in one of them, the other one has to reflect it too.

4. web.config change

Make sure you have this node in your site’s web.config file:

<globalization fileencoding="utf-8"

uiCulture="auto" culture="auto" />

5. Deployment

Those resource files need to be deployed to your site’s App_GlobalResources folder. This usually is: C:\Inetpub\wwwroot\wss\VirtualDirectories\[Port Number]\App_GlobalResources

6. Language Preference

So how exactly do you set the language preference? How do you tell the page which localization (English or Hungarian) you want to see? That’s easy. Go to your browser’s Language setting and set your preference.

For IE:

Tools -> Internet Options -> General Tab -> Languages -> Add / Move Up / Move Down

For FF:

Tools -> Options -> Content tab -> Languages (bottom) Choose button -> Add / Move Up / Move Down

Your browser provides the page what is your language preference, by sending an appropriate header. So, at this point, what happens when the page renders? Browser sends the page request, along with the language preference header. ASP.NET replaces the resource reference with the actual value, from the appropriate resource file.

How ASP.NET determines which one is the appropriate resource file? Easy. Let’s say your first language preference is Arabic, second is Hungarian and third is English. You open the page, ASP.NET tries to find arabic localization file but fails, then it tries to locate Hungarian localization file – it is found and it is determined as the appropriate one to use.

What happens if your only language preference is ‘Arabic’ (and in our case, we don’t have localization for it)? We send the request, ASP.NET doesn’t find the right localization and since you don’t have other language preferences, it falls back to the default resource file, which is WarehouseManagement.resx and happens to be in English.

This gives you the ability to ‘plug and play’ new language localizations by simply copying one of the existing .resx files, give it a proper extension (depending on the language you are localizing to), translate the content and deploy it to the right location. You won’t need to change anything on your pages.

Also, note that this architecture allows every client of the web application to set language preference for himself, rather than you, setting the language translation globally for the whole site and every user.

For automation of the deployment of resource files, you may want to look into this blog post:

SharePoint Resources, Types, Use and Deployment