1

I am having some problems in changing the language in SharePoint programmatically. I have two language packs installed on my server (Norwegian and English). If I change the language manually through the GUI then its changing but when I do it programmatically it is setting the Lcid of the language in the background as default language but not translating the content. I am checking lcid on the page load. It is the selected one. I have also updated the site after changing locale ID.

Here is the code. Please tell me where I am missing something or something else.

ImageButton btn = sender as ImageButton;
            SPWeb spweb = SPContext.Current.Web;
            spweb.AllowUnsafeUpdates = true;
            int LCID = Convert.ToInt32(btn.ID);
            CultureInfo culture = new CultureInfo(LCID);
            spweb.Locale = culture;
            spweb.Update();
            string url = btn.ImageUrl;
            string btnId = btn.ID;
            imgBtnDefault.ImageUrl = url;
            imgBtnDefault.Height = 18;
            imgBtnDefault.Width = 25;
            spweb.AllowUnsafeUpdates = false;
Phil Greer
  • 2,287
  • 9
  • 21
  • 23
Pathfinder
  • 11
  • 1
  • 2

2 Answers2

1

Language in SharePoint that is Displayed to the user is based on a the HTTP Request Header ["Accepted Languages"] or the SharePoint language cookie if the user selects a specific language in the SharePoint language selection menu. This menu is only shown when activating one or more alternative languages on a site collection.

If you want to change language in the background for a specific part of the code then you will need to set the current running threads to the language you want to use.

System.Threading.Thread.CurrentThread.CurrentUICulture.Name="no" 
System.Threading.Thread.CurrentThread.CurrentCulture.Name="no"

By default Ajax requests for example, will fall back to the site collections default language, the language you selected as default when creating the site collection. On normal page requests it will use the users selected language(cookie or Accepted Languages)

And as mentioned before SPWeb.Local only has a public Get not a Set.

Benny Skogberg
  • 25,542
  • 12
  • 68
  • 163
Robban1980
  • 1,004
  • 7
  • 17
  • We've tried this but we noticed that some things do not get translated. For example when we load the site it says "Working on it" when that should be translated. The only way to get it to show the translation is if we set the browser to the language we're looking for. When I check the _spPageContextInfo some properties are in one locale and others are in another: http://imgur.com/a/ntoRF – Batman Jun 13 '17 at 18:08
0
spweb.Locale = culture;

This property is read only, you can't modify site's culture using object model.

As far as I know Site's languages are set-up on web application level not site collection level, You can change them by making changes to Web Application's web.config (culture) attribute.

Here's a tutorial on how you can change web application's culture using DB but you can do it by modifying web.onfig as well.

Hope it helps

Muhammad Raja
  • 9,250
  • 7
  • 44
  • 85