forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L10N.cs
153 lines (144 loc) · 5.53 KB
/
L10N.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using MissionPlanner.Properties;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using log4net;
using MissionPlanner.Utilities;
namespace MissionPlanner
{
public class L10N
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static CultureInfo ConfigLang;
public static bool isMirrorAvailable = true;
public static bool isMirrorAvailableChecked = false;
static L10N()
{
ConfigLang = GetConfigLang();
Strings.Culture = ConfigLang;
//In .NET 4.5,System.Globalization.CultureInfo.DefaultThreadCurrentCulture & DefaultThreadCurrentUICulture is avaiable
}
public static CultureInfo GetConfigLang()
{
if (Settings.Instance["language"] == null)
return CultureInfo.CurrentUICulture;
CultureInfo ci = CultureInfoEx.GetCultureInfo(Settings.Instance["language"]);
if (ci != null)
{
return ci;
}
else
{
return System.Globalization.CultureInfo.CurrentUICulture;
}
}
public static string ReplaceMirrorUrl(ref string url)
{
switch (ConfigLang.Name)
{
case "zh-CN":
case "zh-Hans":
if (!isMirrorAvailableChecked) CheckMirror();
if (isMirrorAvailable)
{
log.InfoFormat("old url {0}", url);
if (url.Contains("firmware.ardupilot.org"))
{
url = url.Replace("firmware.ardupilot.org", "firmware.diywrj.com");
}
else if (url.Contains("firmware.diydrones.com"))
{
url = url.Replace("firmware.diydrones.com", "firmware.diywrj.com");
}
else if (url.Contains("raw.github.com"))
{
url = url.Replace("raw.github.com", "githubraw.diywrj.com");
}
/*
else if (url.Contains("raw.githubusercontent.com"))
{
url = url.Replace("raw.githubusercontent.com", "githubraw.diywrj.com");
}
else if (url.Contains("github.com"))
{
url = url.Replace("github.com", "github.diywrj.com");
}
*/
log.InfoFormat("updated url {0}", url);
}
break;
default:
break;
}
return url;
}
public static void CheckMirror()
{
switch (ConfigLang.Name)
{
case "zh-CN":
case "zh-Hans":
bool isDIYWRJ = CheckHTTP("http://firmware.diywrj.com");
bool isDIYDRONES = CheckHTTP("http://firmware.ardupilot.org");
bool isGITHUB = Ping("raw.github.com");
if (!isDIYWRJ)
{
string notice =
String.Format(
"[✘] 奠基网国内镜像\r\n\r\n{0} diydrones官网服务器\r\n\r\n{1} GitHub服务器\r\n\r\n已切换到官网服务器,\r\n您的固件下载和软件更新可能会受到影响。",
(isDIYDRONES ? "[✔]" : "[✘]"), (isGITHUB ? "[✔]" : "[✘]"));
CustomMessageBox.Show(notice, "服务器连通性检查");
isMirrorAvailable = false;
}
break;
}
isMirrorAvailableChecked = true;
}
public static bool Ping(string ip)
{
try
{
using (System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping())
{
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "MissionPlanner";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 500;
System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
return true;
else
return false;
}
}
catch
{
return false;
}
}
public static bool CheckHTTP(string url)
{
try
{
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
req.Timeout = 500;
using (HttpWebResponse response = (HttpWebResponse) req.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
return true;
else
return false;
}
}
catch
{
return false;
}
}
}
}