You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

I18N.cs 4.2 kB

10 years ago
10 years ago
10 years ago
10 years ago
6 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Microsoft.VisualBasic.FileIO;
  2. using Shadowsocks.Properties;
  3. using Shadowsocks.Util;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace Shadowsocks.Controller
  10. {
  11. public static class I18N
  12. {
  13. public const string I18N_FILE = "i18n.csv";
  14. private static Dictionary<string, string> _strings = new Dictionary<string, string>();
  15. private static void Init(string res, string locale)
  16. {
  17. using (TextFieldParser csvParser = new TextFieldParser(new StringReader(res)))
  18. {
  19. csvParser.SetDelimiters(",");
  20. // search language index
  21. string[] localeNames = csvParser.ReadFields();
  22. int enIndex = 0;
  23. int targetIndex = -1;
  24. for (int i = 0; i < localeNames.Length; i++)
  25. {
  26. if (localeNames[i] == "en")
  27. enIndex = i;
  28. if (localeNames[i] == locale)
  29. targetIndex = i;
  30. }
  31. // Fallback to same language with different region
  32. if (targetIndex == -1)
  33. {
  34. string localeNoRegion = locale.Split('-')[0];
  35. for (int i = 0; i < localeNames.Length; i++)
  36. {
  37. if (localeNames[i].Split('-')[0] == localeNoRegion)
  38. targetIndex = i;
  39. }
  40. if (targetIndex != -1 && enIndex != targetIndex)
  41. {
  42. Logging.Info($"Using {localeNames[targetIndex]} translation for {locale}");
  43. }
  44. else
  45. {
  46. // Still not found, exit
  47. Logging.Info($"Translation for {locale} not found");
  48. return;
  49. }
  50. }
  51. // read translation lines
  52. while (!csvParser.EndOfData)
  53. {
  54. string[] translations = csvParser.ReadFields();
  55. string source = translations[enIndex];
  56. string translation = translations[targetIndex];
  57. // source string or translation empty
  58. if (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(translation)) continue;
  59. // line start with comment
  60. if (translations[0].TrimStart(' ')[0] == '#') continue;
  61. _strings[source] = translation;
  62. }
  63. }
  64. }
  65. static I18N()
  66. {
  67. string i18n;
  68. string locale = CultureInfo.CurrentCulture.Name;
  69. if (!File.Exists(I18N_FILE))
  70. {
  71. i18n = Resources.i18n_csv;
  72. //File.WriteAllText(I18N_FILE, i18n, Encoding.UTF8);
  73. }
  74. else
  75. {
  76. Logging.Info("Using external translation");
  77. i18n = File.ReadAllText(I18N_FILE, Encoding.UTF8);
  78. }
  79. Logging.Info("Current language is: " + locale);
  80. Init(i18n, locale);
  81. }
  82. public static string GetString(string key, params object[] args)
  83. {
  84. return string.Format(_strings.TryGetValue(key.Trim(), out var value) ? value : key, args);
  85. }
  86. public static void TranslateForm(Form c)
  87. {
  88. if (c == null) return;
  89. c.Text = GetString(c.Text);
  90. foreach (var item in ViewUtils.GetChildControls<Control>(c))
  91. {
  92. if (item == null) continue;
  93. item.Text = GetString(item.Text);
  94. }
  95. TranslateMenu(c.Menu);
  96. }
  97. public static void TranslateMenu(Menu m)
  98. {
  99. if (m == null) return;
  100. foreach (var item in ViewUtils.GetMenuItems(m))
  101. {
  102. if (item == null) continue;
  103. item.Text = GetString(item.Text);
  104. }
  105. }
  106. }
  107. }