Skip to content

Commit 48962bc

Browse files
committed
Change plural for zero
1 parent 8485c9d commit 48962bc

File tree

4 files changed

+16
-30
lines changed

4 files changed

+16
-30
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.2.0</Version>
3+
<Version>1.2.1</Version>
44
<LangVersion>latest</LangVersion>
55
<Authors>lepo.co</Authors>
66
<Company>lepo.co</Company>

Lepo.i18n.Demo/App.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ protected override void OnStartup(StartupEventArgs e)
2525
new Dictionary<string, string>
2626
{
2727
{"en_US", langPath + "en_US.yml"},
28-
{"pl_PL", langPath + "pl_PL.yml"},
29-
{"de_DE", langPath + "de_DE.yml"},
28+
{"pl_PL", langPath + "pl_PL.yml"}
3029
}
3130
);
3231

Lepo.i18n.Demo/Views/Main.xaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Windows;
1+
using System.Reflection;
2+
using System.Windows;
23
using System.Windows.Controls;
34

45
namespace Lepo.i18n.Demo.Views
@@ -37,7 +38,8 @@ private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
3738
switch (Translator.Current)
3839
{
3940
case "pl_PL":
40-
await Translator.SetLanguageAsync("de_DE");
41+
// This language is dynamically loaded after other languages have been initialized on startup.
42+
await Translator.SetLanguageAsync(Assembly.GetExecutingAssembly(), "de_DE", "Lepo.i18n.Demo.Strings.de_DE.yml");
4143
break;
4244

4345
case "de_DE":

Lepo.i18n/Translator.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)