Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions src/Providers/ResxLocalizationProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@ string TryGetNamespace(Type type)
// remove ".resources" from the end
foundResource = foundResource.Substring(0, foundResource.Length - ResourceFileExtension.Length);

if (TryCreateResourceManager(foundResource, assembly, out ResourceManager resourceManager))
{
CacheResourceManager(resManKey, resourceManager);

return resourceManager;
}

// First try the simple retrieval
Type resourceManagerType;
try
Expand Down Expand Up @@ -519,27 +526,46 @@ bool MatchesDictTypeName(Type type)
if (resManager == null)
throw new ArgumentException(string.Format("No resource manager for dictionary '{0}' in assembly '{1}' found! ({1}.{0})", resourceDictionary, resourceAssembly));

// Add the ResourceManager to the cachelist
Add(resManKey, resManager);
CacheResourceManager(resManKey, resManager);
}

try
{
// Look in all cultures and check available ressources.
foreach (var c in SearchCultures)
{
var rs = resManager.GetResourceSet(c, true, false);
if (rs != null)
AddCulture(c);
}
}
catch
// return the found ResourceManager
return resManager;
}

private void CacheResourceManager(string resManKey, ResourceManager resManager)
{
// Add the ResourceManager to the cachelist
Add(resManKey, resManager);

try
{
// Look in all cultures and check available ressources.
foreach (var c in SearchCultures)
{
// ignored
var rs = resManager.GetResourceSet(c, true, false);
if (rs != null)
AddCulture(c);
}
}
catch
{
// ignored
}
}

// return the found ResourceManager
return resManager;
private bool TryCreateResourceManager(string resourceManagerBaseName, Assembly resourceManagerAssembly, out ResourceManager resourceManager)
{
try
{
resourceManager = new ResourceManager(resourceManagerBaseName, resourceManagerAssembly);
}
catch
{
resourceManager = null;
}

return resourceManager != null;
}

private ResourceManager GetResourceManagerFromType(IReflect type)
Expand All @@ -561,9 +587,9 @@ private ResourceManager GetResourceManagerFromType(IReflect type)
return null;
}
}
#endregion
#endregion

#region ILocalizationProvider implementation
#region ILocalizationProvider implementation
/// <summary>
/// Uses the key and target to build a fully qualified resource key (Assembly, Dictionary, Key)
/// </summary>
Expand Down Expand Up @@ -711,6 +737,6 @@ public virtual object GetLocalizedObject(string key, DependencyObject target, Cu
/// An observable list of available cultures.
/// </summary>
public ObservableCollection<CultureInfo> AvailableCultures { get; protected set; }
#endregion
#endregion
}
}
51 changes: 51 additions & 0 deletions tests/HelloWorldWPF.NoDesignFiles.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30011.22
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WPFLocalizeExtension", "..\src\WPFLocalizeExtension.csproj", "{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelloWorldWPF.NoDesignFiles", "HelloWorldWPF.NoDesignFiles\HelloWorldWPF.NoDesignFiles.csproj", "{585EA5B3-C6A5-4444-8890-4D06561B4914}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|x86.ActiveCfg = Debug|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|x86.Build.0 = Debug|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|Any CPU.Build.0 = Release|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|x86.ActiveCfg = Release|Any CPU
{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|x86.Build.0 = Release|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|Any CPU.Build.0 = Debug|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|x86.ActiveCfg = Debug|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|x86.Build.0 = Debug|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|Any CPU.ActiveCfg = Release|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|Any CPU.Build.0 = Release|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|x86.ActiveCfg = Release|Any CPU
{585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F0544A03-17E3-4EFB-BB50-4EDEB145B721}
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions tests/HelloWorldWPF.NoDesignFiles/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Application
x:Class="HalloWeltWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources />
</Application>
16 changes: 16 additions & 0 deletions tests/HelloWorldWPF.NoDesignFiles/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace HalloWeltWPF
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>HalloWeltWPF</RootNamespace>
<OutputType>WinExe</OutputType>
<StartupObject />
<UseWPF>True</UseWPF>
<AutoGenerateBindingRedirects Condition="!$(TargetFramework.StartsWith('netcore'))">true</AutoGenerateBindingRedirects>
<AssemblyName>HelloWorldWPF</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Condition="!$(TargetFramework.StartsWith('net40'))" Include="SmartFormat.NET" Version="2.5.0" />
</ItemGroup>

<!--References for .Net Framwork 4 and 35-->
<ItemGroup Condition="!$(TargetFramework.StartsWith('netcore'))">
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>

<!--References for .Net Core 3.1-->
<ItemGroup Condition="$(TargetFramework.StartsWith('netcore'))">
<PackageReference Include="Microsoft.Windows.Compatibility" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\WPFLocalizeExtension.csproj" />
</ItemGroup>
</Project>
91 changes: 91 additions & 0 deletions tests/HelloWorldWPF.NoDesignFiles/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<Window x:Class="HalloWeltWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:HalloWeltWPF"
d:DataContext="{d:DesignInstance Type=local:TestVM, IsDesignTimeCreatable=True}"
mc:Ignorable="d"
lex:LocalizeDictionary.DesignCulture="en"
lex:LocalizeDictionary.IncludeInvariantCulture="False"
lex:ResxLocalizationProvider.DefaultAssembly="HelloWorldWPF"
lex:ResxLocalizationProvider.DefaultDictionary="Ressourcen"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize"
Title="{lex:LocText Title}"
Background="{lex:Loc Background}"
FlowDirection="{lex:Loc FlowDirection}" WindowStartupLocation="CenterScreen">
<Window.Resources>
<ResourceDictionary>
<Style x:Key="hardCodedStyle1" TargetType="TextBlock">
<Setter Property="Text" Value="{lex:BLoc Key=de}" />
</Style>
<Style x:Key="hardCodedStyle" TargetType="TextBlock">
<Setter Property="Text" >
<Setter.Value>
<MultiBinding Converter="{lex:TranslateConverter}" >
<Binding Path="language" />
<Binding Source="{x:Static lex:LocalizeDictionary.Instance}" Path="Culture"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Foreground">
<Setter.Value>
<MultiBinding Converter="{lex:TranslateConverter}" FallbackValue="#00FF00" >
<Binding Path="color"/>
<Binding Source="{x:Static lex:LocalizeDictionary.Instance}" Path="Culture"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="50,20">
<StackPanel.LayoutTransform>
<ScaleTransform ScaleX="2.0" ScaleY="2.0" />
</StackPanel.LayoutTransform>
<lex:LocProxy x:Name="LocProxySessionLabel" Source="{Binding Path=language, FallbackValue=de}" />
<Label Background="Green" x:Name="SessionLabel" Content="{Binding ElementName=LocProxySessionLabel, Path=Result}" />
<TextBlock Style="{StaticResource hardCodedStyle}" Background="Red" />
<TextBlock Style="{StaticResource hardCodedStyle1}"/>
<TextBlock Text="{Binding ElementName=testFELoc, Path=Content}"></TextBlock>

<TextBlock Name="MyLabel3" FontSize="20" HorizontalAlignment="Center">
<TextBlock.Text>
<MultiBinding Converter="{lex:StringFormatConverter}" >
<lex:BLoc Key="HelloWorldWPF:Ressourcen:MyLabel2"></lex:BLoc>
<Binding Path="Hours" FallbackValue=""/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Name="MyLabel" FontSize="20" Text="{lex:Loc}" HorizontalAlignment="Center" />
<TextBlock Name="BindTest1" Text="{lex:Loc {Binding Path=language, FallbackValue=en}}" Background="Aqua"></TextBlock>
<Button Height="30" Tag="en">
<Button.Template>
<ControlTemplate TargetType="Button">
<Canvas>
<Ellipse Width="100" Height="{TemplateBinding Height}" Fill="DarkCyan"></Ellipse>
<TextBox Text="{lex:Loc {Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}}"></TextBox>
<TextBox Canvas.Left="100" Text="{lex:Loc {TemplateBinding Tag}}"></TextBox>
</Canvas>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock Text="{lex:Loc {Binding tenum, StringFormat=TestEnum_{0}}}"></TextBlock>
<TextBlock Text="{lex:Loc {Binding tenum, Converter={lex:PrependTypeConverter}, ConverterParameter=__}}"></TextBlock>
<Button Name="BindeTestButton" Content="Press to toggle binded property" Margin="5" Click="BindeTestButton_Click" ></Button>
<TextBlock Name="MyLabel2" FontSize="20" Text="{lex:Loc PresentationCore:ExceptionStringTable:DeleteText}" HorizontalAlignment="Center" />
<StackPanel Orientation="Horizontal">
<Button Content="{lex:Loc de}" Margin="5" CommandParameter="de" Command="{Binding Source={x:Static lex:LocalizeDictionary.Instance}, Path=SetCultureCommand}" />
<Button Content="{lex:Loc en}" Margin="5" CommandParameter="en" Command="{Binding Source={x:Static lex:LocalizeDictionary.Instance}, Path=SetCultureCommand}" />
<Button Content="{lex:Loc he}" Margin="5" CommandParameter="he" Command="{Binding Source={x:Static lex:LocalizeDictionary.Instance}, Path=SetCultureCommand}" />
<Button Content="{lex:Loc ar}" Margin="5" CommandParameter="ar" Command="{Binding Source={x:Static lex:LocalizeDictionary.Instance}, Path=SetCultureCommand}" />
</StackPanel>
<ComboBox ItemsSource="{Binding Source={x:Static lex:LocalizeDictionary.Instance}, Path=MergedAvailableCultures}"
SelectedItem="{Binding Source={x:Static lex:LocalizeDictionary.Instance}, Path=Culture}"
DisplayMemberPath="NativeName"
Margin="2"/>
</StackPanel>
</Window>
70 changes: 70 additions & 0 deletions tests/HelloWorldWPF.NoDesignFiles/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFLocalizeExtension.Engine;
using WPFLocalizeExtension.Providers;

namespace HalloWeltWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
TestVM vm = new TestVM();

public MainWindow()
{
vm.language = "de";
vm.color = "Background";
vm.Hours = 0;
this.DataContext = vm;

LocalizeDictionary.Instance.Culture = new System.Globalization.CultureInfo("de");
(LocalizeDictionary.Instance.DefaultProvider as ResxLocalizationProvider).SearchCultures =
new List<System.Globalization.CultureInfo>()
{
System.Globalization.CultureInfo.GetCultureInfo("de-de"),
System.Globalization.CultureInfo.GetCultureInfo("en"),
System.Globalization.CultureInfo.GetCultureInfo("he"),
System.Globalization.CultureInfo.GetCultureInfo("ar"),
};
LocalizeDictionary.Instance.OutputMissingKeys = true;
LocalizeDictionary.Instance.MissingKeyEvent += Instance_MissingKeyEvent;
}

private void Instance_MissingKeyEvent(object sender, MissingKeyEventArgs e)
{
e.MissingKeyResult = "Hello World";
}

private void BindeTestButton_Click(object sender, RoutedEventArgs e)
{
vm.Hours = vm.Hours + 1;

vm.language = vm.language switch
{
"en" => "de",
"de" => "error",
_ => "en",
};
if (vm.tenum == TestVM.TestEnum.Test1)
vm.tenum = TestVM.TestEnum.Test2;
else
vm.tenum = TestVM.TestEnum.Test1;
}
}
}
Loading