none
Multilanguage Resource Properties Question RRS feed

  • Question

  • I'm a little bit confused regarding the use of resource properties for SharePoint 2010. I've read this article , but still no conclusion for me. After reading this text

    "In the SharePoint Foundation object model, a user resource is represented by an instance of the SPUserResource class. For example, the display text for a list title is provided by the Title property of the SPList object that represents the list. The Title property, in turn, gets its value from an SPUserResource object that is returned by the list's TitleResource property.

    When new user interface text is added to a website after it is created, the SPUserResource object that backs the text returns the same value—the initial value—for all languages until someone adds translations. "

    I expected the following behaviour: Until the SPUserResource object is filled a SPField should use the default Title value, but after the TitleResource is set for each supported language, that value should be used instead of the one which was set initially. However for this peace of code:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
      SPWeb currentWeb = (SPWeb)properties.Feature.Parent;
    
      string fieldName = currentWeb.Fields.Add("MyField", SPFieldType.Text, false);
    
      SPField field = currentWeb.Fields.GetFieldByInternalName(fieldName);
    
      int count = 0;
      foreach (CultureInfo culture in currentWeb.SupportedUICultures)
      {
        field.TitleResource.SetValueForUICulture(culture, "MyField Title " + count);
        count++;
      }
    
      field.Update();
    }
    
    For my default language the title is still "MyField" instead of "MyField Title x". How come? Any explanation? Am I expecting too much?

    Monday, February 21, 2011 11:00 AM

Answers

  • From what I've found out there's a difference between using SetValueForUICulture for your default language and using the "Title" property as there's some special code which is executed when changing this value. So there's no restriction to not use resource values for your default language, but it's not doing the whole trick as some internal values are not set correctly.

    The documentation of SPUserResource describes an other way to set the resource for different languages as you can change the culture of your current thread and use the title property afterswards. So for every language you first change the thread culture and than set the title property. This seems to be working fine for me.

    Thursday, April 7, 2011 10:59 AM

All replies

  • Hi,

    Are you sure that the property IsMultilingual of the SPWeb is set to true?

    If not, please set it to true and try it again.

    Also you can see the following document.

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuserresource.setvalueforuiculture.aspx


    Microsoft Online Community Support
    Tuesday, February 22, 2011 3:40 AM
  • Hey,

    thanks for your reply. Yes IsMultilingual is set to true as I activated the alternate language via UI.

    From my experiments if found the following solution: If I set field.Title with the Resourcevalue for web.UICulture which is the default UI culture the correct displayname is shown from the beginning.

    However this doesn't seem to be the best solution as I have to set the Title property explicitly to a value even though TitleResource has been filled for each supported language :/

    Tuesday, February 22, 2011 8:55 AM
  • Hi,

    You can create a console application with the following code to test the problem.

     SPField field = web.Fields["MyField"];
          int count = 0;
          foreach (CultureInfo culture in web.SupportedUICultures)
          {               
            string value1 = field.TitleResource.GetValueForUICulture(culture);
            field.TitleResource.SetValueForUICulture(culture, "my field"+count.ToString());
            Console.Write(value1);
            Console.Write("   :   ");
            string value = field.TitleResource.GetValueForUICulture(culture);
            
           Console.WriteLine(value);
            count++;
          }
          field.Update();
    

    If all the resource value of other cultures have been changed, it means the method works. 

    Please make sure that you have multiple cultures which are supported. Maybe you will find that result value of the default culture can't be changed. 


    Microsoft Online Community Support
    Thursday, February 24, 2011 8:05 AM
  • Hi,

    I've tried your console application and got a pretty strange result. I've expanded your example code and I'll explain what I get afterswards:

    string fieldname = "MyField";
    if (!web.Fields.ContainsField(fieldname))
    {
      web.Fields.Add(fieldname, SPFieldType.Text, true);
    }
    
    SPField field = web.Fields.GetFieldByInternalName(fieldname);
    int count = 0;
    foreach (CultureInfo culture in web.SupportedUICultures)
    {
      // Get original value
      string value1 = field.TitleResource.GetValueForUICulture(culture);
      // Set new value
      field.TitleResource.SetValueForUICulture(culture, "my field" + count.ToString());
      // Get what should be the new value
      string value = field.TitleResource.GetValueForUICulture(culture);
            
      Console.Write(value1);
      Console.Write("  :  ");
      Console.WriteLine(value);
      count++;
    }
          
    field.Update();
    
    field = web.Fields.GetFieldByInternalName(fieldname);
    foreach (CultureInfo culture in web.SupportedUICultures)
    {
      // Get AGAIN what should be the new value
      string value = field.TitleResource.GetValueForUICulture(culture);
    
      Console.WriteLine(value);
      count++;
    }
    

    So what I'm doing is creating a field if it doesn't exist yet. After that I go on and read the TitleResource for web.SupportedUICultures, change this resource and receive again. After this the field is updated, so the value should be stored, however when I go ahead and receive the field and TitleResources again, I get the old values and not the one I edited before.

    Thursday, February 24, 2011 10:50 AM
  • "Maybe you will find that result value of the default culture can't be changed."

    Why shouldn't I be able to change the resource value for the default culture? I've read through this article "Understanding the MUI" but I couldn't find why this should be true. Experimenting with both english and german as default language I found out that always the TitleResource for the default language seems to stick to the internal / original value no matter which value has been set for the language.

    However .. digging into it the problem only exists if I create a field, change the TitleResource right after and read the values immediately after that. If I restart my console application the TitleResource seems to be displayed correctly, but on the website the original InternalName is displayed everywhere instead of the TitleResource for the given language.

    After all it feels pretty strange to me (for SPWeb it works fine right away), but I can't figure out what I'm doing wrong.

    Thursday, February 24, 2011 4:07 PM
  • Hi,

    I have mentioned that the modification is not valid for default culture. We can't update the resource value for default culture.

    Because the default resource value for other cultures come from the value of default culture.

    Regarding the SPUserResource , you can see the following document.

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuserresource.aspx

    And I have proved that the change for other cultures works well.

    Regarding the language settings from UI, you can do it with the following steps: Site Settings=>Site Administrator=>Language settings


    Microsoft Online Community Support
    Friday, February 25, 2011 2:55 AM
  • I've read the article more than once and I still don't get the reason why I shouldn't be able to update the TitleResource or DescriptionResource for my default culture. Using the resource of the default language for undefinied languages is reasonable and makes sense to me.

    Why I shouldn't be able to change the SPUserResource for the default language at all? Funny thing .. if I change the Title attribute to the value I want it to be, the TitleResource for the default language gets changed aswell.

    Friday, February 25, 2011 1:14 PM
  • Hi,

    If you want to change the resource value for default culture, you can create a new field with new display name and title.

    The document has mentioned that when a new resource is created, the text for all cultures is in the default language of the Web site. And subsequently, someone can translate the text for a particular culture and add the translation to the resource, replacing the untranslated text. So you can think that the SetValueForUICulture method is used to set translation for alternate culture. 

    So I don't think it's necessary to modify the resource value for default culture. 

    The root reason why you can't change the resource value for default culture is that the product developers have made restriction for that.

    You can open the SetValueForUICulture method of SPUserResource  class with Reflector to see the exact code.


    Microsoft Online Community Support
    • Marked as answer by KeFang Chen Monday, February 28, 2011 9:43 AM
    • Unmarked as answer by Andreas Blueher Thursday, March 24, 2011 8:56 AM
    Saturday, February 26, 2011 7:17 AM
  • Hello,

    after quiet some time I came back here to check again my problem. I've looked up both SetValueForUICulture and GetValueForUICulture with Reflector and I couldn't find a single line of code which would suggest why I can't set Resource values for my default language.

    Just to help you understand, why it's necessary to have something like that: We are a german software company which develops a multilanguage SharePoint application. So our default language is German for obvious reasons. Being forced to use the german title as internalname for our fields means that I would have to use Umlaute like ä,ö,ü or using eg _x00c4_ = Ä, _x00e4_ = ä instead.

    Thursday, March 24, 2011 10:05 AM
  • From what I've found out there's a difference between using SetValueForUICulture for your default language and using the "Title" property as there's some special code which is executed when changing this value. So there's no restriction to not use resource values for your default language, but it's not doing the whole trick as some internal values are not set correctly.

    The documentation of SPUserResource describes an other way to set the resource for different languages as you can change the culture of your current thread and use the title property afterswards. So for every language you first change the thread culture and than set the title property. This seems to be working fine for me.

    Thursday, April 7, 2011 10:59 AM
  • (...)

    The root reason why you can't change the resource value for default culture is that the product developers have made restriction for that.

    (...)


    Microsoft Online Community Support
    If 'developers made a restriction', why does the method not throw an exception or return an invalid status when used against these restrictions? Why is this not explicitly documented?

    Pierre

    Tuesday, August 25, 2015 9:35 AM
  • I really have no idea why this is the case. Till this day i'm using the workaround setting the thread culture and changing the title afterwards and it works just perfectly.

    Regards Andreas MCPD SharePoint 2010. Please remember to mark your question as "answered"/"Vote helpful" if this solves/helps your problem.

    Tuesday, August 25, 2015 9:37 AM