locked
Two question about localization of custom server controls RRS feed

  • Question

  • User-718569536 posted

    Hi, I have some question about localization of custom server controls. It's a very serious and critical problem to me, which deserves posted on such a memorial day.

    To localize my custom server controls, I have prepared resource files for it. On the way of my inspection process, I've found the following facts.

    1. ASP.NET 2.0 has provided with new Resource file manipulating models, but it does not bring much benefit on custom server controls development. For custom server control's, we have to follow the good-old process, like converting resource files from text or resx files, compiling it by ResourceGenerator and AssemblyLinker.

    2. Resource files and satelite assmbly can be located GAC, but it can also be placed on BIN directory of web application in which the cust.server control is used.

    Now, I have made resource files and sat.assemblies, and have successed in getting data from resource files in cust.server control's rendering method. So far, it works well. But a few question remains, to complete what I want to realize.

    The problem is as follows

    --------------------------------
    1. How to construct ResourceManager in custom control's code.
    --------------------------------
    I don't know the valid or established way to construct ResourceManager instance in "OnInit" method in custom control. My code is as follows.

    const string resourceName = "Samples.AspNet.CS.Controls.BookRsc";
    string programPath = Parent.Page.Server.MapPath("BIN");
    FileInfo fiDll = 
    	new FileInfo(programPath + "\\" + resourceName + ".dll");
    FileInfo fiRsc = 
    	new FileInfo(programPath + "\\" + resourceName + ".resources");
    if ((fiDll.Exists) && (fiRsc.Exists))
    {
    	rm = ResourceManager.CreateFileBasedResourceManager(
    		resourceName,
    		programPath,
    		null);
    }
    else
    {
    	// Following code is not correct.
    	// How shoul I get resource manager when resources are in GAC?
    	rm = new ResourceManager(resourceName, 
    		Assembly.GetExecutingAssembly());
    }
    


    Upper block of "if"-clause is for the case resource files and sat.assemblies are in the "BIN" folder of Web application. But in case they miss, and they are located in GAC, I can't figure out how to construct "rm".


    --------------------------------
    2. Can I localize attribute value of "Description"?
    --------------------------------
    To tell the truth, I'm mostly giving up.
    As you may anticipate from this title easily, I dont't have to put extra explanation on this matter.
    I just want to get a definit informatin if it's possible to localize it or not.

    For example, is it possible to write such a code to realize it.(Of course it can't be built, arising warnings like "you cant put formula in there") Or is there any hidden parameter in Description-attribute?

    [
    Bindable(true),
    Category("Appearance"),
    Description(rm.GetString("Desc_Of_Auther")),
    ]
    public virtual Author Author
    {
    	...
    }
    


    If it's impossible, then how do most of major world custom control developers localize Description attribute. Only way I can imagin is that, they are using directive like following.

    #define ja
    // #define de
    // #define es
    ...
    	[
    	Bindable(true),
    	Category("Appearance"),
    #if ja	
    	Description(...explanation in Japanese...),
    #else if de
    	Description(...explanation in Germany...),
    #else if es
    	Description(...explanation in Spanish...),
    #else
    	Description("Information about author"),
    #endif
    	]
    ...
    


    I would appreciate any kind of information, instruction, suggestion or hint, etc.

    Thanks.


    --------------------------------
    Appendix
    --------------------------------
    In the process of inspecting this topics, I have looked over many topics in Web, and found that there's no practical samples which shows the way to localize custom server controls.
    As I'm so much fond of make the world premiere, I'll publish another one. The world premiere sample of "Custom server control's resource ussage". You can download it from

    http://www.ad.il24.net/~masa_cmt/Samples/RsrcControlsTest(Before).zip <-[Sample01]

    http://www.ad.il24.net/~masa_cmt/Samples/RsrcControlsTest(After).zip <-[Sample02]

    Sample01 is the beggining image of web application projects, in which conversion and build process of resource file are not in action. (Thus, program does not work when you start debugging in this status. Exception showing the ASP.NET failed to find proper resouce will arise.)

    In "/App_Code/Book/Resources" folder, you can find resx files and bat file. You can execute this batch program to make resource files and sat.assemblies. (Before doing it, you have to adjutst some part of path definition to fit your environment)

    After you execute the batch, you'll find two resource files and two dll files are generated. Please copy them to Web app's "BIN" folder, then start debugging. Now you'll see the program works. You can change the base Language of browser using internet option menu, to inspect if the program automatically choose the proper resource. (I'm sorry, I only prepare English and Japanese resources. Anyone can add resource to them.)

    Sample02 is the final image of it. (=if you follows the process descibed above, program configuration will be the same naturally.)

    Please try them. Any kind of information ,advice will be appreciated.

    Thanks.

    Friday, September 11, 2009 7:43 AM

Answers

All replies

  • User-2106054853 posted

    Hi,

    1. You can try Assembly.Load() method to load assembly in GAC:

    http://www.dotnetfunda.com/interview/exam123-how-to-load-assembly-from-gac.aspx

    2. As far as I know there's no elegant way to do so.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2009 5:31 AM
  • User-718569536 posted

     Thank you Allen. I think I'd better practice much more about GAC (fortunately it seems it's not much so complecated)

    Your answer for 2nd question is the decisive factor. I'll give up localizing custom server controls. Instead I'll use directive like "#define ja-JP".

    Any way, it's not such a ambtious matter to use resource file for simple server controls. (It requires less than 20 sentences which have to be translate in according with the culture info)

    Imagine each custom server controls have resource files. It will overflow BIN folder of application which uses a number of server controls.

    Now I've made up my mind. and have another argument.

    Should I add surfix representing culture name to custom server control's assembly?
    (f.e. "SomewhereLib.CustomControls.dll" for english culter and "SomewhereLib.CustomControls.ja.dll" for Japanese)

    But it's a another argument. I'll solve by myself. (But if you have any opinion, please give me a response)

     

    Tuesday, September 15, 2009 11:07 AM
  • User1196957752 posted

    2. Can I localize attribute value of "Description"?

    Not sure, but you can try create you own class inherited from DescriptionAttribute, like


    	public sealed class LocalizableDescriptionAttribute : DescriptionAttribute
    	{
    		private string _key;
    
    		public LocalizableDescriptionAttribute(string descriptionKey)
    			:base()
    		{
    			_key = descriptionKey;
    		}
    
    		public override string Description
    		{
    			get
    			{
    				return rm.GetString(_key);
    			}
    		}
    	}


    , and then you can write this

    [  
    Bindable(true),  
    Category("Appearance"),  
    LocalizableDescription("Desc_Of_Auther"),  
    ]  
    public virtual Author Author  
    {  
         ...  
    } 



    Tuesday, September 15, 2009 10:57 PM
  • User-718569536 posted

    Wow! this is the elegant way! I have just work out to realize this method. But problem concerning with my 1st question in this thread rises up and I've got stuck.

    In "LocalizableDescriptionAttribute" which you suggested, we have to create ResourceManager instance. The container program of this custom control is ASP.Net Application, so I put resourcefiles and sattelite assembly on it's BIN-folder.

    The full name of the custom control is "Samples.AspNet.CS.Controls.Book", so I named resource files and satelite assembly as "Samples.AspNet.CS.Controls.Book.[culturename].resources" and "Samples.AspNet.CS.Controls.Book.resources.dll"

    What I expect when I create ResouurceManager is either of following code works.

    (1) rm = new ResourceManager("Samples.AspNet.CS.Controls.Book", Assembly.GetExecutingAssembly());
    (2) rm = new ResourceManager("Samples.AspNet.CS.Controls.Book", typeof(Book).Assembly);
    (3) rm = new ResourceManager(typeof(Book));

    Neither of them arises exception when called. Howerver, when I try to get resource text from "rm" as follows

      CultureInfo ci = Thread.CurrentThread.CurrentCulture;
      strCap = rm.GetString("Cap_Title", ci);

    "System.Resources.MissingManifestResourceException" arises. There's no way to avoid it so far.
    It means the ResourceManager instance fails to get satelite assembly!

    ...................
    Of course it will work if I write code as follows.

      string targetDir = argDirectory  // <= argDirectory is argument of constructor
      rm = new ResourceManager("Samples.AspNet.CS.Controls.Book", targetDir);

    But if I make logic in this way, I'm afraid, it will degrade usability of ResourceManagerDescriptionAttribute and also the custom control which is provided with the attribute. I don't want to make program like this.


    --------------------------------
    The things what I want to know
    --------------------------------
    This time, I just want to know how to get satelite assembly of custom control and create ResourceManager instance, in Web Application. The satelite assembly is different from that of Web Application's, and it is located in Bin-folder of it.

    That's the problem. Anyone, please help me.


    ************
    If it solves, I promise to refresh the sample's mentioned at the APPENDIX of Top of this Thred.

    Thanks

    Wednesday, September 16, 2009 11:30 AM
  • User1196957752 posted

    I've just found this topic in msdn. It describes how you should place your assemblies that .NET runtime can pick them up. Maybe it helps http://msdn.microsoft.com/en-us/library/1ztca10y(VS.71).aspx

    Wednesday, September 16, 2009 1:23 PM