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.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.IO;
  4. using Shadowsocks.Properties;
  5. using Shadowsocks.Util;
  6. using System.Windows.Forms;
  7. using Microsoft.VisualBasic.FileIO;
  8. using System.Text;
  9. namespace Shadowsocks.Controller
  10. {
  11. public static class I18N
  12. {
  13. private static readonly 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. Logging.Info($"Using {localeNames[targetIndex]} translation for {locale}");
  41. }
  42. // Still not found, exit
  43. if (targetIndex == -1 || enIndex == targetIndex)
  44. {
  45. Logging.Info($"Translation for {locale} not found");
  46. return;
  47. }
  48. // read translation lines
  49. while (!csvParser.EndOfData)
  50. {
  51. string[] translations = csvParser.ReadFields();
  52. string source = translations[enIndex];
  53. string translation = translations[targetIndex];
  54. // source string or translation empty
  55. if (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(translation)) continue;
  56. // line start with comment
  57. if (translations[0].TrimStart(' ')[0] == '#') continue;
  58. _strings[source] = translation;
  59. }
  60. }
  61. }
  62. static I18N()
  63. {
  64. string i18n;
  65. if (!File.Exists(I18N_FILE))
  66. {
  67. i18n = Resources.i18n_csv;
  68. File.WriteAllText(I18N_FILE, i18n, Encoding.UTF8);
  69. }
  70. else
  71. {
  72. i18n = File.ReadAllText(I18N_FILE, Encoding.UTF8);
  73. }
  74. Logging.Info("Current language is: " + CultureInfo.CurrentCulture.Name);
  75. Init(i18n, CultureInfo.CurrentCulture.Name);
  76. }
  77. public static string GetString(string key, params object[] args)
  78. {
  79. return string.Format(_strings.TryGetValue(key, out var value) ? value : key, args);
  80. }
  81. public static void TranslateForm(Form c)
  82. {
  83. if (c == null) return;
  84. c.Text = GetString(c.Text);
  85. foreach (var item in ViewUtils.GetChildControls<Control>(c))
  86. {
  87. if (item == null) continue;
  88. item.Text = GetString(item.Text);
  89. }
  90. TranslateMenu(c.Menu);
  91. }
  92. public static void TranslateMenu(Menu m)
  93. {
  94. if (m == null) return;
  95. foreach (var item in ViewUtils.GetMenuItems(m))
  96. {
  97. if (item == null) continue;
  98. item.Text = GetString(item.Text);
  99. }
  100. }
  101. }
  102. }