Files
Wino-Mail/Wino.Services/TranslationService.cs
Aleh Khantsevich 2ec05ea7cc UWP .NET9 (#555)
* Ground work for NET9 UWP switch.

* Add launch settings for Wino.Mail

* Added new test WAP project

* fix platforms in slnx solution

* ManagePackageVersionsCentrally set default

* Fixing assets and couple issues with the new packaging project.

* Add back markdown

* Fix nuget warnings

* FIx error in WAP about build tools

* Add build.props with default language preview

* Some AOT compilation progress.

* More AOT stuff.

* Remove deprecated protocol auth activation handler.

* Fix remaining protocol handler for google auth.

* Even more AOT

* More more AOT fixes

* Fix a few more AOT warnings

* Fix signature editor AOT

* Fix composer and renderer AOT JSON

* Outlook Sync AOT

* Fixing bundle generation and package signing.

---------

Co-authored-by: Burak Kaan Köse <bkaankose@outlook.com>
2025-02-14 01:43:52 +01:00

86 lines
3.5 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Messaging;
using Serilog;
using Wino.Core.Domain;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Translations;
using Wino.Messaging.Client.Shell;
namespace Wino.Services
{
public class TranslationService : ITranslationService
{
public const AppLanguage DefaultAppLanguage = AppLanguage.English;
private ILogger _logger = Log.ForContext<TranslationService>();
private readonly IPreferencesService _preferencesService;
private bool isInitialized = false;
public TranslationService(IPreferencesService preferencesService)
{
_preferencesService = preferencesService;
}
// Initialize default language with ignoring current language check.
public Task InitializeAsync() => InitializeLanguageAsync(_preferencesService.CurrentLanguage, ignoreCurrentLanguageCheck: true);
public async Task InitializeLanguageAsync(AppLanguage language, bool ignoreCurrentLanguageCheck = false)
{
if (!ignoreCurrentLanguageCheck && _preferencesService.CurrentLanguage == language)
{
_logger.Warning("Changing language is ignored because current language and requested language are same.");
return;
}
if (ignoreCurrentLanguageCheck && isInitialized) return;
var currentDictionary = Translator.Resources;
await using var resourceStream = Core.Domain.Translations.WinoTranslationDictionary.GetLanguageStream(language);
var streamValue = await new StreamReader(resourceStream).ReadToEndAsync().ConfigureAwait(false);
var translationLookups = JsonSerializer.Deserialize(streamValue, BasicTypesJsonContext.Default.DictionaryStringString);
// Insert new translation key-value pairs.
// Overwrite existing values for the same keys.
foreach (var pair in translationLookups)
{
// Replace existing value.
currentDictionary[pair.Key] = pair.Value;
}
_preferencesService.CurrentLanguage = language;
isInitialized = true;
WeakReferenceMessenger.Default.Send(new LanguageChanged());
}
public List<AppLanguageModel> GetAvailableLanguages()
{
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")
];
}
}
}