Created
January 23, 2013 21:26
-
-
Save maxfridbe/4613643 to your computer and use it in GitHub Desktop.
Dynamic Localization wpf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In code: | |
<Button | |
Content="{DynamicResource Previous}" | |
Command="{Binding PrevView}" Grid.Column="0"/> | |
<Window.Resources> | |
<ResourceDictionary> | |
<!--<Selectors:ViewSelector x:Key="ViewSelector"> | |
</Selectors:ViewSelector>--> | |
<ResourceDictionary.MergedDictionaries> | |
<local:ResourceDictionaryLocator Assembly="{x:Type Windows:MainWindowStudioFlow}"/> | |
</ResourceDictionary.MergedDictionaries> | |
</ResourceDictionary> | |
</Window.Resources> | |
public class ResourceDictionaryLocator : ResourceDictionary | |
{ | |
private Type _assembly ; | |
public Type Assembly | |
{ | |
set | |
{ | |
_assembly = value; | |
updateSrc(); | |
} | |
} | |
public ResourceDictionaryLocator() | |
{ | |
ModuleBootstrapper.LanguageChanged += (s) => updateSrc(); | |
} | |
private void updateSrc() | |
{ | |
var uriSRC = ModuleBootstrapper.GetCurrentUICultureSource("StringResources", _assembly); | |
base.Source = uriSRC; | |
} | |
} | |
public static class ModuleBootstrapper | |
{ | |
public static Uri GetCurrentUICultureSource(string resourceName, Type containingModule) | |
{ | |
var name = containingModule.Assembly.GetName().Name; | |
var cult = Thread.CurrentThread.CurrentUICulture.ToString(); | |
var location = cult == "en-US" | |
? string.Format("pack://application:,,,/{0};component/Resources/{1}.xaml", name, resourceName) | |
: string.Format("pack://application:,,,/{0};component/Resources/{1}.{2}.xaml", name, resourceName, cult); | |
var uri = new Uri(location, UriKind.RelativeOrAbsolute); | |
return uri; | |
} | |
public static ResourceDictionary GetCurrentUICultureResourceDictionary(string resourceName, Type containingModule) | |
{ | |
var dict = new ResourceDictionary(); | |
var uri = GetCurrentUICultureSource(resourceName, containingModule); | |
dict.Source = uri; | |
return dict; | |
} | |
public static void LoadCulturalDictionary(Collection<ResourceDictionary> mergedDictionaries, string stringresources, Type getType) | |
{ | |
var name = CultureInfo.CurrentUICulture.Name; | |
if (name != "en-US") | |
{ | |
var existing = mergedDictionaries.FirstOrDefault(rd => rd.Source.OriginalString.Contains(stringresources)); | |
if (existing != null) | |
mergedDictionaries.Remove(existing); | |
var dict = ModuleBootstrapper.GetCurrentUICultureResourceDictionary(stringresources, getType); | |
mergedDictionaries.Add(dict); | |
} | |
} | |
public static void ChangeLanguage(string code) | |
{ | |
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(code); | |
if(LanguageChanged != null) | |
LanguageChanged(code); | |
} | |
public static event Action<string> LanguageChanged; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment