How to embed multilanguage *.resx (or *.resources) files in single EXE?

前端 未结 4 1909
无人及你
无人及你 2020-12-14 02:32

There are plenty of tutorials how to create multilanguage RESX files and how to create satellite assemblies with AL.exe, but I haven\'t found working example how to embed RE

相关标签:
4条回答
  • 2020-12-14 02:54

    Use this program, it Works with me: EXEPack

    You just need to do manually everytime you compile, not sure if there is a command tool.

    0 讨论(0)
  • 2020-12-14 03:01

    I used the GetString approach above. The article Can't load a manifest resource with GetManifestResourceStream() describes how to correctly retrieve your resource as a stream object. After that, everything worked.

    0 讨论(0)
  • 2020-12-14 03:03

    You didn't find it because it's not the way the .NET framework works. .NET expects satellite DLLs in specifically named location (iow directories named after the language of the resources it contains. eg. de, de-DE, chs,...). If you don't work that way, .NET won't be able to apply its magic (which is to automatically pick the correct resource according to the current UI culture: Thread.CurrentThread.CurrentUICulture).

    0 讨论(0)
  • 2020-12-14 03:04

    My solution: program contains only one default language resource file (resx). All other languages are compiled from .resx to .resources and embedded as resource file. Important! I have changed extension because ".resources" is recognized as a special type of resource, so my French files is named "PIAE.LangResources.fr".

    Here is simple code to retrieve translated string (it should be improved with caching values from resource):

        internal static string GetString(string str, string lang)
        {
    
            if (string.IsNullOrEmpty(str)) throw new ArgumentNullException("empty language query string");
            if (string.IsNullOrEmpty(lang)) throw new ArgumentNullException("no language resource given");
    
            // culture-specific file, i.e. "LangResources.fr"
            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PIAE.LangResources."+lang);
    
            // resource not found, revert to default resource
            if (null == stream)
            {                                                                   
                stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PIAE.Properties.LangResources.resources");
            }
    
            ResourceReader reader = new ResourceReader(stream);
            IDictionaryEnumerator en= reader.GetEnumerator();
            while (en.MoveNext())
            {
                if (en.Key.Equals(str))
                {
                    return en.Value.ToString();
                }
            }
    
            // string not translated, revert to default resource
            return LangResources.ResourceManager.GetString(str);
        }
    
    0 讨论(0)
提交回复
热议问题