forked from xupefei/Locale-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI18n.cs
66 lines (53 loc) · 2 KB
/
I18n.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
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
namespace LEContextMenuHandler
{
internal class I18n
{
internal static readonly CultureInfo CurrentCultureInfo = CultureInfo.CurrentUICulture;
//internal static readonly CultureInfo CurrentCultureInfo = CultureInfo.GetCultureInfo("zh-CN");
private static XDocument cacheDictionary;
internal static string GetString(string key)
{
var dict = LoadDictionary();
try
{
var strings = from i in dict.Descendants("Strings").Elements() select i;
var str = (from s in strings where s.Name == key select s.Value).FirstOrDefault();
if (string.IsNullOrEmpty(str))
return key;
return str;
}
catch
{
return key;
}
}
private static XDocument LoadDictionary()
{
if (cacheDictionary != null)
return cacheDictionary;
XDocument dictionary = null;
try
{
var langDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Lang\");
var firstLangPath = Path.Combine(langDir,CurrentCultureInfo.Name + ".xml");
var fallbackLangPath = Path.Combine(langDir,
$@"{CurrentCultureInfo.TwoLetterISOLanguageName}.xml");
dictionary = XDocument.Load(File.Exists(firstLangPath) ? firstLangPath : fallbackLangPath);
}
catch
{
}
//If dictionary is still null, use default language.
if (dictionary == null)
dictionary = XDocument.Load(new XmlTextReader(new StringReader(Resource.DefaultLanguage)));
cacheDictionary = dictionary;
return cacheDictionary;
}
}
}