-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiLanguage.cs
More file actions
115 lines (105 loc) · 3.82 KB
/
MultiLanguage.cs
File metadata and controls
115 lines (105 loc) · 3.82 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NotepadSharp
{
class MultiLanguage
{
//当前默认语言
public static string _DefaultLanguage;
public static string DefaultLanguage
{
get => _DefaultLanguage;
set => _DefaultLanguage = value;
}
/// <summary>
/// 修改默认语言
/// </summary>
/// <param name="lang">待设置默认语言</param>
public static void SetDefaultLanguage(string lang)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
DefaultLanguage = lang;
Properties.Settings.Default.DefaultLanguage = lang;
Properties.Settings.Default.Save();
}
/// <summary>
/// 加载语言
/// </summary>
/// <param name="form">加载语言的窗口</param>
/// <param name="formType">窗口的类型</param>
public static void LoadLanguage(Form form, Type formType)
{
if (form != null)
{
System.ComponentModel.ComponentResourceManager resources =
new System.ComponentModel.ComponentResourceManager(formType);
if (Properties.Settings.Default.DefaultLanguage=="")
{
resources.ApplyResources(form, "$this", CultureInfo.CurrentUICulture);
SetDefaultLanguage(CultureInfo.CurrentUICulture.Name);
}
else
{
resources.ApplyResources(form, "$this", new CultureInfo(Properties.Settings.Default.DefaultLanguage));
SetDefaultLanguage(Properties.Settings.Default.DefaultLanguage);
}
Loading(form, resources);
}
}
/// <summary>
/// 加载语言
/// </summary>
/// <param name="control">控件</param>
/// <param name="resources">语言资源</param>
private static void Loading(Control control, System.ComponentModel.ComponentResourceManager resources)
{
if (control is MenuStrip)
{
//将资源与控件对应
resources.ApplyResources(control, control.Name);
MenuStrip ms = (MenuStrip) control;
if (ms.Items.Count > 0)
{
foreach (ToolStripMenuItem c in ms.Items)
{
//遍历菜单
Loading(c, resources);
}
}
}
foreach (Control c in control.Controls)
{
resources.ApplyResources(c, c.Name);
Loading(c, resources);
}
}
/// <summary>
/// 遍历菜单
/// </summary>
/// <param name="item">菜单项</param>
/// <param name="resources">语言资源</param>
private static void Loading(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
{
if (item is ToolStripMenuItem)
{
resources.ApplyResources(item, item.Name);
ToolStripMenuItem tsmi = (ToolStripMenuItem) item;
if (tsmi.DropDownItems.Count > 0)
{
foreach (object c in tsmi.DropDownItems)
{
if (c is ToolStripMenuItem)
{
Loading((ToolStripMenuItem)c, resources);
}
}
}
}
}
}
}