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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Serialization;
namespace MediaBrowser.Controller.Localization
{
/// <summary>
/// Class LocalizedStrings
/// </summary>
public class LocalizedStrings
{
/// <summary>
/// The logger
/// </summary>
static internal ILogger Logger { get; set; }
/// <summary>
/// The base prefix
/// </summary>
public const string BasePrefix = "base-";
/// <summary>
/// The local strings
/// </summary>
protected ConcurrentDictionary<string, string> LocalStrings = new ConcurrentDictionary<string, string>();
/// <summary>
/// The _instance
/// </summary>
private static LocalizedStrings _instance;
/// <summary>
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
public static LocalizedStrings Instance { get { return _instance ?? (_instance = new LocalizedStrings()); } }
/// <summary>
/// Initializes a new instance of the <see cref="LocalizedStrings" /> class.
/// </summary>
public LocalizedStrings()
{
foreach (var stringObject in Kernel.Instance.StringFiles)
{
AddStringData(LoadFromFile(GetFileName(stringObject),stringObject.GetType()));
}
}
/// <summary>
/// Gets the name of the file.
/// </summary>
/// <param name="stringObject">The string object.</param>
/// <returns>System.String.</returns>
protected string GetFileName(LocalizedStringData stringObject)
{
var path = Kernel.Instance.ApplicationPaths.LocalizationPath;
var name = Path.Combine(path, stringObject.Prefix + "strings-" + CultureInfo.CurrentCulture + ".xml");
if (File.Exists(name))
{
return name;
}
name = Path.Combine(path, stringObject.Prefix + "strings-" + CultureInfo.CurrentCulture.Parent + ".xml");
if (File.Exists(name))
{
return name;
}
//just return default
return Path.Combine(path, stringObject.Prefix + "strings-en.xml");
}
/// <summary>
/// Loads from file.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="t">The t.</param>
/// <returns>LocalizedStringData.</returns>
protected LocalizedStringData LoadFromFile(string file, Type t)
{
var xs = new XmlSerializer(t);
var strings = (LocalizedStringData)Activator.CreateInstance(t);
strings.FileName = file;
Logger.Info("Using String Data from {0}", file);
if (File.Exists(file))
{
using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
strings = (BaseStrings)xs.Deserialize(fs);
}
}
else
{
strings.Save(); //brand new - save it
}
if (strings.ThisVersion != strings.Version && file.ToLower().Contains("-en.xml"))
{
//only re-save the english version as that is the one defined internally
strings = new BaseStrings {FileName = file};
//strings.Save();
}
return strings;
}
/// <summary>
/// Adds the string data.
/// </summary>
/// <param name="stringData">The string data.</param>
public void AddStringData(object stringData )
{
//translate our object definition into a dictionary for lookups
// and a reverse dictionary so we can lookup keys by value
foreach (var field in stringData.GetType().GetFields().Where(f => f != null && f.FieldType == typeof(string)))
{
string value;
try
{
value = field.GetValue(stringData) as string;
}
catch (TargetException ex)
{
Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
continue;
}
catch (FieldAccessException ex)
{
Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
continue;
}
catch (NotSupportedException ex)
{
Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
continue;
}
LocalStrings.TryAdd(field.Name, value);
}
}
/// <summary>
/// Gets the string.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>System.String.</returns>
public string GetString(string key)
{
string value;
LocalStrings.TryGetValue(key, out value);
return value;
}
}
}
|