-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
94 lines (85 loc) · 3.66 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace TouristOrgAdmin
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static List<CultureInfo> m_Languages = new List<CultureInfo>();
public static event EventHandler LanguageChanged;
public static List<CultureInfo> Languages
{
get
{
return m_Languages;
}
}
public App()
{
InitializeComponent();
App.LanguageChanged += App_LanguageChanged;
m_Languages.Clear();
m_Languages.Add(new CultureInfo("en-US"));
m_Languages.Add(new CultureInfo("ru-RU"));
m_Languages.Add(new CultureInfo("be-BY"));
Language = TouristOrgAdmin.Properties.Settings.Default.DefaultLanguage;
}
private void App_LanguageChanged(Object sender, EventArgs e)
{
TouristOrgAdmin.Properties.Settings.Default.DefaultLanguage = Language;
TouristOrgAdmin.Properties.Settings.Default.Save();
}
public static CultureInfo Language
{
get
{
return System.Threading.Thread.CurrentThread.CurrentUICulture;
}
set
{
if (value == null) throw new ArgumentNullException("value");
if (value == System.Threading.Thread.CurrentThread.CurrentUICulture) return;
//1. Меняем язык приложения:
System.Threading.Thread.CurrentThread.CurrentUICulture = value;
//2. Создаём ResourceDictionary для новой культуры
ResourceDictionary dict = new ResourceDictionary();
switch (value.Name)
{
case "ru-RU":
dict.Source = new Uri(String.Format("Resources/LanguageDictionary.{0}.xaml", value.Name), UriKind.Relative);
break;
case "be-BY":
dict.Source = new Uri(String.Format("Resources/LanguageDictionary.{0}.xaml", value.Name), UriKind.Relative);
break;
default:
dict.Source = new Uri("Resources/LanguageDictionary.xaml", UriKind.Relative);
break;
}
//3. Находим старую ResourceDictionary и удаляем его и добавляем новую ResourceDictionary
ResourceDictionary oldDict = (from d in Application.Current.Resources.MergedDictionaries
where d.Source != null && d.Source.OriginalString.StartsWith("Resources/LanguageDictionary.")
select d).FirstOrDefault();
if (oldDict != null)
{
int ind = Application.Current.Resources.MergedDictionaries.IndexOf(oldDict);
Application.Current.Resources.MergedDictionaries.Remove(oldDict);
Application.Current.Resources.MergedDictionaries.Insert(ind, dict);
}
else
{
Application.Current.Resources.MergedDictionaries.Add(dict);
}
//4. Вызываем евент для оповещения всех окон.
LanguageChanged(Application.Current, new EventArgs());
}
}
}
}