Skip to content

Commit

Permalink
fix: update timezone handling for language detection
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Sep 24, 2024
1 parent e321fa8 commit f85c33f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions SubRenamer/Helper/I18NHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@ public static class I18NHelper

public static string GetLanguageNameFromOs()
{
string timeZoneId = TimeZoneInfo.Local.Id.ToUpper();
string tz = TimeZoneInfo.Local.Id.ToLower();

// FIXME: `CultureInfo.CurrentCulture.Name` always return `null` in AvaloniaUI
var name = CultureInfo.CurrentCulture.Name;
if (string.IsNullOrWhiteSpace(name)) name = Environment.GetEnvironmentVariable("LANG");

name = name?.Trim().ToLower().Replace("_", "-") ?? "";
if (name.Contains("en") || timeZoneId == "EST" || timeZoneId == "EDT") return "en-US";
if (name.Contains("zh-hans") || name.Contains("zh-cn") || timeZoneId == "PRC") return "zh-Hans";
if (name.Contains("zh-hant") || name.Contains("zh-tw") || name.Contains("zh-hk") || timeZoneId == "ROC") return "zh-Hant";
if (name.Contains("en")) return "en-US";

if (tz == "est" || tz == "edt" || tz.Contains("america")) return "en-US"; // on macos and linux
if (tz == "pacific standard time" || tz == "mountain standard time"
|| tz == "central standard time" || tz == "eastern standard time") return "en-US"; // on windows

if (name.Contains("zh-hans") || name.Contains("zh-cn")) return "zh-Hans";
if (tz == "prc" || tz == "cst" || tz == "asia/shanghai" || tz.Contains("china")) return "zh-Hans";

if (name.Contains("zh-hant") || name.Contains("zh-tw") || name.Contains("zh-hk")) return "zh-Hant";
if (tz == "roc" || tz == "hkt" || tz == "asia/hong_kong" || tz == "asia/taipei" || tz.Contains("taiwan") || tz.Contains("hong kong"))
return "zh-Hant";

if (name.Contains("zh")) return "zh-Hans";
if (name.Contains("ja") || name.Contains("jp") || timeZoneId == "JST") return "ja-JP";
if (name.Contains("ja") || name.Contains("jp") || tz == "JST" || tz == "asia/tokyo" || tz.Contains("japan")) return "ja-JP";

return DefaultLanguage;
}
Expand Down

0 comments on commit f85c33f

Please sign in to comment.