Files
SGGL_SHJ/SGGL/BLL/UiText.cs
T
lpf e30bb4b1b7 feat(i18n): 支持系统中英文切换
为页面、菜单及提示接入中英文资源,保留业务值和导出中文标题,以支持用户切换界面语言。
2026-11-02 11:16:43 +08:00

98 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Text.RegularExpressions;
namespace BLL
{
/// <summary>界面资源;业务值和导出内容不通过此入口转换。</summary>
public static class UiText
{
private static readonly ResourceManager Resources = new ResourceManager("BLL.Resources.UiText", typeof(UiText).Assembly);
private static readonly CultureInfo Chinese = CultureInfo.GetCultureInfo("zh-CN");
private static readonly Lazy<Dictionary<string, string>> OriginalKeys = new Lazy<Dictionary<string, string>>(() =>
Resources.GetResourceSet(Chinese, true, true).Cast<System.Collections.DictionaryEntry>()
.Where(entry => entry.Key.ToString().StartsWith("T_", StringComparison.Ordinal) || entry.Key.ToString().StartsWith("Menu_", StringComparison.Ordinal))
.GroupBy(entry => (entry.Key.ToString().StartsWith("Menu_", StringComparison.Ordinal) ? "Menu_" : "T_") + entry.Value)
.ToDictionary(group => group.Key, group => group.Select(entry => entry.Key.ToString()).OrderBy(key => key, StringComparer.Ordinal).First()));
public static string Key(string text, string prefix = "T_")
{
foreach (string category in new[] { "export", "option", "options" })
{
if (text.StartsWith(category + "|", StringComparison.Ordinal))
return char.ToUpperInvariant(category[0]) + category.Substring(1) + "_" + Regex.Replace(text.Substring(category.Length + 1), @"[^\p{L}\p{N}_]+", "_").Trim('_');
}
string key;
if (OriginalKeys.Value.TryGetValue(prefix + text, out key)) return key;
string readable = Regex.Replace(Regex.Replace(text, "<[^>]*>", ""), @"[^\p{L}\p{N}_]+", "_").Trim('_');
return prefix + (readable.Length > 48 ? readable.Substring(0, 48) : readable);
}
public static string Get(string key, params object[] args)
{
string value = Resources.GetString(key, CultureInfo.CurrentUICulture);
string resourceKey;
if (value == null && OriginalKeys.Value.TryGetValue("T_" + key, out resourceKey))
value = Resources.GetString(resourceKey, CultureInfo.CurrentUICulture);
value = value ?? key;
return args.Length == 0 ? value : string.Format(CultureInfo.CurrentCulture, value, args);
}
public static string GetChinese(string key)
{
return Resources.GetString(key, Chinese);
}
private static readonly Lazy<Tuple<Regex, string>[]> MessageTemplates = new Lazy<Tuple<Regex, string>[]>(() =>
Resources.GetResourceSet(Chinese, true, true).Cast<System.Collections.DictionaryEntry>()
.Where(entry => entry.Key.ToString().StartsWith("T_", StringComparison.Ordinal) && Regex.IsMatch(entry.Value.ToString(), @"\{\d+\}"))
.Select(entry => Tuple.Create(new Regex("^" + Regex.Replace(Regex.Escape(entry.Value.ToString()), @"\\\{(\d+)}", match => "(?<p" + match.Groups[1].Value + ">.*?)") + "$", RegexOptions.Singleline), entry.Key.ToString()))
.ToArray());
public static string Message(string text)
{
if (text == null || CultureInfo.CurrentUICulture.TwoLetterISOLanguageName != "en") return text;
string translated = Get(text);
if (translated != text) return translated;
foreach (var template in MessageTemplates.Value)
{
Match match = template.Item1.Match(text);
if (match.Success)
{
return Regex.Replace(Get(template.Item2), @"\{(\d+)\}", item => match.Groups["p" + item.Groups[1].Value].Value);
}
}
return string.Concat(Regex.Split(text, @"(<br\s*/?>|\r?\n)", RegexOptions.IgnoreCase)
.Select(part => part == text ? part : Message(part)));
}
public static IDictionary<string, string> ClientResources(IEnumerable<string> keys)
{
var result = new Dictionary<string, string>();
foreach (string key in keys.Distinct())
{
string original = GetChinese(key);
if (original != null)
{
string translated = Get(key);
result[key] = translated;
result[original] = translated;
}
}
return result;
}
/// <summary>仅用于明确按分号或换行拼接的界面错误列表,括号内的业务编号保持原文。</summary>
public static string MessageList(string text)
{
if (text == null || CultureInfo.CurrentUICulture.TwoLetterISOLanguageName != "en") return text;
return string.Concat(Regex.Split(text, @"(<br\s*/?>|\r?\n|;(?![^【】]*】))", RegexOptions.IgnoreCase)
.Select(part => part == ";" ? "; " : Message(part)));
}
}
}