Skip to content

Commit

Permalink
Add support for plurals and preparing
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed Feb 17, 2022
1 parent 166f3c0 commit 8485c9d
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 53 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.1.0</Version>
<Version>1.2.0</Version>
<LangVersion>latest</LangVersion>
<Authors>lepo.co</Authors>
<Company>lepo.co</Company>
Expand Down
6 changes: 3 additions & 3 deletions Lepo.i18n.Demo/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ protected override void OnStartup(StartupEventArgs e)
Assembly.GetExecutingAssembly(),
new Dictionary<string, string>
{
{"en_US", langPath + "en_US.yaml"},
{"pl_PL", langPath + "pl_PL.yaml"},
{"de_DE", langPath + "de_DE.yaml"},
{"en_US", langPath + "en_US.yml"},
{"pl_PL", langPath + "pl_PL.yml"},
{"de_DE", langPath + "de_DE.yml"},
}
);

Expand Down
12 changes: 6 additions & 6 deletions Lepo.i18n.Demo/Lepo.i18n.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<None Remove="Strings\de_DE.yaml" />
<None Remove="Strings\en_US.yaml" />
<None Remove="Strings\pl_PL.yaml" />
<None Remove="Strings\de_DE.yml" />
<None Remove="Strings\en_US.yml" />
<None Remove="Strings\pl_PL.yml" />
</ItemGroup>

<ItemGroup>
Expand All @@ -27,9 +27,9 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Strings\de_DE.yaml" />
<EmbeddedResource Include="Strings\en_US.yaml" />
<EmbeddedResource Include="Strings\pl_PL.yaml" />
<EmbeddedResource Include="Strings\de_DE.yml" />
<EmbeddedResource Include="Strings\en_US.yml" />
<EmbeddedResource Include="Strings\pl_PL.yml" />
</ItemGroup>

</Project>
9 changes: 0 additions & 9 deletions Lepo.i18n.Demo/Strings/de_DE.yaml

This file was deleted.

21 changes: 21 additions & 0 deletions Lepo.i18n.Demo/Strings/de_DE.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Comment
main.languages: Sprachen
main.globalization: Die Globalisierung schreitet voran, aber manche Menschen mögen Englisch immer noch nicht.
main.changeLanguage: Sprache ändern
main.hello: Hallo Welt
main.preview: Nur eine Vorschau.
main.myCode: Mein Code hat nie Fehler ...
main.randomFeatures: Es entwickelt nur zufällige unerwartete Funktionen.
main.reddit: Und ja, ich habe den obigen Witz von Reddit geklaut...
main.checkWpf: Gefällt Ihnen der Stil dieser App? Überprüfen Sie die WPF-UI.

main.forConverter: Mein Freund hat %s Katzen.

# The value may or may not be in parentheses
house.single: '%i Haus'
house.plural: '%i Häuser'

main.empty:

home.multiplePrepare: 'Diesen Monat habe ich %s aus dem Tierheim genommen. Ich gab auch %ikg Tierfutter und € %d.'
home.multiplePrepare.inside: Hund
1 change: 0 additions & 1 deletion Lepo.i18n.Demo/Strings/en_US.yaml

This file was deleted.

21 changes: 21 additions & 0 deletions Lepo.i18n.Demo/Strings/en_US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Comment
main.languages: Languages
main.globalization: Globalization is progressing, but some people still don't enjoy English.
main.changeLanguage: Change language
main.hello: Hello World
main.preview: Just a preview.
main.myCode: My code never has bugs...
main.randomFeatures: It just develop random unexpected features.
main.reddit: And yeah, I stole above joke from Reddit...
main.checkWpf: Do you like the style of this app? Check WPF UI.

main.forConverter: My friend has %s cats.

# The value may or may not be in parentheses
house.single: '%i house'
house.plural: '%i houses'

main.empty:

home.multiplePrepare: 'This month I took %s from the shelter. I also gave %ikg of pet food and $%d.'
home.multiplePrepare.inside: dog
9 changes: 0 additions & 9 deletions Lepo.i18n.Demo/Strings/pl_PL.yaml

This file was deleted.

21 changes: 21 additions & 0 deletions Lepo.i18n.Demo/Strings/pl_PL.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Comment
main.languages: Języki
main.globalization: Globalizacja postępuje, ale nie wszyscy lubią angielski.
main.changeLanguage: Zmień język
main.hello: Witaj świecie
main.preview: Tak o, dla kurażu.
main.myCode: Mój kod nie ma błędów...
main.randomFeatures: Po prostu samoistnie wytwarza losowe funkcjonalności.
main.reddit: I tak, skradłem ten mem z Reddita...
main.checkWpf: Podoba Ci się stylistyka aplikacji? Sprawdź WPF UI.

main.forConverter: Moja przyjaciółka ma %s kotów.

# The value may or may not be in parentheses
house.single: '%i dom'
house.plural: '%i domy'

main.empty:

home.multiplePrepare: 'W tym miesiącu przygarnąłem %s ze schroniska. Przekazałem też %ikg karmy, oraz %d zł.'
home.multiplePrepare.inside: psa
35 changes: 26 additions & 9 deletions Lepo.i18n.Demo/Views/Main.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,33 @@
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Page.Resources>
<i18n:TranslateConverter x:Key="TranslateConverter" />
</Page.Resources>

<Grid>
<StackPanel>
<wpfui:CardControl
Title="{i18n:Translate String='Languages'}"
Title="{i18n:Translate String='main.languages'}"
Margin="0,0,0,8"
Icon="LocalLanguage24"
Subtitle="{i18n:Translate String='Globalization is progressing, but some people still don\'t enjoy English.'}">
Subtitle="{i18n:Translate String='main.globalization'}">
<wpfui:Button
Appearance="Secondary"
Click="ButtonBase_OnClick"
Content="{i18n:Translate String='Change language'}" />
Content="{i18n:Translate String='main.changeLanguage'}" />
</wpfui:CardControl>

<wpfui:CardAction Margin="0,0,0,8" Icon="Globe24">
<StackPanel>
<TextBlock
Margin="0,0,0,4"
FontWeight="Medium"
Text="{i18n:Translate String='Hello World'}" />
Text="{i18n:Translate String='main.hello'}" />
<TextBlock
FontSize="10"
Foreground="{DynamicResource TextFillColorTertiaryBrush}"
Text="{i18n:Translate String='Just a preview.'}" />
Text="{i18n:Translate String='main.preview'}" />
</StackPanel>
</wpfui:CardAction>

Expand All @@ -43,22 +46,36 @@
<TextBlock
Margin="0,0,0,4"
FontWeight="Medium"
Text="{i18n:Translate String='My code never has bugs...'}" />
Text="{i18n:Translate String='main.myCode'}" />
<TextBlock
FontSize="10"
Foreground="{DynamicResource TextFillColorTertiaryBrush}"
Text="{i18n:Translate String='It just develop random unexpected features.'}" />
Text="{i18n:Translate String='main.randomFeatures'}" />
</StackPanel>
</wpfui:CardAction>

<TextBlock
Margin="0,0,0,8"
Foreground="{DynamicResource TextFillColorSecondaryBrush}"
Text="{i18n:Translate String='And yeah, I stole above joke from Reddit...'}" />
Text="{i18n:Translate String='main.reddit'}" />

<StackPanel Margin="0,0,0,8" Orientation="Horizontal">
<wpfui:Badge Appearance="Success" Content="{i18n:Translate String='house.single', Plural='house.plural', Number=2}" />

<wpfui:Badge
Margin="4,0,0,0"
Appearance="Danger"
Content="{Binding TranslatedTextPreparedInBackend, Mode=OneWay}" />

<wpfui:Badge
Margin="4,0,0,0"
Appearance="Primary"
Content="{Binding CatsCount, ConverterParameter='main.forConverter', Converter={StaticResource TranslateConverter}}" />
</StackPanel>

<wpfui:Hyperlink
Margin="0,0,0,4"
Content="{i18n:Translate String='Do you like the style of this app? Check WPF UI.'}"
Content="{i18n:Translate String='main.checkWpf'}"
Icon="Link48"
NavigateUri="https://github.com/lepoco/wpfui" />
</StackPanel>
Expand Down
17 changes: 16 additions & 1 deletion Lepo.i18n.Demo/Views/Main.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ namespace Lepo.i18n.Demo.Views
/// </summary>
public partial class Main : Page
{
public string TranslatedTextPreparedInBackend { get; set; }

public int CatsCount { get; set; }

public Main()
{
// We need to use a page if we want to reload it.

InitializeComponent();

CatsCount = 2;

TranslatedTextPreparedInBackend = Translator.Prepare(
"home.multiplePrepare",
Translator.String("home.multiplePrepare.inside"),
88,
512.55d
);

DataContext = this;
}

private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -41,4 +56,4 @@ private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
(Application.Current.MainWindow as MainWindow)?.LoadFrame();
}
}
}
}
8 changes: 7 additions & 1 deletion Lepo.i18n/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ internal class AssemblyLoader
/// <summary>
/// Try to get dictionary from YAML file located in <see cref="System.Windows.Application"/> resources.
/// </summary>
/// <param name="resourceStreamPath"></param>
/// <param name="applicationAssembly">Assembly of the main application.</param>
/// <param name="resourceStreamPath">Path to the application resource.</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IDictionary<uint, string> TryLoad(Assembly applicationAssembly, string resourceStreamPath)
Expand All @@ -31,7 +32,12 @@ public static IDictionary<uint, string> TryLoad(Assembly applicationAssembly, st
using Stream stream = applicationAssembly.GetManifestResourceStream(resourceStreamPath);

if (stream == null)
{
System.Diagnostics.Debug.WriteLine($"ERROR | Could not load path {resourceStreamPath}, the file probably does not exist.", "Lepo.i18n");

return null;
}


using StreamReader reader = new StreamReader(stream);

Expand Down
8 changes: 5 additions & 3 deletions Lepo.i18n/TranslateConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public sealed class TranslateConverter : IValueConverter
/// <summary>
/// Translates text strings using the Converter in XAML
/// </summary>
/// <param name="value">ID of the translated text</param>
/// <param name="targetType">The format that will be returned, usually a string</param>
/// <param name="value">Binded value to be replaced via <see cref="Translator.Prepare"/></param>
/// <param name="targetType">Target type.</param>
/// <param name="parameter">Word or key of the translated string.</param>
/// <param name="culture">Culture information.</param>
/// <returns>Translated text string based on the identifier, or only the identifier if the id does not exist</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Translator.String((string)parameter);
return value == null ? Translator.String((string)parameter ?? "") : Translator.Prepare((string)parameter ?? "", value);
}

/// <summary>
Expand Down
15 changes: 14 additions & 1 deletion Lepo.i18n/TranslateExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ namespace Lepo.i18n
public class TranslateExtension : MarkupExtension
{
/// <summary>
/// Word to be translated.
/// Word or key to be translated.
/// </summary>
public string String { get; set; }

/// <summary>
/// If the given <see cref="Number"/> is less than one the <see cref="String"/> will be used, if greater than one - the <see cref="Plural"/> version will be used.
/// </summary>
public string Plural { get; set; }

/// <summary>
/// The number that is used to determine whether we are using <see cref="Plural"/> or <see cref="String"/>. Can also be used for <see cref="Translator.Prepare"/>.
/// </summary>
public object Number { get; set; }

/// <summary>
/// Overridden method, returns the source and path to bind to
/// </summary>
Expand All @@ -27,6 +37,9 @@ public override object ProvideValue(System.IServiceProvider serviceProvider)
if (System.String.IsNullOrEmpty(String))
return System.String.Empty;

if (!System.String.IsNullOrEmpty(String) && !System.String.IsNullOrEmpty(Plural) && Number != null)
return Translator.Plural(FixSpecialCharacters(String), FixSpecialCharacters(Plural), Number);

return Translator.String(FixSpecialCharacters(String));
}

Expand Down
Loading

0 comments on commit 8485c9d

Please sign in to comment.