-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLocalizationService.cs
More file actions
72 lines (60 loc) · 2.55 KB
/
LocalizationService.cs
File metadata and controls
72 lines (60 loc) · 2.55 KB
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
//This file is part of AudioCuesheetEditor.
//AudioCuesheetEditor is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//AudioCuesheetEditor is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Model.Options;
using System.Globalization;
namespace AudioCuesheetEditor.Services.UI
{
public class LocalizationService(ILocalStorageOptionsProvider localStorageOptionsProvider)
{
private readonly ILocalStorageOptionsProvider _localStorageOptionsProvider = localStorageOptionsProvider;
public const String DefaultCulture = "en-US";
public static IReadOnlyCollection<CultureInfo> AvailableCultures
{
get
{
var cultures = new List<CultureInfo>
{
new(DefaultCulture),
new("de-DE")
};
return cultures.AsReadOnly();
}
}
public event EventHandler? LocalizationChanged;
public CultureInfo SelectedCulture => CultureInfo.DefaultThreadCurrentUICulture ?? CultureInfo.CurrentUICulture;
public async Task SetCultureFromConfigurationAsync()
{
var options = await _localStorageOptionsProvider.GetOptions<ApplicationOptions>();
ChangeLanguage(options.Culture.Name);
}
public async Task ChangeLanguageAsync(string name)
{
if (ChangeLanguage(name))
{
await _localStorageOptionsProvider.SaveOptionsValue<ApplicationOptions>(x => x.CultureName!, name);
LocalizationChanged?.Invoke(this, new EventArgs());
}
}
private static Boolean ChangeLanguage(string name)
{
var newCulture = AvailableCultures.SingleOrDefault(c => c.Name == name);
if (newCulture != null)
{
CultureInfo.DefaultThreadCurrentUICulture = newCulture;
CultureInfo.CurrentUICulture = newCulture;
}
return newCulture != null;
}
}
}