source: trunk/eraser6/Eraser.Manager/Language.cs @ 1353

Revision 1353, 4.3 KB checked in by lowjoel, 5 years ago (diff)

Display the displayed name of the language in the Eraser Settings dialog instead of the native name as this uses the names from the native .NET runtime (i.e. a Spanish .NETfx will yield Spanish language names, English .NETfx with English language names etc.) Thanks to Andrea Ramacciotti for the suggestion.

  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008 The Eraser Project
4 * Original Author: Kasra Nassiri <cjax@users.sourceforge.net>
5 * Modified By: Joel Low <lowjoel@users.sourceforge.net>
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Globalization;
26using System.Reflection;
27using System.IO;
28
29namespace Eraser.Manager
30{
31    /// <summary>
32    /// Basic language class holding the language-related subset of the CultureInfo class
33    /// </summary>
34    public class Language
35    {
36        /// <summary>
37        /// Constructor.
38        /// </summary>
39        /// <param name="info">The culture information structure to retrieve language
40        /// information from.</param>
41        public Language(CultureInfo info)
42        {
43            culture = info;
44        }
45
46        public override string ToString()
47        {
48            return culture.DisplayName;
49        }
50
51        /// <summary>
52        /// Gets the culture name in the format "&lt;languagecode2&gt;-&lt;country/regioncode2&gt;".
53        ///
54        /// The culture name in the format "&lt;languagecode2&gt;-&lt;country/regioncode2&gt;", where
55        /// &lt;languagecode2&gt; is a lowercase two-letter code derived from ISO 639-1 and
56        /// &lt;country/regioncode2&gt; is an uppercase two-letter code derived from ISO 3166.
57        /// </summary>
58        public string Name
59        {
60            get { return culture.Name; }
61        }
62
63        /// <summary>
64        /// Gets the culture name in the format "&lt;languagefull&gt; (&lt;country/regionfull&gt;)"
65        /// in the language of the localized version of .NET Framework.
66        ///
67        /// The culture name in the format "&lt;languagefull&gt; (&lt;country/regionfull&gt;)" in
68        /// the language of the localized version of .NET Framework, where &lt;languagefull&gt;
69        /// is the full name of the language and &lt;country/regionfull&gt; is the full name
70        /// of the country/region.
71        /// </summary>
72        public string DisplayName
73        {
74            get { return culture.DisplayName; }
75        }
76
77        /// <summary>
78        /// Gets the culture name in the format "&lt;languagefull&gt; (&lt;country/regionfull&gt;)"
79        /// in English.
80        ///
81        /// The culture name in the format "&lt;languagefull&gt; (&lt;country/regionfull&gt;)" in
82        /// English, where &lt;languagefull&gt; is the full name of the language and
83        /// &lt;country/regionfull&gt; is the full name of the country/region.
84        /// </summary>
85        public string EnglishName
86        {
87            get { return culture.EnglishName; }
88        }
89
90        /// <summary>
91        /// Converts the current Language object into a .NET CultureInfo object.
92        /// </summary>
93        /// <param name="lang">The language object being converted.</param>
94        /// <returns>A CultureInfo object, containing the data for the given language.</returns>
95        public static explicit operator CultureInfo(Language lang)
96        {
97            return lang.culture;
98        }
99       
100        public override int GetHashCode()
101        {
102            return culture.GetHashCode();
103        }
104
105        CultureInfo culture;
106    }
107
108    /// <summary>
109    /// A class managing all plugins dealing with languages.
110    /// </summary>
111    public static class LanguageManager
112    {
113        /// <summary>
114        /// Retrieves all present language plugins
115        /// </summary>
116        /// <returns>A list, with an instance of each Language class</returns>
117        public static IList<Language> Items
118        {
119            get
120            {
121                List<Language> result = new List<Language>();
122                foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.AllCultures))
123                {
124                    if (string.IsNullOrEmpty(info.Name))
125                        continue;
126                    else if (new DirectoryInfo(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) +
127                        Path.DirectorySeparatorChar + info.Name).Exists)
128                        result.Add(new Language(info));
129                }
130
131                //Last resort
132                if (result.Count == 0)
133                    result.Add(new Language(CultureInfo.GetCultureInfo("EN")));
134                return result;
135            }
136        }
137    }
138}
Note: See TracBrowser for help on using the repository browser.