@@ -17,6 +17,11 @@ namespace Lepo.i18n
1717 /// </summary>
1818 public static class Translator
1919 {
20+ /// <summary>
21+ /// Pattern used when looking for replacement keys in the translated string.
22+ /// </summary>
23+ private static readonly string PreparePattern = /* language=regex */ "[%]+[s|i|f|d]" ;
24+
2025 /// <summary>
2126 /// Returns the currently used language.
2227 /// </summary>
@@ -166,7 +171,6 @@ public static string String(string textOrKey)
166171 return "i18n.error.nullInput" ;
167172 }
168173
169-
170174 return Translator . FirstOrDefault ( textOrKey ) ;
171175 }
172176
@@ -191,11 +195,10 @@ public static string Prepare(string textOrKey, params object[] parameters)
191195
192196 var parameterIndex = 0 ;
193197 var indexOffset = 0 ;
194- var pattern = /* language=regex */ "[%]+[s|i|f|d]" ;
195198
196199 // TODO: It's experimental
197200
198- foreach ( Match match in Regex . Matches ( translatedString , pattern ) )
201+ foreach ( Match match in Regex . Matches ( translatedString , PreparePattern ) )
199202 {
200203 if ( parameters . Length < parameterIndex + 1 )
201204 break ;
@@ -235,7 +238,7 @@ public static string Prepare(string textOrKey, params object[] parameters)
235238 translatedString = translatedString . Remove ( match . Index + indexOffset , 2 ) . Insert ( match . Index + indexOffset , embeddedString ) ;
236239
237240 // As we edit a string on the fly, we need to take into account that it changes.
238- indexOffset += CalcAddedOffset ( embeddedString ) ;
241+ indexOffset += embeddedString . Length - 2 ;
239242
240243 // Update parameter index from method attributes
241244 parameterIndex ++ ;
@@ -260,7 +263,8 @@ public static string Plural(string single, string plural, object number)
260263 return "i18n.error.nullInput" ;
261264 }
262265
263- var isPlural = ( number . ToString ( ) ?? "0" ) . All ( char . IsDigit ) && Int32 . Parse ( number . ToString ( ) ?? "0" ) > 1 ;
266+ // TODO: It's bad...
267+ var isPlural = ( number . ToString ( ) ?? "0" ) . All ( char . IsDigit ) && Int32 . Parse ( number . ToString ( ) ?? "0" ) != 1 ;
264268
265269 return isPlural ? Prepare ( plural , number ) : Prepare ( single , number ) ;
266270 }
@@ -275,7 +279,7 @@ internal static string FirstOrDefault(string key)
275279
276280 key = key . Trim ( ) ;
277281
278- uint mappedKey = Yaml . Map ( key ) ;
282+ var mappedKey = Yaml . Map ( key ) ;
279283
280284 if ( ! TranslationStorage . TranslationsDictionary . ContainsKey ( TranslationStorage . CurrentLanguage ) )
281285 {
@@ -298,24 +302,5 @@ internal static string FirstOrDefault(string key)
298302
299303 return TranslationStorage . TranslationsDictionary [ TranslationStorage . CurrentLanguage ] [ mappedKey ] ;
300304 }
301-
302- /// <summary>
303- /// Adds an offset to the two-letter translation key.
304- /// </summary>
305- /// <param name="input"></param>
306- /// <returns></returns>
307- internal static int CalcAddedOffset ( string input )
308- {
309- if ( input . Length == 0 )
310- return - 2 ;
311-
312- if ( input . Length == 1 )
313- return - 1 ;
314-
315- if ( input . Length == 2 )
316- return 0 ;
317-
318- return input . Length - 2 ;
319- }
320305 }
321306}
0 commit comments