How to get the available translations from a dll

夙愿已清 提交于 2020-01-03 02:48:11

问题


Is there a way to get the available resource translations of a .net dll?

Our software is being translated in some different languages, and I would like to give the user the choise of what language the software is in, although I would only like to let them choose only between the languages it has been translated in.


回答1:


I just got similar problem so just for future reference.

For my software translations are in the program folder, each under their own subfolder named after culture name. Code explains it all:

    private void SettingsForm_Load(object sender, EventArgs e)
    {
   // load default language to the list
        languageList.Add(new Language("English", "en"));
        string fileName = "myProgram.resources.dll";

   // load other languages available in the folder
        DirectoryInfo di = new DirectoryInfo(Application.StartupPath);
        foreach (DirectoryInfo dir in di.GetDirectories())
        {
            if (File.Exists(dir.FullName + "\\" + fileName))
            {
                try
                {
                    CultureInfo ci = new CultureInfo(dir.Name);
                    languageList.Add(new Language(ci.NativeName, ci.Name));
                }
                catch
                {
                    // whatever happens just don't load the language and proceed ;)
                    continue;
                }
            }
        }
    }

It ads some exception handling overhead but how many users will create custom folders in the installation directory with the fake resource named exactly as localization file?? :P




回答2:


So you need to be able to get the culture of the base dll, and then enumerate all the satellite dlls?

The first part is easy enough (just get the assembly level attribute).

For the latter, there does not appear to be a helper on the class ResourceManager, which means you would need to emulate its probing machanism (including, potentially, all the overrides available with attributes and .config file).

An alternative is to build the list at build or install (latter would work better if additional languages can be added later), creates a list for the .config file.



来源:https://stackoverflow.com/questions/585461/how-to-get-the-available-translations-from-a-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!