source: branches/eraser6/Manager/Language.cs @ 936

Revision 936, 8.3 KB checked in by lowjoel, 6 years ago (diff)

Replaced the LanguageManager? usage paradigm to be that of a static class since we only have one staic function GetAll?

  • Property svn:keywords set to Id
Line 
1/*
2 * $Id: Plugin.cs 345 2008-04-02 12:58:48Z lowjoel $
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.NativeName;
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 "<languagefull> (<country/regionfull>)"
65        /// in the language of the localized version of .NET Framework.
66        ///
67        /// The culture name in the format "<languagefull> (<country/regionfull>)" in
68        /// the language of the localized version of .NET Framework, where <languagefull>
69        /// is the full name of the language and <country/regionfull> 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 "<languagefull> (<country/regionfull>)"
79        /// in English.
80        ///
81        /// The culture name in the format "<languagefull> (<country/regionfull>)" in
82        /// English, where <languagefull> is the full name of the language and <country/regionfull>
83        /// 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 implicit operator CultureInfo(Language lang)
96        {
97            return lang.culture;
98        }
99
100        CultureInfo culture;
101    }
102
103    /// <summary>
104    /// A class managing all plugins dealing with languages.
105    /// </summary>
106    public static class LanguageManager
107    {
108        /// <summary>
109        /// Retrieves all present language plugins
110        /// </summary>
111        /// <returns>A list, with an instance of each Language class</returns>
112        public static List<Language> GetAll()
113        {
114            List<Language> result = new List<Language>();
115            foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.AllCultures))
116            {
117                if (string.IsNullOrEmpty(info.Name))
118                    continue;
119                else if (new DirectoryInfo(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) +
120                    Path.DirectorySeparatorChar + info.Name).Exists)
121                    result.Add(new Language(info));
122            }
123
124            //Last resort
125            if (result.Count == 0)
126                result.Add(new Language(CultureInfo.GetCultureInfo("EN")));
127            return result;
128        }
129    }
130}
Note: See TracBrowser for help on using the repository browser.