-
Notifications
You must be signed in to change notification settings - Fork 13
/
TranslationHelper.cs
89 lines (74 loc) · 3.05 KB
/
TranslationHelper.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
83
84
85
86
87
88
89
// Copyright © 2017 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Xml.XPath;
namespace QuickLook.Common.Helpers
{
public class TranslationHelper
{
private static readonly CultureInfo CurrentCultureInfo = CultureInfo.CurrentUICulture;
//private static readonly CultureInfo CurrentCultureInfo = CultureInfo.GetCultureInfo("zh-CN");
private static readonly Dictionary<string, XPathNavigator> FileCache = new Dictionary<string, XPathNavigator>();
public static string Get(string id, string file = null, CultureInfo locale = null, string failsafe = null,
string domain = "QuickLook")
{
if (file == null)
{
var subDir = domain == "QuickLook" ? "" : $"QuickLook.Plugin\\{domain}";
file = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), // path of QuickLook.Common.dll
subDir, "Translations.config");
}
if (!File.Exists(file))
return failsafe ?? id;
if (locale == null)
locale = CurrentCultureInfo;
var nav = GetLangFile(file);
// try to get string
var s = GetStringFromXml(nav, id, locale);
if (s != null)
return s;
// try again for parent language
if (locale.Parent.Name != string.Empty)
s = GetStringFromXml(nav, id, locale.Parent);
if (s != null)
return s;
// use fallback language
s = GetStringFromXml(nav, id, CultureInfo.GetCultureInfo("en"));
if (s != null)
return s;
return failsafe ?? id;
}
private static string GetStringFromXml(XPathNavigator nav, string id, CultureInfo locale)
{
var result = nav.SelectSingleNode($@"/Translations/{locale.Name}/{id}");
return result?.Value;
}
private static XPathNavigator GetLangFile(string file)
{
if (FileCache.ContainsKey(file))
return FileCache[file];
var res = new XPathDocument(file).CreateNavigator();
FileCache.Add(file, res);
return res;
}
}
}