Try to compile following code
<Window x:Class="WPFLocalizationSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel HorizontalAlignment="Left" Margin="20" VerticalAlignment="Top">
<TextBlock Text="{DynamicResource tbName}" Margin="2" />
<TextBox Width="200" Margin="2"/>
<Button Content="{DynamicResource btnAdd}" HorizontalAlignment="Left" Margin="2" />
</StackPanel>
<StackPanel HorizontalAlignment="Left" Margin="20,181,0,0" VerticalAlignment="Top" Orientation="Vertical" Width="305">
<TextBlock Text="Translation Panel" FontSize="20" FontWeight="Black" />
<Grid >
<TextBlock Text="{DynamicResource tbName}" Margin="2" />
<TextBox Width="200" Margin="2" HorizontalAlignment="Right" Tag="tbName" x:Name="tbNameLoc"/>
</Grid>
<Grid>
<TextBlock Text="{DynamicResource btnAdd}" Margin="2" />
<TextBox Width="200" Margin="2" HorizontalAlignment="Right" Tag="btnAdd" x:Name="btnAddLoc" />
</Grid>
<Button Content="Translate/Update" HorizontalAlignment="Left" Margin="2" Click="Button_Click" />
</StackPanel>
</Grid>
</Window>
Here is MainWindow.Xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace WPFLocalizationSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static ResourceDictionary CurrentLanguageDictionary;
private static string LanguageResourceFile = "en-US.xaml";
private Dictionary<string, System.Windows.Controls.TextBox> TextBoxControls = new Dictionary<string, TextBox>();
public MainWindow()
{
InitializeComponent();
AddControlsToLocalization();
}
private void AddControlsToLocalization()
{
TextBoxControls.Add("tbName", tbNameLoc);
TextBoxControls.Add("btnAdd", btnAddLoc);
}
public static bool SaveLanguageFile(Dictionary<string, System.Windows.Controls.TextBox> TextBoxControls, string selectedLang)
{
bool status = false;
try
{
using (var fs = new FileStream(LanguageResourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Read in ResourceDictionary File
CurrentLanguageDictionary = (ResourceDictionary)XamlReader.Load(fs);
}
foreach (string key in TextBoxControls.Keys)
{
TextBox txt = TextBoxControls[key];
if (txt.Text.Length > 0)
{
CurrentLanguageDictionary[key] = txt.Text;
}
}
using (var writer = new StreamWriter(LanguageResourceFile))
{
XamlWriter.Save(CurrentLanguageDictionary, writer);
}
status = true;
}
catch (Exception _exception)
{
MessageBox.Show("Exception in language file edit");
return false;
}
SetLanguageResourceDictionary(LanguageResourceFile, App.Current);
return status;
}
public static void SetLanguageResourceDictionary(String inFile, Application application)
{
if (File.Exists(inFile))
{
// Read in ResourceDictionary File
var languageDictionary = new ResourceDictionary();
languageDictionary.Source = new Uri(inFile,UriKind.Relative);
// Remove any previous Localization dictionaries loaded
int langDictId = -1;
for (int i = 0; i < application.Resources.MergedDictionaries.Count; i++)
{
var md = application.Resources.MergedDictionaries[i];
// Make sure your Localization ResourceDictionarys have the ResourceDictionaryName
// key and that it is set to a value starting with "Loc-".
if (md.Contains("ResourceDictionaryName"))
{
if (md["ResourceDictionaryName"].ToString().Equals("en-US"))
{
langDictId = i;
break;
}
}
}
if (langDictId == -1)
{
// Add in newly loaded Resource Dictionary
application.Resources.MergedDictionaries.Add(languageDictionary);
}
else
{
// Replace the current langage dictionary with the new one
application.Resources.MergedDictionaries[langDictId] = languageDictionary;
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SaveLanguageFile(TextBoxControls, "en-US");
}
}
}
Here is sample en-US.xaml (resource dictionary file)
<ResourceDictionary 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">
<sys:String x:Key="ResourceDictionaryName">en-US</sys:String>
<sys:String x:Key="tbName">Enter youe Name</sys:String>
<sys:String x:Key="btnAdd">Add</sys:String>
</ResourceDictionary>
Adding resource file in app.xaml
<Application x:Class="WPFLocalizationSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="en-US.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
if you compile this , you will see a form with two sample controls.
below translation panel added. you can type any text that you want to change
click on translate button changed text will updated in en-US file.
if you want to save different file also you can do.
Note: en-US.xaml file should be in release folder.