PythEch
PythEch

Reputation: 952

Embedding Localization Resources .DLL's to the Executable in C#?

I want to make my program multilingual. I have successfully made the program multilingual via Form's Localizable and Language properties. It made some .resx files. Then I deleted non-needed files such as images (which they are the same in all langauges) etc from the .resx files.

The problem is, for example, it also generates a folder called "en" and in that folder, another generated file is called "ProjectName.resources.dll".

Is there anyway to embed this resource file to the .exe? Adding it to the resources and setting Build Action to "Embedded Resource" doesn't work also.

Thanks.

Upvotes: 15

Views: 9808

Answers (2)

Mickey
Mickey

Reputation: 933

You've asked this question a while ago and you've already accepted an answer, but still I'll try to provide an alternative way. I had the same problem and this is how I solved it:

I added the dll as a Ressource to my C#-Project and added this code to my Main-Method (the one that starts your main winform).

public static void Main(string[] args)
{
    if (InitdeDEDll()) // Create dll if it's missing.
    {
        // Restart the application if the language-package was added
        Application.Restart();
        return;
    }
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new YOURMAINFORM());
}

private static bool InitdeDEDll() // Initialize the German de-DE DLL
{
    try
    {
        // Language of my package. This will be the name of the subfolder.
        string language = "de-DE";
        return TryCreateFileFromRessource(language, @"NAMEOFYOURDLL.dll",
            NAMESPACEOFYOURRESSOURCE.NAMEOFYOURDLLINRESSOURCEFILE);
    }
    catch (Exception)
    {
        return false;
    }
}

private static bool TryCreateFileFromRessource(string subfolder, string fileName, byte[] buffer)
{
    try
    {
        // path of the subfolder
        string subfolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + (subfolder != "" ? @"\" : "") + subfolder;

        // Create subfolder if it doesn't exist
        if (!Directory.Exists(subfolder))
            Directory.CreateDirectory(subfolderPath);


        fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\" + subfolder + (subfolder!=""?@"\":"") + fileName;
        if (!File.Exists(fileName)) // if the dll doesn't already exist, it has to be created
        {
            // Write dll
            Stream stream = File.Create(fileName);
            stream.Write(buffer, 0, buffer.GetLength(0));
            stream.Close();
        }
        else
        {
            return false;
        }
    }
    catch
    {
        return false;
    }

    return true;
}

}

Note: This will create the folder and language-dll again if it's missing, so you don't have to care anymore that you copy that folder and the dll with your exe-file. If you want it to be completely vanished this won't be the right approach of course.

Upvotes: 2

IvanP
IvanP

Reputation: 260

In .NET Framework 4 you can embed resource library into executable.

http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx

Just create same structure ( with localized folders 'lib/en', 'lib/de' ) and embed them.

private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
    AssemblyName MissingAssembly = new AssemblyName(args.Name);
    CultureInfo ci = MissingAssembly.CultureInfo;

    ...
    resourceName = "MyApp.lib." + ci.Name.Replace("-","_") + "." + MissingAssembly.Name + ".dll";
    var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
    ...
}

Upvotes: 7

Related Questions