Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions BLE_tcp_bridge/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -39,7 +39,7 @@ public static AppConfig Load()
}
catch (Exception ex)
{
Console.WriteLine("配置文件读取失败: " + ex.Message);
Console.WriteLine(BridgeText.T("configReadError", ex.Message));
return CreateDefault();
}
}
Expand All @@ -60,7 +60,7 @@ public void Save()
}
catch (Exception ex)
{
Console.WriteLine("配置文件保存失败: " + ex.Message);
Console.WriteLine(BridgeText.T("configWriteError", ex.Message));
}
}

Expand Down
12 changes: 11 additions & 1 deletion BLE_tcp_bridge/BLE_tcp_driver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,23 @@
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="AppConfig.cs" />
<Compile Include="BridgeText.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Protocol.cs" />
<Compile Include="TcpServer.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Localization\Messages_en.resx">
<LogicalName>BLE_tcp_driver.Messages.en.resources</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Localization\Messages_ru.resx">
<LogicalName>BLE_tcp_driver.Messages.ru.resources</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Localization\Messages_zh.resx">
<LogicalName>BLE_tcp_driver.Messages.zh.resources</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
Expand Down Expand Up @@ -115,4 +125,4 @@
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
60 changes: 60 additions & 0 deletions BLE_tcp_bridge/BridgeText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Resources;

namespace BLE_tcp_driver
{
/// <summary>UI text only; language never changes wire data or diagnostic IDs.</summary>
internal static class BridgeText
{
private static readonly ResourceManager English = Manager("en");
private static ResourceManager selected = English;
internal static string Language { get; private set; } = "en";

private static ResourceManager Manager(string language) =>
new ResourceManager("BLE_tcp_driver.Messages." + language, typeof(BridgeText).Assembly);

internal static string Normalize(string language)
{
string code = (language ?? "").Trim().ToLowerInvariant().Split('-', '_')[0];
return code == "ru" || code == "zh" ? code : "en";
}

internal static void Initialize(string[] arguments, string preferencesPath, string systemLanguage)
{
string explicitLanguage = arguments.FirstOrDefault(arg =>
arg.StartsWith("--language=", StringComparison.OrdinalIgnoreCase));
string language = explicitLanguage == null ? null : explicitLanguage.Substring(11);
if (string.IsNullOrWhiteSpace(language))
{
try
{
if (File.Exists(preferencesPath))
foreach (string line in File.ReadLines(preferencesPath))
{
string entry = line.Trim();
if (entry.StartsWith("#") || entry.StartsWith("!")) continue;
int delimiter = entry.IndexOfAny(new[] { '=', ':' });
if (delimiter > 0 && entry.Substring(0, delimiter).Trim() == "AhaKeySelectedLanguage")
language = entry.Substring(delimiter + 1).Trim();
}
}
catch (IOException) { }
catch (UnauthorizedAccessException) { }
}
Language = Normalize(string.IsNullOrWhiteSpace(language) ? systemLanguage : language);
selected = Manager(Language);
}

internal static string T(string key, params object[] arguments)
{
string template = selected.GetString(key, CultureInfo.InvariantCulture)
?? English.GetString(key, CultureInfo.InvariantCulture);
if (template == null) throw new InvalidOperationException("Missing bridge translation: " + key);
return arguments.Length == 0 ? template
: string.Format(CultureInfo.GetCultureInfo(Language), template, arguments);
}
}
}
18 changes: 9 additions & 9 deletions BLE_tcp_bridge/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading