List of all available languages for Windows .NET framework

ε祈祈猫儿з 提交于 2019-11-30 09:50:57

The complete list of all languages can be returned from CultureInfo:

using System.Globalization;
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

foreach (CultureInfo culture in cultures)
{
    Debug.WriteLine(culture.EnglishName);
}

As in this post: Programmatic way to get all the available languages (in satellite assemblies)

And as covered on msdn.

And after extensive searching and testing, I found that the language collection that is used by the SDL Trados Studio is the CultureInfo.

It is accessed through the API as:

string strTgtLocaleId = EditorController.ActiveDocument.ActiveFile.Language.ToString();
string strTgtLanguage = EditorController.ActiveDocument.ActiveFile.Language.DisplayName.ToString();
int intTgtLanguageId = EditorController.ActiveDocument.ActiveFile.Language.CultureInfo.LCID;

Thus the full list actually that I need for my plugin (acknowledging @Jenszcz's observation on the legacy strings from earlier products) is in fact can be enumerated from the CultureInfo.

My goal was however to directly translate these codes to the Word version of the IDs. So I ran a code to compare the two lists. I used the Word.Language enumeration that I posted in the OP:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Word = Microsoft.Office.Interop.Word;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Object oMissing = System.Reflection.Missing.Value;
            Object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

            //OBJECTS OF FALSE AND TRUE
            Object oTrue = true;
            Object oFalse = false;


            //CREATING OBJECTS OF WORD AND DOCUMENT
            Word.Application oWord = new Word.Application();

            var test = oWord.Application.Languages;

            foreach (var item in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures))
            {
                if (LanguageList._languageList.SingleOrDefault(i => i.Id.Equals(item.LCID)) != null)
                {
                    Debug.WriteLine(LanguageList._languageList.SingleOrDefault(i => i.Id.Equals(item.LCID)).Name +
                        " -- " +
                        item.EnglishName +
                        " -- " +
                        ((int)item.LCID).ToString()
                    );
                }
                else if (LanguageList._languageList.SingleOrDefault(i => i.Id.Equals(item.Parent.LCID)) != null)
                {
                    Debug.Indent();
                    Debug.WriteLine("-------- PARENT MATCH: " + item.EnglishName + " -- " + ((int)item.Parent.LCID).ToString());
                    Debug.Unindent();
                }
                else
                {
                    Debug.Indent();
                    Debug.WriteLine("!!!!!!!! NO MATCH: " + item.EnglishName + " -- " + ((int)item.LCID).ToString());
                    Debug.Unindent();
                }
            }

        }
    }

And the result was very lucky for me. In fact the Word.WdLanguageID matched all the CultureInfo.LCID values one for one, except for the legacy and exotic locales (which is not needed for my plugin).

Therefore I ended up not even needing the list of language strings returned by item.EnglishName such as Chinese (Traditional, Taiwan).

So I skipped the enumeration whole cloth. The code now runs in milliseconds as compared to the minutes it originally took to loop through all languages in the Word.Languages. I used the following code to set the language in the Word Document:

try
{
    oWord.Selection.LanguageID = (Word.WdLanguageID)intTgtLanguageId;
}
catch (Exception)
{
    oWord.Selection.LanguageID = (Word.WdLanguageID)0;
}

This sets all matching languages, casting the LCID to the correct Word.WdLanguageID constant. For those that are not matched, it sets it to "Not set".

I think you are looking for Microsoft's Locale ID (LCID) list: https://msdn.microsoft.com/en-us/goglobal/bb964664.aspx

These strings are probably taken from SDL Studio embedded resources, from Sdl.LanguagePlatform.Lingua.dll. I did a quick try to locate your Taiwanese example, but it did not work. Still, to show you how not to do it, here is my code:

    static void Main(string[] args)
    {
        // forced enum iteration
        for (int n = 0; n < 6; ++n)
        {
            var localInfoSet = Sdl.LanguagePlatform.Lingua.Locales.LocaleInfoSet.GetLocaleInfoSet((Sdl.LanguagePlatform.Lingua.Locales.LocaleSource)n);

            var tradLocaleInfo = localInfoSet.Where(item => item.Name.Contains("Traditional,"));
            foreach (var item in tradLocaleInfo)
            {
                System.Diagnostics.Debug.WriteLine(item.Name);
            }
        }
    }

I deliberately left the full namespaces in instead of using them. Perhaps this can serve you as a starting point to get access to those lists. Or perhaps not, but it could be worth a try.

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