aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Ava/Common/Locale/LocaleManager.cs
blob: 5bcaa437e6cadfd4b87032c872c5c4a4b3d03c16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Ryujinx.Ava.UI.ViewModels;
using Ryujinx.Common;
using Ryujinx.Common.Utilities;
using Ryujinx.Ui.Common.Configuration;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;

namespace Ryujinx.Ava.Common.Locale
{
    class LocaleManager : BaseModel
    {
        private const string DefaultLanguageCode = "en_US";

        private Dictionary<LocaleKeys, string> _localeStrings;
        private ConcurrentDictionary<LocaleKeys, object[]> _dynamicValues;

        public static LocaleManager Instance { get; } = new LocaleManager();
        public Dictionary<LocaleKeys, string> LocaleStrings { get => _localeStrings; set => _localeStrings = value; }


        public LocaleManager()
        {
            _localeStrings = new Dictionary<LocaleKeys, string>();
            _dynamicValues = new ConcurrentDictionary<LocaleKeys, object[]>();

            Load();
        }

        public void Load()
        {
            string localeLanguageCode = CultureInfo.CurrentCulture.Name.Replace('-', '_');

            if (Program.PreviewerDetached)
            {
                if (!string.IsNullOrEmpty(ConfigurationState.Instance.Ui.LanguageCode.Value))
                {
                    localeLanguageCode = ConfigurationState.Instance.Ui.LanguageCode.Value;
                }
            }

            // Load english first, if the target language translation is incomplete, we default to english.
            LoadDefaultLanguage();

            if (localeLanguageCode != DefaultLanguageCode)
            {
                LoadLanguage(localeLanguageCode);
            }
        }

        public string this[LocaleKeys key]
        {
            get
            {
                if (_localeStrings.TryGetValue(key, out string value))
                {
                    if (_dynamicValues.TryGetValue(key, out var dynamicValue))
                    {
                        return string.Format(value, dynamicValue);
                    }

                    return value;
                }

                return key.ToString();
            }
            set
            {
                _localeStrings[key] = value;

                OnPropertyChanged();
            }
        }

        public void UpdateDynamicValue(LocaleKeys key, params object[] values)
        {
            _dynamicValues[key] = values;

            OnPropertyChanged("Item");
        }

        public void LoadDefaultLanguage()
        {
            LoadLanguage(DefaultLanguageCode);
        }

        public void LoadLanguage(string languageCode)
        {
            string languageJson = EmbeddedResources.ReadAllText($"Ryujinx.Ava/Assets/Locales/{languageCode}.json");

            if (languageJson == null)
            {
                return;
            }

            var strings = JsonHelper.Deserialize<Dictionary<string, string>>(languageJson);

            foreach (var item in strings)
            {
                if (Enum.TryParse<LocaleKeys>(item.Key, out var key))
                {
                    this[key] = item.Value;
                }
            }

            if (Program.PreviewerDetached)
            {
                ConfigurationState.Instance.Ui.LanguageCode.Value = languageCode;
                ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
            }
        }
    }
}