-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLocalization.cs
More file actions
113 lines (93 loc) · 3.17 KB
/
Copy pathLocalization.cs
File metadata and controls
113 lines (93 loc) · 3.17 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Text;
using MHArmory.Core.DataStructures;
namespace MHArmory.Core
{
public static class Localization
{
public static readonly Dictionary<string, string> AvailableLanguageCodes = new Dictionary<string, string>
{
["EN"] = "English",
//["FR"] = "Français",
//["JP"] = "日本語",
//["IT"] = "Italiano",
//["DE"] = "Deutsch",
//["KR"] = "한국어",
//["CN"] = "中文繁體",
};
public const string DefaultLanguage = "EN";
public static event EventHandler LanguageChanged;
private class EventTarget
{
public WeakReference Reference;
public Action<object> OnEvent;
}
private static readonly List<EventTarget> listeners = new List<EventTarget>();
public static void RegisterLanguageChanged(object reference, Action<object> onEvent)
{
listeners.Add(new EventTarget
{
Reference = new WeakReference(reference),
OnEvent = onEvent
});
}
private static string language;
public static string Language
{
get
{
return language;
}
set
{
if (language != value)
{
language = value;
LanguageChanged?.Invoke(null, EventArgs.Empty);
RaiseWeakEvent();
}
}
}
private static void RaiseWeakEvent()
{
foreach (EventTarget eventTarget in listeners)
{
object reference = eventTarget.Reference.Target;
if (reference != null)
eventTarget.OnEvent(reference);
else
eventTarget.OnEvent = null;
}
listeners.RemoveAll(x => x.OnEvent == null);
}
public static string Get(ILocalizedItem localizations)
{
return Get(localizations.Values);
}
public static string Get(Dictionary<string, string> localizations)
{
if (localizations == null)
return null;
// Fallback to default language if language is not provided.
if (localizations.TryGetValue(Language ?? DefaultLanguage, out string value))
return value;
// Fallback to default language if nothing found with provided language
if (localizations.TryGetValue(DefaultLanguage, out value))
return value;
return null;
}
public static string GetDefault(ILocalizedItem localizations)
{
return GetDefault(localizations.Values);
}
public static string GetDefault(Dictionary<string, string> localizations)
{
if (localizations == null)
return null;
// Fallback to default language if language is not provided.
localizations.TryGetValue(DefaultLanguage, out string value);
return value;
}
}
}