Skip to content

Commit

Permalink
Change plural for zero
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed Feb 17, 2022
1 parent 8485c9d commit 48962bc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
<LangVersion>latest</LangVersion>
<Authors>lepo.co</Authors>
<Company>lepo.co</Company>
Expand Down
3 changes: 1 addition & 2 deletions Lepo.i18n.Demo/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ protected override void OnStartup(StartupEventArgs e)
new Dictionary<string, string>
{
{"en_US", langPath + "en_US.yml"},
{"pl_PL", langPath + "pl_PL.yml"},
{"de_DE", langPath + "de_DE.yml"},
{"pl_PL", langPath + "pl_PL.yml"}
}
);

Expand Down
6 changes: 4 additions & 2 deletions Lepo.i18n.Demo/Views/Main.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

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

case "de_DE":
Expand Down
35 changes: 10 additions & 25 deletions Lepo.i18n/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace Lepo.i18n
/// </summary>
public static class Translator
{
/// <summary>
/// Pattern used when looking for replacement keys in the translated string.
/// </summary>
private static readonly string PreparePattern = /* language=regex */ "[%]+[s|i|f|d]";

/// <summary>
/// Returns the currently used language.
/// </summary>
Expand Down Expand Up @@ -166,7 +171,6 @@ public static string String(string textOrKey)
return "i18n.error.nullInput";
}


return Translator.FirstOrDefault(textOrKey);
}

Expand All @@ -191,11 +195,10 @@ public static string Prepare(string textOrKey, params object[] parameters)

var parameterIndex = 0;
var indexOffset = 0;
var pattern = /* language=regex */ "[%]+[s|i|f|d]";

// TODO: It's experimental

foreach (Match match in Regex.Matches(translatedString, pattern))
foreach (Match match in Regex.Matches(translatedString, PreparePattern))
{
if (parameters.Length < parameterIndex + 1)
break;
Expand Down Expand Up @@ -235,7 +238,7 @@ public static string Prepare(string textOrKey, params object[] parameters)
translatedString = translatedString.Remove(match.Index + indexOffset, 2).Insert(match.Index + indexOffset, embeddedString);

// As we edit a string on the fly, we need to take into account that it changes.
indexOffset += CalcAddedOffset(embeddedString);
indexOffset += embeddedString.Length - 2;

// Update parameter index from method attributes
parameterIndex++;
Expand All @@ -260,7 +263,8 @@ public static string Plural(string single, string plural, object number)
return "i18n.error.nullInput";
}

var isPlural = (number.ToString() ?? "0").All(char.IsDigit) && Int32.Parse(number.ToString() ?? "0") > 1;
// TODO: It's bad...
var isPlural = (number.ToString() ?? "0").All(char.IsDigit) && Int32.Parse(number.ToString() ?? "0") != 1;

return isPlural ? Prepare(plural, number) : Prepare(single, number);
}
Expand All @@ -275,7 +279,7 @@ internal static string FirstOrDefault(string key)

key = key.Trim();

uint mappedKey = Yaml.Map(key);
var mappedKey = Yaml.Map(key);

if (!TranslationStorage.TranslationsDictionary.ContainsKey(TranslationStorage.CurrentLanguage))
{
Expand All @@ -298,24 +302,5 @@ internal static string FirstOrDefault(string key)

return TranslationStorage.TranslationsDictionary[TranslationStorage.CurrentLanguage][mappedKey];
}

/// <summary>
/// Adds an offset to the two-letter translation key.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
internal static int CalcAddedOffset(string input)
{
if (input.Length == 0)
return -2;

if (input.Length == 1)
return -1;

if (input.Length == 2)
return 0;

return input.Length - 2;
}
}
}

0 comments on commit 48962bc

Please sign in to comment.