-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguage.cs
82 lines (72 loc) · 2.9 KB
/
Language.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using RConControl.Properties;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Text;
namespace RConControl {
public class Language {
// Singleton
private static Language instance;
public static Language Instance {
get {
if (instance == null) {
instance = new Language();
}
return instance;
}
}
//*************************************************
// Variables
//*************************************************
private static ResourceManager res_man;
private static CultureInfo cul;
public delegate void VoidHandler();
public event VoidHandler SwitchLangEvent;
//*************************************************
// CTor
//*************************************************
private Language() {
res_man = new ResourceManager("RConControl.Resources.Language.lang", Assembly.GetExecutingAssembly());
InitLang();
}
//*************************************************
// Methods
//*************************************************
public List<CultureInfo> AvailableLanguages() {
List<CultureInfo> resultList = new List<CultureInfo>();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
foreach (CultureInfo culture in cultures) {
ResourceSet rs = res_man.GetResourceSet(culture, true, false);
if (rs != null) resultList.Add(culture);
}
return resultList;
}
public string GetString(string key) {
string result = "";
if (!String.IsNullOrEmpty(key)) {
try {
result = res_man.GetString(key, cul);
} catch (Exception ex) {
ex.Data.Add("key", key);
ErrorLogger.Log(ex);
}
}
return result;
}
// Private helper methods
private void InitLang() {
if (String.IsNullOrEmpty(Settings.Default.Language)) {
CultureInfo systemCulture = (CultureInfo.CurrentUICulture.IsNeutralCulture ? CultureInfo.CurrentUICulture : CultureInfo.CurrentUICulture.Parent);
Settings.Default.Language = (AvailableLanguages().Contains(systemCulture) ? systemCulture.TwoLetterISOLanguageName : new CultureInfo("en").TwoLetterISOLanguageName);
Settings.Default.Save();
}
cul = Tools.GetCultureByTwoLetterISO(Settings.Default.Language);
}
public void SwitchLang() {
InitLang();
SwitchLangEvent();
}
}
}