Archive

Posts Tagged ‘GetLocalizedString’

SharePoint: The usage of SPUtility.GetLocalizedString

May 12th, 2011 No comments

     Sometimes when I parse the Schema.xml of some list I need to resolve such attribute values as:

  • “$Resources:core,lists_Folder;/ONEOFYOURLISTNAME” – often can be found in Lookup-field definitions;
  • “$Resources:groupExpColl;” – can be found in View definition;
  • “$Resources:core,moreItemsParen;” – can be found in View definition;
  • “$ResourcesNoEncode:core,noXinviewofY_LIST;” – can be found in View definition;
  • “$Resources:spscore,FeaturePushdownTaskTitle;”;
  • “$Resources:cmscore,PagesListDisplayName”;
  • and so on.

The method SPUtility.GetLocalizedString(string source, string defaultResourceFile, uint language) is dedicated for retrieving the value for a named resource string from the resource file for a specified language. In example above, a number of strings already contains indications what resource file should be used for searching the localized value, i.e. core, spscore and cmscore. That is why, in most cases, null for defaultResourceFile-parameter can be passed into SPUtility.GetLocalizedString.

If resource file name isn’t included into string (e.g. “$Resources:groupExpColl;”) and defaultResourceFile-parameter is null, I believe, SPUtility.GetLocalizedString searchs in core.resx or in its language-specific variant, e.g. core.en-US.resx (according to language-parameter).

I’ve wrapped the usage of SPUtility.GetLocalizedString into small method, which takes it upon itself to detect the current language.

public static string GetLocalizedString(SPWeb spWeb, string source)
{
    if (string.IsNullOrEmpty(source) || !source.StartsWith("$Resources", StringComparison.OrdinalIgnoreCase))
        return source;
    uint language = spWeb != null ? spWeb.Language : (HttpContext.Current != null ? (uint)Thread.CurrentThread.CurrentUICulture.LCID : 1033);
    return SPUtility.GetLocalizedString(source, null, language);
}

public static string GetLocalizedString(string source)
{
    return GetLocalizedString(null, source);
}

Below the results of GetLocalizedString in respect to each string from the above-mentioned example

String

Localized string

“$Resources:core,lists_Folder;/ONEOFYOURLISTNAME” “Lists/ONEOFYOURLISTNAME”
“$Resources:groupExpColl;” “Expand/Collapse”
“$Resources:core,moreItemsParen;” “(More Items…)”
“$ResourcesNoEncode:core,noXinviewofY_LIST;” “<HTML>There are no items to show in this view of the \”</HTML><ListProperty Select=\”Title\” HTMLEncode=\”TRUE\”><HTML>\” list.</HTML>”
“$Resources:spscore,FeaturePushdownTaskTitle;” “Enable new features on existing sites”
“$Resources:cmscore,PagesListDisplayName” “Pages”
Related posts: