diff --git a/Wino.Core.Domain/Interfaces/ITranslationService.cs b/Wino.Core.Domain/Interfaces/ITranslationService.cs index f1619567..6445554b 100644 --- a/Wino.Core.Domain/Interfaces/ITranslationService.cs +++ b/Wino.Core.Domain/Interfaces/ITranslationService.cs @@ -9,4 +9,5 @@ public interface ITranslationService : IInitializeAsync { Task InitializeLanguageAsync(AppLanguage language, bool ignoreCurrentLanguageCheck = false); List GetAvailableLanguages(); + AppLanguageModel CurrentLanguageModel { get; } } diff --git a/Wino.Core.Domain/Models/Translations/AppLanguageModel.cs b/Wino.Core.Domain/Models/Translations/AppLanguageModel.cs index 1c779508..58b6d859 100644 --- a/Wino.Core.Domain/Models/Translations/AppLanguageModel.cs +++ b/Wino.Core.Domain/Models/Translations/AppLanguageModel.cs @@ -2,4 +2,4 @@ namespace Wino.Core.Domain.Models.Translations; -public record AppLanguageModel(AppLanguage Language, string DisplayName); +public record AppLanguageModel(AppLanguage Language, string DisplayName, string Code); diff --git a/Wino.Core.UWP/Helpers/XamlHelpers.cs b/Wino.Core.UWP/Helpers/XamlHelpers.cs index 98d54e56..721b45d5 100644 --- a/Wino.Core.UWP/Helpers/XamlHelpers.cs +++ b/Wino.Core.UWP/Helpers/XamlHelpers.cs @@ -36,9 +36,6 @@ public static class XamlHelpers }; } - - - public static Visibility ReverseBoolToVisibilityConverter(bool value) => value ? Visibility.Collapsed : Visibility.Visible; public static Visibility ReverseVisibilityConverter(Visibility visibility) => visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible; public static bool ReverseBoolConverter(bool value) => !value; @@ -125,7 +122,7 @@ public static class XamlHelpers public static string GetCreationDateString(DateTime date, bool prefer24HourTime) { var localTime = date.ToLocalTime(); - return $"{localTime.ToLongDateString()} {(prefer24HourTime ? localTime.ToString(TwentyFourHourTimeFormat) : localTime.ToString(TwelveHourTimeFormat))}"; + return $"{localTime.ToString("D", CultureInfo.DefaultThreadCurrentUICulture)} {(prefer24HourTime ? localTime.ToString(TwentyFourHourTimeFormat) : localTime.ToString(TwelveHourTimeFormat))}"; } public static string GetMailGroupDateString(object groupObject) { @@ -152,7 +149,10 @@ public static class XamlHelpers else if (dateTimeValue == DateTime.Today.AddDays(-1)) return Translator.Yesterday; else - return dateTimeValue.ToLongDateString(); + { + return dateTimeValue.ToString("D", CultureInfo.DefaultThreadCurrentUICulture); + } + } else return dateObject.ToString(); diff --git a/Wino.Core.UWP/WinoApplication.cs b/Wino.Core.UWP/WinoApplication.cs index bd99d917..25bf20b2 100644 --- a/Wino.Core.UWP/WinoApplication.cs +++ b/Wino.Core.UWP/WinoApplication.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Threading.Tasks; +using CommunityToolkit.Mvvm.Messaging; using Microsoft.Extensions.DependencyInjection; using Nito.AsyncEx; using Serilog; @@ -11,6 +13,7 @@ using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.AppService; using Windows.ApplicationModel.Core; using Windows.Foundation.Metadata; +using Windows.Globalization; using Windows.Storage; using Windows.UI; using Windows.UI.Core.Preview; @@ -20,11 +23,13 @@ using Windows.UI.Xaml.Controls; using Wino.Activation; using Wino.Core.Domain; using Wino.Core.Domain.Interfaces; +using Wino.Core.Domain.Models.Translations; +using Wino.Messaging.Client.Shell; using Wino.Services; namespace Wino.Core.UWP; -public abstract class WinoApplication : Application +public abstract class WinoApplication : Application, IRecipient { public new static WinoApplication Current => (WinoApplication)Application.Current; public const string WinoLaunchLogPrefix = "[Wino Launch] "; @@ -215,8 +220,6 @@ public abstract class WinoApplication : Application CoreApplication.EnablePrelaunch(true); } - - public virtual async void OnResuming(object sender, object e) { // App Service connection was lost on suspension. @@ -245,4 +248,16 @@ public abstract class WinoApplication : Application string logFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, Constants.ClientLogFile); LogInitializer.SetupLogger(logFilePath); } + + public virtual void OnLanguageChanged(AppLanguageModel languageModel) + { + var newCulture = new CultureInfo(languageModel.Code); + + ApplicationLanguages.PrimaryLanguageOverride = languageModel.Code; + + CultureInfo.DefaultThreadCurrentCulture = newCulture; + CultureInfo.DefaultThreadCurrentUICulture = newCulture; + } + + public void Receive(LanguageChanged message) => OnLanguageChanged(TranslationService.CurrentLanguageModel); } diff --git a/Wino.Mail/App.xaml.cs b/Wino.Mail/App.xaml.cs index 69759f0f..35e78dc0 100644 --- a/Wino.Mail/App.xaml.cs +++ b/Wino.Mail/App.xaml.cs @@ -24,12 +24,14 @@ using Wino.Mail.Services; using Wino.Mail.ViewModels; using Wino.Messaging.Client.Connection; using Wino.Messaging.Client.Navigation; +using Wino.Messaging.Client.Shell; using Wino.Messaging.Server; using Wino.Services; namespace Wino; -public sealed partial class App : WinoApplication, IRecipient +public sealed partial class App : WinoApplication, + IRecipient { private BackgroundTaskDeferral connectionBackgroundTaskDeferral; private BackgroundTaskDeferral toastActionBackgroundTaskDeferral; @@ -40,7 +42,8 @@ public sealed partial class App : WinoApplication, IRecipient(this); + WeakReferenceMessenger.Default.Register(this); } public override async void OnResuming(object sender, object e) diff --git a/Wino.Services/TranslationService.cs b/Wino.Services/TranslationService.cs index d5d80571..de977f42 100644 --- a/Wino.Services/TranslationService.cs +++ b/Wino.Services/TranslationService.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text.Json; using System.Threading.Tasks; using CommunityToolkit.Mvvm.Messaging; @@ -20,6 +21,8 @@ public class TranslationService : ITranslationService private readonly IPreferencesService _preferencesService; private bool isInitialized = false; + public AppLanguageModel CurrentLanguageModel { get; private set; } + public TranslationService(IPreferencesService preferencesService) { _preferencesService = preferencesService; @@ -56,6 +59,7 @@ public class TranslationService : ITranslationService } _preferencesService.CurrentLanguage = language; + CurrentLanguageModel = GetAvailableLanguages().FirstOrDefault(a => a.Language == language); isInitialized = true; WeakReferenceMessenger.Default.Send(new LanguageChanged()); @@ -65,20 +69,20 @@ public class TranslationService : ITranslationService { return [ - new AppLanguageModel(AppLanguage.Chinese, "Chinese"), - new AppLanguageModel(AppLanguage.Czech, "Czech"), - new AppLanguageModel(AppLanguage.Deutsch, "Deutsch"), - new AppLanguageModel(AppLanguage.English, "English"), - new AppLanguageModel(AppLanguage.French, "French"), - new AppLanguageModel(AppLanguage.Italian, "Italian"), - new AppLanguageModel(AppLanguage.Greek, "Greek"), - new AppLanguageModel(AppLanguage.Indonesian, "Indonesian"), - new AppLanguageModel(AppLanguage.Polish, "Polski"), - new AppLanguageModel(AppLanguage.PortugeseBrazil, "Portugese-Brazil"), - new AppLanguageModel(AppLanguage.Russian, "Russian"), - new AppLanguageModel(AppLanguage.Romanian, "Romanian"), - new AppLanguageModel(AppLanguage.Spanish, "Spanish"), - new AppLanguageModel(AppLanguage.Turkish, "Turkish") + new AppLanguageModel(AppLanguage.Chinese, "Chinese", "zh-CN"), + new AppLanguageModel(AppLanguage.Czech, "Czech", "cs-CZ"), + new AppLanguageModel(AppLanguage.Deutsch, "Deutsch", "de-DE"), + new AppLanguageModel(AppLanguage.English, "English", "en-US"), + new AppLanguageModel(AppLanguage.French, "French", "fr-FR"), + new AppLanguageModel(AppLanguage.Italian, "Italian", "it-IT"), + new AppLanguageModel(AppLanguage.Greek, "Greek", "el-GR"), + new AppLanguageModel(AppLanguage.Indonesian, "Indonesian", "id-ID"), + new AppLanguageModel(AppLanguage.Polish, "Polski", "pl-PL"), + new AppLanguageModel(AppLanguage.PortugeseBrazil, "Portugese-Brazil", "pt-BR"), + new AppLanguageModel(AppLanguage.Russian, "Russian", "ru-RU"), + new AppLanguageModel(AppLanguage.Romanian, "Romanian", "ro-RO"), + new AppLanguageModel(AppLanguage.Spanish, "Spanish", "es-ES"), + new AppLanguageModel(AppLanguage.Turkish, "Turkish", "tr-TR") ]; } }