locked
Resources file shared between multiple features RRS feed

  • Question

  • Hi,

    I have a resource file in 14\Resources that is used between multiple features (workflow, Event Receiver and Timer Job).

    However, I have to write the key value in code in order to retrieve the value

    SPUtility.GetLocalizedString("$Resources:"+"Hello", Resources, workflowProperties.Web.Language);
    
    SPUtility.GetLocalizedString("$Resources:"+"Hello2", Resources, workflowProperties.Web.Language);
    
    SPUtility.GetLocalizedString("$Resources:"+"Hello3", Resources, workflowProperties.Web.Language);

    Is there a way to retrieve the value by creating static variable or something?

    Tuesday, June 11, 2013 2:10 PM

Answers

  • If you're working in an environment where all systems are installed in the same language you could use something like this:

    public static class ResourcesClass
    {
    	public static string Something
    	{
    		get { return SPUtility.GetLocalizedString("$Resources:Something" , Resources, (uint)System.Globalization.CultureInfo.CurrentUICulture.LCID);}
    		//or use this 
    		get { return SPUtility.GetLocalizedString("$Resources:Something" , Resources, (uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);}
    	}
    }

    If not, you should be careful since you might end up with the wrong Resource value depending on if you got more than 1 resource file.

    • Marked as answer by okdreamy Wednesday, June 12, 2013 9:26 AM
    Wednesday, June 12, 2013 7:24 AM

All replies

  • When using a static variable you couldn't hand over the web.Language so you would have to use some like Thread.Current.CurrentThread.CurrentUICulture instead which would be available at all times. If web.Language and the thread culture would differ, you could run into some ugly errors.

    Besides that you could create static methods with 1 or 2 parameter and hand over the web language and if needed the Resources.

    Tuesday, June 11, 2013 2:38 PM
  • Yes but it will be the same case...

            private string GetResource(string resourceName)
            {
                    return SPUtility.GetLocalizedString("$Resources:" + resourceName, Resources, workflowProperties.Web.Language);
            }

    and to retrieve the value:

    GetResource("something");

    Is there no way to create a class from the resource file so I can directly use the variable?

    item["Title"] = ResourceClass.Something;
    item.Update();


    Wednesday, June 12, 2013 6:23 AM
  • Hi Okdreamy

    have a look at this Page: generating a class dynamically

    BUT instead of the first code block you can use something more dynamically like the add function from the dictionary and iterate throught your resx file.

    I think setting this up is pretty cool!

    Happy Translating!

    Timo


    Only 10 Choices of SharePoint - you love it or not

    Wednesday, June 12, 2013 6:57 AM
  • If you're working in an environment where all systems are installed in the same language you could use something like this:

    public static class ResourcesClass
    {
    	public static string Something
    	{
    		get { return SPUtility.GetLocalizedString("$Resources:Something" , Resources, (uint)System.Globalization.CultureInfo.CurrentUICulture.LCID);}
    		//or use this 
    		get { return SPUtility.GetLocalizedString("$Resources:Something" , Resources, (uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);}
    	}
    }

    If not, you should be careful since you might end up with the wrong Resource value depending on if you got more than 1 resource file.

    • Marked as answer by okdreamy Wednesday, June 12, 2013 9:26 AM
    Wednesday, June 12, 2013 7:24 AM
  • Hi Timo, thanks for you reply but it does create a dll right? I dont know how it will be handled in sharePoint workflow... does it need to be deployed in GAC at runtime?

    Andreas: yes there will be only 1 resource file for this project. I am not sure to understand why I cant use web.Language from workflowProperties..

    In case there is problem, may I just add several Resources files for other language with the same information?

    Wednesday, June 12, 2013 8:20 AM
  • Andreas: yes there will be only 1 resource file for this project. I am not sure to understand why I cant use web.Language from workflowProperties..

    In case there is problem, may I just add several Resources files for other language with the same information?

    Hey, 

    In a static property you can't depend on variables like workflowProperties, so you have to rely on information which are avialable at that point in time. So using the language of the current thread is a viable option, while using the workflow properties is not.

    Wednesday, June 12, 2013 8:27 AM
  • Thanks for the detail.

    Are

    (uint)System.Globalization.CultureInfo.CurrentUICulture.LCID);

    and
    (uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);

    equivalent?


    Wednesday, June 12, 2013 9:28 AM
  • The first seems to refer to the System Culture of the SharePoint Server while the second one depends on the language of the current which is currently used. So If you're using an internet explorer running with german language, you'll get 1031 in return. If all your local software and SharePoint software are in the same language, they're equivalent.
    Wednesday, June 12, 2013 12:08 PM
  • Actually it generates the DLL when you build the Solution, after its build you can use it too.

    If you include it in your deployment file you are good to go and it can be used.

    But dont Forget to add it as safe :)

    Timo


    Only 10 Choices of SharePoint - you love it or not

    Wednesday, June 12, 2013 5:46 PM