@@ -15,11 +15,11 @@ public class TranslationProvider
15
15
{
16
16
public List < string > AvailableLanguageIDs = new ( ) ;
17
17
public List < string > AvailableLanguages = new ( ) ;
18
- public bool IsDefault = true ;
19
18
20
19
private readonly string _tempDir = Paths . GetTempDirectory ( ) ;
21
20
private readonly string _translationsDir = Paths . GetTranslationsDirectory ( ) ;
22
21
private static readonly Dictionary < string , string > _langDictionary = new ( StringComparer . OrdinalIgnoreCase ) ;
22
+ private static readonly Dictionary < string , string > _defaultDictionary = new ( StringComparer . OrdinalIgnoreCase ) ;
23
23
private Release _latestVersion ;
24
24
25
25
public TranslationProvider ( )
@@ -45,7 +45,18 @@ public TranslationProvider()
45
45
/// <returns></returns>
46
46
public static string Translate ( string phrase )
47
47
{
48
- return _langDictionary . ContainsKey ( phrase ) ? _langDictionary [ phrase ] : "<empty>" ;
48
+ if ( _langDictionary . ContainsKey ( phrase ) )
49
+ {
50
+ return _langDictionary [ phrase ] ;
51
+ }
52
+ else if ( _defaultDictionary . ContainsKey ( phrase ) )
53
+ {
54
+ return _defaultDictionary [ phrase ] ;
55
+ }
56
+ else
57
+ {
58
+ return $ "<{ phrase } >";
59
+ }
49
60
}
50
61
51
62
/// <summary>
@@ -55,14 +66,14 @@ public static string Translate(string phrase)
55
66
/// <param name="initial">Whether this was the startup initial method call</param>
56
67
public void LoadLanguage ( string lang , bool initial = false )
57
68
{
58
- // This is probably the first boot ever
69
+ _langDictionary . Clear ( ) ;
59
70
if ( lang == string . Empty )
60
71
{
72
+ // This is probably the first boot ever
61
73
lang = Constants . DefaultLanguageID ;
62
74
Program . OptionsObject . Language = lang ;
63
75
}
64
76
lang = lang . Trim ( ) . ToLowerInvariant ( ) ;
65
- IsDefault = ( string . IsNullOrEmpty ( lang ) || lang . ToLowerInvariant ( ) == Constants . DefaultLanguageID ) && initial ;
66
77
var doc = new XmlDocument ( ) ;
67
78
68
79
try
@@ -73,13 +84,10 @@ public void LoadLanguage(string lang, bool initial = false)
73
84
doc . Load ( Path . Combine ( _translationsDir , Constants . DefaultTranslationsFile ) ) ;
74
85
foreach ( XmlNode node in doc . ChildNodes [ 0 ] . ChildNodes )
75
86
{
76
- _langDictionary . Add ( node . Name , node . InnerText ) ;
77
- }
78
-
79
- // Return if the attempted language to load is the default one
80
- if ( lang == Constants . DefaultLanguageID )
81
- {
82
- return ;
87
+ if ( node . NodeType != XmlNodeType . Comment )
88
+ {
89
+ _defaultDictionary . Add ( node . Name , node . InnerText ) ;
90
+ }
83
91
}
84
92
}
85
93
@@ -97,7 +105,10 @@ public void LoadLanguage(string lang, bool initial = false)
97
105
// Replace existing keys with the ones available in this file
98
106
foreach ( XmlNode node in doc . ChildNodes [ 0 ] . ChildNodes )
99
107
{
100
- _langDictionary [ node . Name ] = node . InnerText ;
108
+ if ( node . NodeType != XmlNodeType . Comment )
109
+ {
110
+ _langDictionary . Add ( node . Name , node . InnerText ) ;
111
+ }
101
112
}
102
113
}
103
114
}
@@ -112,10 +123,10 @@ public void LoadLanguage(string lang, bool initial = false)
112
123
/// </summary>
113
124
public void ParseTranslationFiles ( )
114
125
{
115
- try
126
+ var filesDir = Directory . GetFiles ( _translationsDir ) . Where ( x => x . EndsWith ( ".xml" ) ) ;
127
+ foreach ( var file in filesDir )
116
128
{
117
- var filesDir = Directory . GetFiles ( _translationsDir ) . Where ( x => x . EndsWith ( ".xml" ) ) ;
118
- foreach ( var file in filesDir )
129
+ try
119
130
{
120
131
// Create wrapper
121
132
var fInfo = new FileInfo ( file ) ;
@@ -135,11 +146,12 @@ public void ParseTranslationFiles()
135
146
AvailableLanguages . Add ( langName ) ;
136
147
AvailableLanguageIDs . Add ( langID ) ;
137
148
}
138
- }
139
- catch ( Exception ex )
140
- {
141
- MessageBox . Show ( $ "There was a problem while parsing the translation files.\n " +
142
- $ "Details: { ex . Message } ") ;
149
+ catch ( Exception ex )
150
+ {
151
+ MessageBox . Show ( $ "There was a problem while parsing the translation file '{ file } '.\n " +
152
+ $ "Details: { ex . Message } ") ;
153
+ continue ;
154
+ }
143
155
}
144
156
}
145
157
0 commit comments