2019-04-11 22:33:10 +03:00
|
|
|
|
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
|
|
|
|
|
{
|
2019-04-11 22:43:13 +03:00
|
|
|
|
private const string ResourceId = "Bit.App.Resources.AppResources";
|
2019-04-11 22:33:10 +03:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 22:43:13 +03:00
|
|
|
|
public string T(string id, string p1, string p2, string p3)
|
2019-04-11 22:33:10 +03:00
|
|
|
|
{
|
2019-04-11 22:43:13 +03:00
|
|
|
|
return Translate(id, p1, p2, p3);
|
2019-04-11 22:33:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 22:43:13 +03:00
|
|
|
|
public string Translate(string id, string p1, string p2, string p3)
|
2019-04-11 22:33:10 +03:00
|
|
|
|
{
|
|
|
|
|
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}}}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-11 22:43:13 +03:00
|
|
|
|
if(p1 == null && p2 == null && p3 == null)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else if(p2 == null && p3 == null)
|
|
|
|
|
{
|
|
|
|
|
return string.Format(result, p1);
|
|
|
|
|
}
|
|
|
|
|
else if(p3 == null)
|
|
|
|
|
{
|
|
|
|
|
return string.Format(result, p1, p2);
|
|
|
|
|
}
|
|
|
|
|
return string.Format(result, p1, p2, p3);
|
2019-04-11 22:33:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|