Skip to content

Commit

Permalink
refactor locale initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
stakira committed May 16, 2023
1 parent 51a72e0 commit af16021
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
4 changes: 3 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Auto detect text files and perform LF normalization
* text=auto

*.cs text diff=csharp
*.cs text diff=csharp eol=crlf
*.cshtml text diff=html
*.csx text diff=csharp
*.sln text eol=crlf
*.csproj text eol=crlf
*.xaml text eol=crlf
*.axaml text eol=crlf
61 changes: 39 additions & 22 deletions OpenUtau/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -35,38 +36,54 @@ public override void OnFrameworkInitializationCompleted() {

public void InitializeCulture() {
Log.Information("Initializing culture.");
var language = CultureInfo.InstalledUICulture.Name;
if (!string.IsNullOrEmpty(Core.Util.Preferences.Default.Language)) {
language = Core.Util.Preferences.Default.Language;
string sysLang = CultureInfo.InstalledUICulture.Name;
string prefLang = Core.Util.Preferences.Default.Language;
var languages = GetLanguages();
if (languages.TryGetValue(prefLang, out var res)) {
SetLanguage(res);
} else if (languages.TryGetValue(sysLang, out res)) {
SetLanguage(res);
Core.Util.Preferences.Default.Language = sysLang;
Core.Util.Preferences.Save();
} else {
SetLanguage(languages["en-US"]);
}
SetLanguage(language);

// Force using InvariantCulture to prevent issues caused by culture dependent string conversion, especially for floating point numbers.
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
Log.Information("Initialized culture.");
}

public static void SetLanguage(string language) {
var dictionaryList = Current.Resources.MergedDictionaries
public static Dictionary<string, ResourceInclude> GetLanguages() {
if (Current == null) {
return new();
}
var re = new Regex(@"Strings\.?([\w-]+)\.axaml");
return Current.Resources.MergedDictionaries
.Select(res => (ResourceInclude)res)
.ToList();
var resDictName = string.Format(@"Strings.{0}.axaml", language);
var resDict = dictionaryList
.FirstOrDefault(d => d.Source!.OriginalString.Contains(resDictName));
if (resDict == null) {
resDict = dictionaryList.FirstOrDefault(d => d.Source!.OriginalString.Contains("Strings.axaml"));
.Where(res => res.Source!.OriginalString.Contains("Strings."))
.ToDictionary(res => {
var m = re.Match(res.Source!.OriginalString);
return string.IsNullOrEmpty(m.Groups[1].Value) ? "en-US" : m.Groups[1].Value;
});
}

public static void SetLanguage(ResourceInclude res) {
if (Current == null) {
return;
}
if (resDict != null) {
Current.Resources.MergedDictionaries.Remove(resDict);
Current.Resources.MergedDictionaries.Add(resDict);
var langName = resDict.Source.OriginalString.Replace("/Strings/Strings", "").Replace(".", "").Replace("axaml", "");
if (langName == "")
langName = language;
if (Core.Util.Preferences.Default.Language != langName) {
Core.Util.Preferences.Default.Language = langName;
Core.Util.Preferences.Save();
}
Current.Resources.MergedDictionaries.Remove(res);
Current.Resources.MergedDictionaries.Add(res);
}

public static void SetLanguage(string language) {
if (Current == null) {
return;
}
var languages = GetLanguages();
if (languages.TryGetValue(language, out var res)) {
SetLanguage(res);
}
}

Expand Down
2 changes: 1 addition & 1 deletion OpenUtau/Controls/TrackHeader.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
ContextRequested="PanFaderContextRequested"/>
<TextBlock FontSize="11" FontFamily="monospace" HorizontalAlignment="Right" Margin="0,0,5,0">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:00R;00L;mid}">
<MultiBinding StringFormat="{}{0:00R;00L;C}">
<Binding Path="Pan"/>
</MultiBinding>
</TextBlock.Text>
Expand Down

0 comments on commit af16021

Please sign in to comment.