Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

c# - List of all available languages for Windows .NET framework

I've been searching for the answer over the net, but I don't seem to be able to find a comprehensive list of all languages available for my app with their exact display name. (I find many lists, but none of them seem to match the language strings I have. Read on.) The closest one I found is this one, but is not complete.

I am coding a plugin in C# where the text is exported from SDL Trados Studio to Word and the language is set accordingly in Word.

I need to write a custom method that casts these language names to their relevant Word.Language equivalent. This is because Trados (using the Windows cultures and .NET framework 4.5) has different language (locale) names than the Word.Language names.

While Hungarian is the same, TraditionalChinese (Word.Language) is Chinese (Traditional, Taiwan) in Trados.

I do have a comprehensive list for Word.Languages already:

public static class LanguageList
{
    public static readonly HashSet<LanguageItem> _languageList = new HashSet<LanguageItem>(new[]
    {
        new LanguageItem("Arabic (Saudi Arabia)", 1025),
        new LanguageItem("Bulgarian", 1026),
        new LanguageItem("Catalan", 1027),
        new LanguageItem("Chinese (Taiwan)", 1028),
        new LanguageItem("Czech", 1029),
        new LanguageItem("Danish", 1030),
        new LanguageItem("German (Germany)", 1031),
        new LanguageItem("Greek", 1032),
        new LanguageItem("English (U.S.)", 1033),
        new LanguageItem("Spanish (Spain-Traditional Sort)", 1034),
        new LanguageItem("Finnish", 1035),
        new LanguageItem("French (France)", 1036),
        new LanguageItem("Hebrew", 1037),
        new LanguageItem("Hungarian", 1038),
        new LanguageItem("Icelandic", 1039),
        new LanguageItem("Italian (Italy)", 1040),
        new LanguageItem("Japanese", 1041),
        new LanguageItem("Korean", 1042),
        new LanguageItem("Dutch (Netherlands)", 1043),
        new LanguageItem("Norwegian (Bokm?l)", 1044),
        new LanguageItem("Polish", 1045),
        new LanguageItem("Portuguese (Brazil)", 1046),
        new LanguageItem("Rhaeto-Romanic", 1047),
        new LanguageItem("Romanian (Romania)", 1048),
        new LanguageItem("Russian (Russia)", 1049),
        new LanguageItem("Croatian", 1050),
        new LanguageItem("Slovak", 1051),
        new LanguageItem("Albanian", 1052),
        new LanguageItem("Swedish (Sweden)", 1053),
        new LanguageItem("Thai", 1054),
        new LanguageItem("Turkish", 1055),
        new LanguageItem("Urdu", 1056),
        new LanguageItem("Indonesian", 1057),
        new LanguageItem("Ukrainian", 1058),
        new LanguageItem("Belarusian", 1059),
        new LanguageItem("Slovenian", 1060),
        new LanguageItem("Estonian", 1061),
        new LanguageItem("Latvian", 1062),
        new LanguageItem("Lithuanian", 1063),
        new LanguageItem("Tajik", 1064),
        new LanguageItem("Farsi", 1065),
        new LanguageItem("Vietnamese", 1066),
        new LanguageItem("Armenian", 1067),
        new LanguageItem("Azeri (Latin)", 1068),
        new LanguageItem("Basque", 1069),
        new LanguageItem("Sorbian", 1070),
        new LanguageItem("FYRO Macedonian", 1071),
        new LanguageItem("Sutu", 1072),
        new LanguageItem("Tsonga", 1073),
        new LanguageItem("Tswana", 1074),
        new LanguageItem("Venda", 1075),
        new LanguageItem("Xhosa", 1076),
        new LanguageItem("Zulu", 1077),
        new LanguageItem("Afrikaans", 1078),
        new LanguageItem("Georgian", 1079),
        new LanguageItem("Faeroese", 1080),
        new LanguageItem("Hindi", 1081),
        new LanguageItem("Maltese", 1082),
        new LanguageItem("Sami (Lappish)", 1083),
        new LanguageItem("Gaelic (Scotland)", 1084),
        new LanguageItem("Yiddish", 1085),
        new LanguageItem("Malay", 1086),
        new LanguageItem("Kazakh", 1087),
        new LanguageItem("Kyrgyz", 1088),
        new LanguageItem("Swahili", 1089),
        new LanguageItem("Turkmen", 1090),
        new LanguageItem("Uzbek (Latin)", 1091),
        new LanguageItem("Tatar", 1092),
        new LanguageItem("Bengali", 1093),
        new LanguageItem("Punjabi", 1094),
        new LanguageItem("Gujarati", 1095),
        new LanguageItem("Oriya", 1096),
        new LanguageItem("Tamil", 1097),
        new LanguageItem("Telugu", 1098),
        new LanguageItem("Kannada", 1099),
        new LanguageItem("Malayalam", 1100),
        new LanguageItem("Assamese", 1101),
        new LanguageItem("Marathi", 1102),
        new LanguageItem("Sanskrit", 1103),
        new LanguageItem("Mongolian", 1104),
        new LanguageItem("Tibetan (PRC)", 1105),
        new LanguageItem("Welsh", 1106),
        new LanguageItem("Khmer", 1107),
        new LanguageItem("Lao", 1108),
        new LanguageItem("Burmese", 1109),
        new LanguageItem("Galician", 1110),
        new LanguageItem("Konkani", 1111),
        new LanguageItem("Manipuri", 1112),
        new LanguageItem("Sindhi (Devanagari)", 1113),
        new LanguageItem("Syriac", 1114),
        new LanguageItem("Sinhalese", 1115),
        new LanguageItem("Cherokee", 1116),
        new LanguageItem("Inuktitut", 1117),
        new LanguageItem("Amharic", 1118),
        new LanguageItem("Tamazight", 1119),
        new LanguageItem("Kashmiri (Arabic)", 1120),
        new LanguageItem("Nepali", 1121),
        new LanguageItem("Frisian (Netherlands)", 1122),
        new LanguageItem("Pashto", 1123),
        new LanguageItem("Filipino", 1124),
        new LanguageItem("Divehi", 1125),
        new LanguageItem("Edo", 1126),
        new LanguageItem("Fulfulde", 1127),
        new LanguageItem("Hausa", 1128),
        new LanguageItem("Ibibio", 1129),
        new LanguageItem("Yoruba", 1130),
        new LanguageItem("Quechua (Bolivia)", 1131),
        new LanguageItem("Sepedi", 1132),
        new LanguageItem("Bashkir (Russia)", 1133),
        new LanguageItem("Luxembourgish (Luxembourg)", 1134),
        new LanguageItem("Greenlandic (Greenland)", 1135),
        new LanguageItem("Igbo", 1136),
        new LanguageItem("Kanuri", 1137),
        new LanguageItem("Oromo", 1138),
        new LanguageItem("Tigrigna (Ethiopia)", 1139),
        new LanguageItem("Guarani", 1140),
        new LanguageItem("Hawaiian", 1141),
        new LanguageItem("Latin", 1142),
        new LanguageItem("Somali", 1143),
        new LanguageItem("Yi", 1144),
        new LanguageItem("Papiamentu", 1145),
        new LanguageItem("Mapudungun (Chile)", 1146),
        new LanguageItem("Mohawk (Mohawk)", 1148),
        new LanguageItem("Breton (France)", 1150),
        new LanguageItem("Uyghur (PRC)", 1152),
        new LanguageItem("Maori", 1153),
        new LanguageItem("Occitan (France)", 1154),
        new LanguageItem("Corsican (France)", 1155),
        new LanguageItem("Alsatian (France)", 1156),
        new LanguageItem("Yakut (Russia)", 1157),
        new LanguageItem("K'iche (Guatemala)", 1158),
        new LanguageItem("Kinyarwanda (Rwanda)", 1159),
        new LanguageItem("Wolof (Senegal)", 1160),
        new LanguageItem("Dari (Afghanistan)", 1164),
        new LanguageItem("Scottish Gaelic (United Kingdom)", 1169),
        new LanguageItem("Arabic (Iraq)", 2049),
        new LanguageItem("Chinese (PRC)", 2052),
        new LanguageItem("German (Switzerland)", 2055),
        new LanguageItem("English (U.K.)", 2057),
        new LanguageItem("Spanish (Mexico)", 2058),
        new LanguageItem("French (Belgium)", 2060),
        new LanguageItem("Italian (Switzerland)", 2064),
        new LanguageItem("Dutch (Belgium)", 2067),
        new LanguageItem("Norwegian (Nynorsk)", 2068),
        new LanguageItem("Portuguese (Portugal)", 2070),
        new LanguageItem("Romanian (Moldova)", 2072),
        new LanguageItem("Russian (Moldova)", 2073),
        new LanguageItem("Serbian (Latin)", 2074),
        new LanguageItem("Swedish (Finland)", 2077),
        new LanguageItem("Azeri (Cyrillic)", 2092),
        new LanguageItem("Lower Sorbian (Germany)", 2094),
        new LanguageItem("Sami, Northern (Sweden)", 2107),
        new LanguageItem("Gaelic (Ireland)", 2108),
        new LanguageItem("Malay (Brunei Darussalam)", 2110),
        new LanguageItem("Uzbek (Cyrillic)", 2115),
        new LanguageItem("Bengali (Bangladesh)", 2117),
        new LanguageItem("Punjabi (Pakistan)", 2118),
        new LanguageItem("Mongolian (Mongolian)", 2128),
        new LanguageItem("Tibetan (Bhutan)", 2129),
        new LanguageItem("Sindhi (Arabic)", 2137),
        new LanguageItem("Inuktitut (Latin, Canada)", 2141),
        new LanguageItem("Tamazight (Latin)", 2143),
        new LanguageItem("Kashmiri", 2144),
        new LanguageItem("Nepali (India)", 2145),
        new LanguageItem("Quechua (Ecuador)", 2155),
        new LanguageItem("Tigrigna (Eritrea)", 2163),
        new LanguageItem("Arabic (Egypt)", 3073),
        new LanguageItem("Chinese (Hong Kong S.A.R.)", 3076),
        new LanguageItem("German (Austria)", 3079),
        new LanguageItem("English (Australia)", 3081),
        new LanguageItem("Spanish (Spain-Modern Sort)", 3082),
        new LanguageItem("French (Canada)", 3084),
        new LanguageItem("Serbian (Cyrillic)", 3098),
        new LanguageItem("Sami, Northern (Finland)", 3131),
        new LanguageItem("Quechua (Peru)", 3179),
        new LanguageItem("Arabic (Libya)", 4097),
        new LanguageItem("Chinese (Singapore)", 4100),
        new LanguageItem("German (Luxembourg)", 4103),
        new LanguageItem("English (Canada)", 4105),
        new LanguageItem("Spanish (Guatemala)", 4106),
        new LanguageItem("French (Switzerland)", 4108),
        new LanguageItem("Croatian (Latin, Bosnia and Herzegovina)", 4122),
        new LanguageItem("Sami, Lule (Norway)", 4155),
        new LanguageItem("Arabic (Algeria)", 5121),
        new LanguageItem("Chinese (Macao S.A.R.)", 5124),
        new LanguageItem("German (Liechtenstein)", 5127),
        new LanguageItem("English (New Zealand)", 5129),
        new LanguageItem("Spanish (Costa Rica)", 5130),
        new LanguageItem("French (Luxembourg)", 5132),
        new LanguageItem("Bosnian (Latin, Bosnia and Herzegovina)", 5146),
        new LanguageItem("Sami, Lule (Sweden)", 5179),
        new LanguageItem("Arabic (Morocco)", 6145),
        new LanguageItem("English (Ireland)", 6153),
        new LanguageItem("Spanish (Panama)", 6154),
        new LanguageItem("French (Monaco)", 6156),
        new LanguageItem("Serbian (Latin, Bosnia and Herzegovina)", 6170),
        new LanguageItem("Sami, Southern (Norway)", 6203),
        new LanguageItem("Arabic (Tunisia)", 7169),
        new LanguageItem("English (South Africa)", 7177),
        new LanguageItem("Spanish (Dominican Republic)", 7178),
        new LanguageItem("French (West Indies)", 7180),
        new LanguageItem("Serbian (Cyrillic, Bosnia and Herzegovina)", 7194),
        new LanguageItem("Sami, Southern (Sweden)", 7227),
        new LanguageItem("Arabic (Oman)", 8193),
        new LanguageItem("English (Jamaica)", 8201),
        new LanguageItem("Spanish (Venezuela)", 8202),
        new LanguageItem("French (Reunion)", 8204),
        new LanguageItem("Bosnian (Cyrillic, Bosnia and Herzegovina)", 8218),
        new LanguageItem("Sami, Skolt (Finland)", 8251),
        new LanguageItem("Arabic (Yemen)", 9217),
        new LanguageItem("English (Caribbean)", 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...