Skip to content

Commit

Permalink
dynamically change to stringresources for a language
Browse files Browse the repository at this point in the history
  • Loading branch information
changbw001 committed Dec 19, 2016
1 parent df04c97 commit 68fb65e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
54 changes: 36 additions & 18 deletions App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//using System.Collections.Generic;
//using System.Configuration;
//using System.Data;
//using System.Linq;
using System.Linq;
//using System.Threading.Tasks;
using System.Windows;

Expand Down Expand Up @@ -33,32 +33,50 @@ private void RunCheck(object sender1, StartupEventArgs e1)
return null;
};

//localization
var lang = System.Threading.Thread.CurrentThread.CurrentCulture.Name.Substring(0, 2);
//check if stringresources.lang exist
bool langadded = false;
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var resourceName = assembly.GetName().Name + ".g";
var resourceManager = new System.Resources.ResourceManager(resourceName, assembly);
try
{
var resourceSet = resourceManager.GetResourceSet(System.Threading.Thread.CurrentThread.CurrentCulture, true, true);
foreach (System.Collections.DictionaryEntry resource in resourceSet)
{
if ((string)resource.Key == @"resources/stringresources." + lang + ".baml")
{
var dict = new ResourceDictionary();
dict.Source = new Uri(@"Resources\StringResources." + lang + ".xaml", UriKind.Relative);
Resources.MergedDictionaries.Add(dict);
langadded = true;
break;
}
}
}
finally
{
resourceManager.ReleaseAllResources();
}
//set english as fallback language
if (!langadded)
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(@"Resources\StringResources.en.xaml", UriKind.Relative) });

//other run checks
if (PathIsNetworkPathW(System.AppDomain.CurrentDomain.BaseDirectory))
{
MessageBox.Show((string)Resources["msgbox_run_from_network"], "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
Current.Shutdown();
return;
}

//if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
//{
// MessageBox.Show("Only one instance of DesktopNote can be running.", "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
// Current.Shutdown();
// return;
//}

//localization
var dict = new ResourceDictionary();
switch (System.Threading.Thread.CurrentThread.CurrentCulture.Name.Substring(0, 2))
if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
{
case "zh":
dict.Source = new Uri(@"Resources\StringResources.zh.xaml", UriKind.Relative);
break;
default:
dict.Source = new Uri(@"Resources\StringResources.xaml", UriKind.Relative);
break;
MessageBox.Show((string)Resources["msgbox_one_inst"], "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
Current.Shutdown();
return;
}
Resources.MergedDictionaries.Add(dict);

var mainwin = new MainWindow();
mainwin.Show();
Expand Down
2 changes: 1 addition & 1 deletion DesktopNote.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="Resources\StringResources.xaml">
<Page Include="Resources\StringResources.en.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ Program will exit now.</sys:String>
<sys:String x:Key="tooltip_strikethrough">Strike-through (Ctrl + D)</sys:String>
<sys:String x:Key="tooltip_underline">Underline (Ctrl + U)</sys:String>
<sys:String x:Key="tooltip_undo">Undo (Ctrl + Z)</sys:String>

<sys:String x:Key="msgbox_one_inst">DesktopNote is already running.</sys:String>
</ResourceDictionary>
2 changes: 1 addition & 1 deletion Resources/StringResources.zh.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@
<sys:String x:Key="tooltip_strikethrough">删除线 (Ctrl + D)</sys:String>
<sys:String x:Key="tooltip_underline">下划线 (Ctrl + U)</sys:String>
<sys:String x:Key="tooltip_undo">撤销 (Ctrl + Z)</sys:String>

<sys:String x:Key="msgbox_one_inst">DesktopNote已经在运行。</sys:String>
</ResourceDictionary>

0 comments on commit 68fb65e

Please sign in to comment.