mirror of
https://github.com/bitwarden/mobile
synced 2025-12-26 13:13:28 +00:00
i18n service
This commit is contained in:
9
src/App/Abstractions/ILocalizeService.cs
Normal file
9
src/App/Abstractions/ILocalizeService.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Bit.App.Abstractions
|
||||
{
|
||||
public interface ILocalizeService
|
||||
{
|
||||
CultureInfo GetCurrentCultureInfo();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
using Bit.App.Models;
|
||||
using Bit.App.Pages;
|
||||
using Bit.App.Resources;
|
||||
using Bit.App.Services;
|
||||
using Bit.App.Utilities;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Utilities;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.StyleSheets;
|
||||
using Xamarin.Forms.Xaml;
|
||||
@@ -12,18 +15,23 @@ namespace Bit.App
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
private readonly MobileI18nService _i18nService;
|
||||
|
||||
public App()
|
||||
{
|
||||
InitializeComponent();
|
||||
_i18nService = ServiceContainer.Resolve<II18nService>("i18nService") as MobileI18nService;
|
||||
|
||||
InitializeComponent();
|
||||
SetCulture();
|
||||
ThemeManager.SetTheme("light");
|
||||
MainPage = new TabsPage();
|
||||
|
||||
ServiceContainer.Resolve<MobilePlatformUtilsService>("platformUtilsService").Init();
|
||||
MessagingCenter.Subscribe<Application, DialogDetails>(Current, "ShowDialog", async (sender, details) =>
|
||||
{
|
||||
var confirmed = true;
|
||||
// TODO: ok text
|
||||
var confirmText = string.IsNullOrWhiteSpace(details.ConfirmText) ? "Ok" : details.ConfirmText;
|
||||
var confirmText = string.IsNullOrWhiteSpace(details.ConfirmText) ?
|
||||
AppResources.Ok : details.ConfirmText;
|
||||
if(!string.IsNullOrWhiteSpace(details.CancelText))
|
||||
{
|
||||
confirmed = await MainPage.DisplayAlert(details.Title, details.Text, confirmText,
|
||||
@@ -51,5 +59,14 @@ namespace Bit.App
|
||||
{
|
||||
// Handle when your app resumes
|
||||
}
|
||||
|
||||
private void SetCulture()
|
||||
{
|
||||
_i18nService.Init();
|
||||
// Calendars are removed by linker. ref https://bugzilla.xamarin.com/show_bug.cgi?id=59077
|
||||
new System.Globalization.ThaiBuddhistCalendar();
|
||||
new System.Globalization.HijriCalendar();
|
||||
new System.Globalization.UmAlQuraCalendar();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
39
src/App/Models/PlatformCulture.cs
Normal file
39
src/App/Models/PlatformCulture.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
|
||||
namespace Bit.App.Models
|
||||
{
|
||||
public class PlatformCulture
|
||||
{
|
||||
public PlatformCulture(string platformCultureString)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(platformCultureString))
|
||||
{
|
||||
throw new ArgumentException("Expected culture identifier.", nameof(platformCultureString));
|
||||
}
|
||||
|
||||
// .NET expects dash, not underscore
|
||||
PlatformString = platformCultureString.Replace("_", "-");
|
||||
var dashIndex = PlatformString.IndexOf("-", StringComparison.Ordinal);
|
||||
if(dashIndex > 0)
|
||||
{
|
||||
var parts = PlatformString.Split('-');
|
||||
LanguageCode = parts[0];
|
||||
LocaleCode = parts[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
LanguageCode = PlatformString;
|
||||
LocaleCode = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public string PlatformString { get; private set; }
|
||||
public string LanguageCode { get; private set; }
|
||||
public string LocaleCode { get; private set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return PlatformString;
|
||||
}
|
||||
}
|
||||
}
|
||||
67
src/App/Services/MobileI18nService.cs
Normal file
67
src/App/Services/MobileI18nService.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Bit.App.Resources;
|
||||
using Bit.Core.Abstractions;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
using System.Threading;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class MobileI18nService : II18nService
|
||||
{
|
||||
private const string ResourceId = "UsingResxLocalization.Resx.AppResources";
|
||||
|
||||
private static readonly Lazy<ResourceManager> _resourceManager = new Lazy<ResourceManager>(() =>
|
||||
new ResourceManager(ResourceId, IntrospectionExtensions.GetTypeInfo(typeof(MobileI18nService)).Assembly));
|
||||
|
||||
private readonly CultureInfo _defaultCulture = new CultureInfo("en-US");
|
||||
private bool _inited;
|
||||
|
||||
public MobileI18nService(CultureInfo systemCulture)
|
||||
{
|
||||
Culture = systemCulture;
|
||||
}
|
||||
|
||||
public CultureInfo Culture { get; set; }
|
||||
|
||||
public void Init(CultureInfo culture = null)
|
||||
{
|
||||
if(_inited)
|
||||
{
|
||||
throw new Exception("I18n already inited.");
|
||||
}
|
||||
_inited = true;
|
||||
if(culture != null)
|
||||
{
|
||||
Culture = culture;
|
||||
}
|
||||
AppResources.Culture = Culture;
|
||||
Thread.CurrentThread.CurrentCulture = Culture;
|
||||
Thread.CurrentThread.CurrentUICulture = Culture;
|
||||
}
|
||||
|
||||
public string T(string id, params string[] p)
|
||||
{
|
||||
return Translate(id, p);
|
||||
}
|
||||
|
||||
public string Translate(string id, params string[] p)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
var result = _resourceManager.Value.GetString(id, Culture);
|
||||
if(result == null)
|
||||
{
|
||||
result = _resourceManager.Value.GetString(id, _defaultCulture);
|
||||
if(result == null)
|
||||
{
|
||||
result = $"{{{id}}}";
|
||||
}
|
||||
}
|
||||
return string.Format(result, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/App/Utilities/TranslateExtension.cs
Normal file
29
src/App/Utilities/TranslateExtension.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Utilities;
|
||||
using System;
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.Xaml;
|
||||
|
||||
namespace Bit.App.Utilities
|
||||
{
|
||||
[ContentProperty("Text")]
|
||||
public class TranslateExtension : IMarkupExtension
|
||||
{
|
||||
private II18nService _i18nService;
|
||||
|
||||
public TranslateExtension()
|
||||
{
|
||||
_i18nService = ServiceContainer.Resolve<II18nService>("i18nService");
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
public string P1 { get; set; }
|
||||
public string P2 { get; set; }
|
||||
public string P3 { get; set; }
|
||||
|
||||
public object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return _i18nService.T(Id, P1, P2, P3);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user