Skip to content

Commit

Permalink
Use CurrentCulture as fallback culture.
Browse files Browse the repository at this point in the history
- Update UI-tests.
  • Loading branch information
JohanLarsson committed Jun 21, 2016
1 parent c80d58a commit a9c515e
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Gu.Localization.Tests/TranslatorTests.Culture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public partial class TranslatorTests
public class Culture
{
[Test, Explicit("Must be run separately due to static")]
public void InitializesToCurrentUiCulture()
public void InitializesToCurrentCulture()
{
Assert.AreEqual(Translator.CurrentCulture.ThreeLetterISOLanguageName, CultureInfo.CurrentUICulture.ThreeLetterISOLanguageName);
Assert.AreEqual(Translator.CurrentCulture.ThreeLetterISOLanguageName, CultureInfo.CurrentCulture.ThreeLetterISOLanguageName);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion Gu.Localization/Translation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public string Value
{
if (this.culture == null)
{
this.TryUpdate(Translator.CurrentCulture);
this.TryUpdate(Translator.CurrentCultureOrDefault());
}

return this.value;
Expand Down
7 changes: 4 additions & 3 deletions Gu.Localization/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ public static CultureInfo CurrentCulture

/// <summary>
/// If <see cref="CurrentCulture"/> is not null it is returned.
/// If not <see cref="CultureInfo.CurrentUICulture"/> is returned if there is a translation in <see cref="Cultures"/>.
/// If not <see cref="CultureInfo.CurrentCulture"/> is returned if there is a translation in <see cref="Cultures"/>.
/// </summary>
/// <returns>The effective culture.</returns>
public static CultureInfo CurrentCultureOrDefault()
{
return currentCulture ??
allCultures.FirstOrDefault(c => Culture.NameEquals(c, CultureInfo.CurrentUICulture)) ??
allCultures.FirstOrDefault(c => Culture.TwoLetterIsoLanguageNameEquals(c, CultureInfo.CurrentUICulture));
allCultures.FirstOrDefault(c => Culture.NameEquals(c, CultureInfo.CurrentCulture)) ??
allCultures.FirstOrDefault(c => Culture.TwoLetterIsoLanguageNameEquals(c, CultureInfo.CurrentCulture)) ??
CultureInfo.InvariantCulture;
}

/// <summary>
Expand Down
30 changes: 29 additions & 1 deletion Gu.Wpf.Localization.Demo/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
Expand All @@ -32,11 +36,35 @@

<TextBlock Grid.Row="1"
Grid.Column="0"
Text="TranslatedToAll" />
Text="SwedishAndNeutral" />
<TextBlock Grid.Row="1"
Grid.Column="1"
AutomationProperties.AutomationId="{x:Static local:AutomationIds.SwedishAndNeutralTextBlockId}"
Text="{l:Static p:Resources.SwedishAndNeutral}" />

<TextBlock Grid.Row="2"
Grid.Column="0"
Text="TranslatedToAll Run" />
<TextBlock Grid.Row="2" Grid.Column="1">
<Run Text="{l:Static p:Resources.TranslatedToAll}" />
</TextBlock>

<TextBlock Grid.Row="3"
Grid.Column="0"
Text="TranslatedToAll" />
<TextBlock Grid.Row="3"
Grid.Column="1"
AutomationProperties.AutomationId="{x:Static local:AutomationIds.TranslatedToAllTextBlockId}"
Text="{l:Static p:Resources.TranslatedToAll}" />

<TextBlock Grid.Row="4"
Grid.Column="0"
Text="Enum" />
<TextBlock Grid.Row="4"
Grid.Column="1"
AutomationProperties.AutomationId="{x:Static local:AutomationIds.EnumTranslatedToAllTextBlockId}"
Text="{l:Enum ResourceManager={x:Static p:Resources.ResourceManager},
Member={x:Static local:DummyEnum.TranslatedToAll}}" />
</Grid>
</DataTemplate>
</Application.Resources>
Expand Down
11 changes: 11 additions & 0 deletions Gu.Wpf.Localization.Demo/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
namespace Gu.Wpf.Localization.Demo
{
using System.Globalization;
using System.Threading;
using System.Windows;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
if (e.Args.Length == 1)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(e.Args[0]);
}

base.OnStartup(e);
}
}
}
31 changes: 28 additions & 3 deletions Gu.Wpf.Localization.Demo/LocalUserControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:expressions="clr-namespace:System.Linq.Expressions;assembly=System.Core"
xmlns:l="clr-namespace:Gu.Wpf.Localization;assembly=Gu.Wpf.Localization"
xmlns:local="clr-namespace:Gu.Wpf.Localization.Demo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:p="clr-namespace:Gu.Wpf.Localization.Demo.Properties"
d:DesignHeight="300"
d:DesignWidth="300"
l:DesignTime.Culture="sv"
mc:Ignorable="d">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
Expand All @@ -32,11 +34,34 @@

<TextBlock Grid.Row="1"
Grid.Column="0"
Text="TranslatedToAll" />
Text="SwedishAndNeutral" />
<TextBlock Grid.Row="1"
Grid.Column="1"
AutomationProperties.AutomationId="{x:Static local:AutomationIds.SwedishAndNeutralTextBlockId}"
Text="{l:Static p:Resources.SwedishAndNeutral}" />

<TextBlock Grid.Row="2"
Grid.Column="0"
Text="TranslatedToAll Run" />
<TextBlock Grid.Row="2" Grid.Column="1">
<Run Text="{l:Static p:Resources.TranslatedToAll}" />
</TextBlock>

<TextBlock Grid.Row="3"
Grid.Column="0"
Text="TranslatedToAll" />
<TextBlock Grid.Row="3"
Grid.Column="1"
AutomationProperties.AutomationId="{x:Static local:AutomationIds.TranslatedToAllTextBlockId}"
Text="{l:Static p:Resources.TranslatedToAll}" />

<TextBlock Grid.Row="4"
Grid.Column="0"
Text="Enum" />
<TextBlock Grid.Row="4"
Grid.Column="1"
AutomationProperties.AutomationId="{x:Static local:AutomationIds.EnumTranslatedToAllTextBlockId}"
Text="{l:Enum ResourceManager={x:Static p:Resources.ResourceManager},
Member={x:Static local:DummyEnum.TranslatedToAll}}" />
</Grid>
</UserControl>
4 changes: 1 addition & 3 deletions Gu.Wpf.Localization.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Gu.Wpf.Localization.Demo.Controls;assembly=Gu.Wpf.Localization.Demo.Controls"
xmlns:globalization="clr-namespace:System.Globalization;assembly=mscorlib"
xmlns:l="http://gu.se/Localization"
xmlns:local="clr-namespace:Gu.Wpf.Localization.Demo"
xmlns:localization="clr-namespace:Gu.Localization;assembly=Gu.Localization"
xmlns:p="clr-namespace:Gu.Wpf.Localization.Demo.Properties"
Title="{x:Static local:AutomationIds.MainWindow}"
Width="800"
Height="600"
l:DesignTime.Culture="en"
Loaded="OnLoaded">
<Grid>
<Grid.ColumnDefinitions>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Wpf.Localization.Demo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private void OnLanguagesComboBoxSelectionChanged(object sender, RoutedEventArgs

private void OnLoaded(object sender, RoutedEventArgs e)
{
this.LanguagesComboBox.SelectedItem = Translator.CurrentCulture;
this.LanguagesComboBox.SelectedItem = Translator.CurrentCultureOrDefault();
}
}
}
1 change: 1 addition & 0 deletions Gu.Wpf.Localization.UiTests/StartInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static ProcessStartInfo DemoProject
var assembly = typeof(MainWindow).Assembly;
var processStartInfo = new ProcessStartInfo
{
Arguments = "en",
FileName = new Uri(assembly.CodeBase, UriKind.Absolute).LocalPath,
UseShellExecute = false,
RedirectStandardOutput = true,
Expand Down
27 changes: 27 additions & 0 deletions Gu.Wpf.Localization.UiTests/Translates.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Gu.Wpf.Localization.UiTests
{
using System.Diagnostics;
using System.Globalization;

using Gu.Wpf.Localization.Demo;

Expand Down Expand Up @@ -36,6 +37,10 @@ public void OneTimeTearDown()
public void VanillaXaml()
{
var groupBox = this.window.Get<GroupBox>(AutomationIds.VanillaXamlGroupId);
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Assert.AreEqual("_SwedishOnly_", groupBox.Get<Label>(AutomationIds.SwedishOnlyTextBlockId).Text);
Assert.AreEqual("_So neutral_", groupBox.Get<Label>(AutomationIds.SwedishAndNeutralTextBlockId).Text);
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.EnumTranslatedToAllTextBlockId).Text);

this.languageComboBox.Select("sv");
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Expand All @@ -54,6 +59,10 @@ public void VanillaXaml()
public void VanillaXamlWithErrorHandling()
{
var groupBox = this.window.Get<GroupBox>(AutomationIds.VanillaXamlGroupWithErrorHandlingId);
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Assert.AreEqual("", groupBox.Get<Label>(AutomationIds.SwedishOnlyTextBlockId).Text);
Assert.AreEqual("So neutral", groupBox.Get<Label>(AutomationIds.SwedishAndNeutralTextBlockId).Text);
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.EnumTranslatedToAllTextBlockId).Text);

this.languageComboBox.Select("sv");
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Expand Down Expand Up @@ -86,27 +95,45 @@ public void DataTemplate()
{
var groupBox = this.window.Get<GroupBox>(AutomationIds.DataTemplateGroupId);

Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Assert.AreEqual("_SwedishOnly_", groupBox.Get<Label>(AutomationIds.SwedishOnlyTextBlockId).Text);
Assert.AreEqual("_So neutral_", groupBox.Get<Label>(AutomationIds.SwedishAndNeutralTextBlockId).Text);
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.EnumTranslatedToAllTextBlockId).Text);

this.languageComboBox.Select("sv");
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.SwedishOnlyTextBlockId).Text);
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.SwedishAndNeutralTextBlockId).Text);
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.EnumTranslatedToAllTextBlockId).Text);

this.languageComboBox.Select("en");
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Assert.AreEqual("_SwedishOnly_", groupBox.Get<Label>(AutomationIds.SwedishOnlyTextBlockId).Text);
Assert.AreEqual("_So neutral_", groupBox.Get<Label>(AutomationIds.SwedishAndNeutralTextBlockId).Text);
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.EnumTranslatedToAllTextBlockId).Text);
}

[Test]
public void UserControlSameProject()
{
var groupBox = this.window.Get<GroupBox>(AutomationIds.UserControlSameProjectGroupId);

Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Assert.AreEqual("_SwedishOnly_", groupBox.Get<Label>(AutomationIds.SwedishOnlyTextBlockId).Text);
Assert.AreEqual("_So neutral_", groupBox.Get<Label>(AutomationIds.SwedishAndNeutralTextBlockId).Text);
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.EnumTranslatedToAllTextBlockId).Text);

this.languageComboBox.Select("sv");
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.SwedishOnlyTextBlockId).Text);
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.SwedishAndNeutralTextBlockId).Text);
Assert.AreEqual("Svenska", groupBox.Get<Label>(AutomationIds.EnumTranslatedToAllTextBlockId).Text);

this.languageComboBox.Select("en");
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.TranslatedToAllTextBlockId).Text);
Assert.AreEqual("_SwedishOnly_", groupBox.Get<Label>(AutomationIds.SwedishOnlyTextBlockId).Text);
Assert.AreEqual("_So neutral_", groupBox.Get<Label>(AutomationIds.SwedishAndNeutralTextBlockId).Text);
Assert.AreEqual("English", groupBox.Get<Label>(AutomationIds.EnumTranslatedToAllTextBlockId).Text);
}

[Test]
Expand Down

0 comments on commit a9c515e

Please sign in to comment.