Resource Files

Jun 30, 2009 at 3:47 PM

I'm provisioning features that contain a lot of default content etc and was looking to put these into resource files (resx) more to separate them out really than specifically for localisation.  I went straight to the latest guidance (as I have being recently to check on sp dev best practices) and (at first glance of the TM RI project at least) it doesn't look like you are using these... are there any plans in the future to implement these in the SPG projects at all?

Jul 28, 2009 at 10:02 PM

Hi,

You could do smth like this:

1) Create Sattelite assembly with resource files

2) add attribute to AssemblyInfo: InternalsVisibleTo

This will make internal resources class visible to all projects that you would like it to have, and allow you to use ResFileName.ResKey instead of string "reskey".

Or

1) Create Sattelite assembly with resource files

2) Create a class that will read from that assembly

 

for example:

    /// <summary>
    /// Provides methods for accessing resources from specified resource manager.
    /// </summary>
    internal class CoreResource
    {
        #region C'tors
        /// <summary>
        /// Initializes a new private instance of the <see cref="CoreResource"/> class.
        /// </summary>
        /// <remarks>
        /// Prohibit class instance.
        /// </remarks>
        private CoreResource()
        {
        }

        #endregion C'tors

        #region Internal Static Methods

        /// <summary>
        /// Gets the string.
        /// </summary>
        /// <param name="resourceManager">The resource manager.</param>
        /// <param name="culture">The culture.</param>
        /// <param name="name">The name - key value.</param>
        /// <param name="values">Optional parameters for string format.</param>
        /// <returns>
        /// Formated resource string of <paramref name="culture"/> or <c>null</c>.
        /// </returns>
        internal static string GetString(ResourceManager resourceManager, CultureInfo culture, string name, params object[] values)
        {
            string str = "{Empty}";
         
            if(resourceManager != null)
            {
                str = resourceManager.GetString(name, culture);
                str = CoreResource.GetStringFormat(str, values);
            }

            return str;
        }

        /// <summary>
        /// Gets formated string,
        /// </summary>
        /// <param name="returnedResource">The returned resource.</param>
        /// <param name="values">Optional values that should be replaced in string.</param>
        /// <returns>
        /// Formated resource string.
        /// </returns>
        internal static string GetStringFormat(string returnedResource, params object[] values)
        {
            if(values == null || values.Length == 0)
            {
                return returnedResource;
            }

            int bracketPos = returnedResource.IndexOf('{');

            if(Char.IsDigit(returnedResource[bracketPos + 1])
                && returnedResource[bracketPos + 2] == '}')
            {
                returnedResource = string.Format(returnedResource, values);
            }

            return returnedResource;
        }

        #endregion Internal Static Methods
    }

and then create a "wrapper class":

    /// <summary>
    /// Class manages access to resources.
    /// 
    /// It can be initialized many times in delivery project to reflect each resouce manager, or it could be
    /// overriden and extended in delivery project.
    /// 
    /// <code>
    /// 
    /// // default using
    /// int timeTaken = 10; // as days
    /// ResourceManager rm = new ResourceManager("MyResourcesFromSatelliteAssembly", Assembly.GetExecutingAssembly());
    /// string done = ImpaqResources.GetString(rm, "wf_done", timeTaken);
    /// string done2 = ImpaqResources.GetString(rm, CultureInfo.GetCultureInfo("en-US"), "wf_done", timeTaken);
    /// 
    /// // wrapper in delivery project
    /// public enum ResType
    /// {
    ///     aspx,
    ///     exception
    /// }
    /// 
    /// public static class MyResourcesFirstWrapper
    /// {
    ///     private static readonly ResourceManager _aspxResources = new ResourceManager("AspxResources", Assembly.GetExecutingAssembly());
    ///     private static readonly ResourceManager _exceptionsResources = new ResourceManager("ExceptionsResources", Assembly.GetExecutingAssembly());
    /// 
    ///     public static string GetString(ResType resType, name)
    ///     {
    ///         switch(resType)
    ///         {
    ///             case ResType.aspx:
    ///                 return ImpaqResources.GetString(_aspxResources, CultureInfo.CurrentCulture, name);
    ///             default:
    ///                 return ImpaqResources.GetString(_exceptionsResources, CultureInfo.CurrentCulture, name);
    ///         }
    ///     }
    /// }
    /// 
    /// public static class MyResourcesSecondWrapper
    /// {
    ///     private static readonly ImpaqResources _aspxResources = new ImpaqResources(new ResourceManager("AspxResources", Assembly.GetExecutingAssembly()));
    ///     private static readonly ImpaqResources _exceptionsResources = new ImpaqResources(Assembly.GetExecutingAssembly(), "ExceptionsResources");
    /// 
    ///     public static string GetString(ResType resType, name)
    ///     {
    ///         switch(resType)
    ///         {
    ///             case ResType.aspx:
    ///                 return _aspxResources.GetString(CultureInfo.CurrentCulture, name);
    ///             default:
    ///                 return _exceptionsResources.GetString(CultureInfo.CurrentCulture, name);
    ///         }
    ///     }
    /// }
    /// 
    /// // override resources
    /// public sealed class MyResources : ImpaqResources
    /// {
    ///     // do whatever you like to do - implement many resource managers, many Satellite assemblies etc.
    ///     // ...
    ///     
    ///     public string GetString(ResType resType, string name)
    ///     {
    ///         switch(resType)
    ///         {
    ///             case ResType.aspx:
    ///                 return base.GetString(...);
    ///             case ResType.X:
    ///                 return MyResources.GetString(...);
    ///             default:
    ///                 return ImpaqResources.GetString(...);
    ///         }
    ///     }
    /// }
    /// 
    /// </code>
    /// </summary>
    public class ImpaqResources
    {
        /// <summary>
        /// Current resource manager which will be used.
        /// </summary>
        private readonly ResourceManager _currentResourceManager;
        
        #region C'tors

        /// <summary>
        /// Initializes a new instance of the <see cref="ImpaqResources"/> class. Default resource manager is selected.
        /// </summary>
        public ImpaqResources()
        {
            this._currentResourceManager = new ResourceManager("Impaq.SharePoint.Common.Resources", typeof(ImpaqResources).Assembly);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ImpaqResources"/> class.
        /// </summary>
        /// <param name="resourceManager">The resource manager.</param>
        public ImpaqResources(ResourceManager resourceManager)
        {
            this._currentResourceManager = resourceManager;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ImpaqResources"/> class.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="resourceName">Name of the resource.</param>
        public ImpaqResources(Assembly assembly, string resourceName)
        {
            this._currentResourceManager = new ResourceManager(resourceName, assembly);
        }

        #endregion C'tors

        #region Public Virtual Methods
        /// <summary>
        /// Gets the string.
        /// </summary>
        /// <param name="name">The name - key value.</param>
        /// <param name="values">Optional parameters for string format.</param>
        /// <returns>
        /// Formated string of current cultrue.
        /// </returns>
        public virtual string GetString(string name, params object[] values)
        {
            return this.GetString(CultureInfo.CurrentCulture, name, values);
        }

        /// <summary>
        /// Gets the string.
        /// </summary>
        /// <param name="culture">Culture of the string that needs to be returned.</param>
        /// <param name="name">The name - key value.</param>
        /// <param name="values">Optional parameters for string format.</param>
        /// <returns>
        /// Formated string of <paramref name="culture"/>.
        /// </returns>
        public virtual string GetString(CultureInfo culture, string name, params object[] values)
        {
            return ImpaqResources.GetString(this._currentResourceManager, culture, name, values);
        }

        #endregion Public Virtual Methods

        #region Public Static Methods

        /// <summary>
        /// Gets the string.
        /// </summary>
        /// <param name="resourceManager">The resource manager.</param>
        /// <param name="name">The name - key value.</param>
        /// <param name="values">Optional parameters for string format.</param>
        /// <returns>
        /// Formated string of current cultrue.
        /// </returns>
        public static string GetString(ResourceManager resourceManager, string name, params object[] values)
        {
            return ImpaqResources.GetString(resourceManager, CultureInfo.CurrentCulture, name, values);
        }

        /// <summary>
        /// Gets the string.
        /// </summary>
        /// <param name="resourceManager">The resource manager.</param>
        /// <param name="culture">Culture of the string that needs to be returned.</param>
        /// <param name="name">The name - key value.</param>
        /// <param name="values">Optional parameters for string format.</param>
        /// <returns>
        /// Formated string of current cultrue.
        /// </returns>
        public static string GetString(ResourceManager resourceManager, CultureInfo culture, string name, params object[] values)
        {
            return CoreResource.GetString(resourceManager, culture, name, values);
        }

        #endregion Public Static Methods
    }

Before I was using ImpaqResources in each solution I have created, this time I was trying to separate it from "only one implementation"

Cheers,

   Jakub G