mirror of
https://github.com/bitwarden/mobile
synced 2026-01-18 16:33:15 +00:00
[EC-1002] [BEEEP] Add ability to change language in app (#2299)
* EC-1002 BEEEP Added ability to change language in app * EC-1002 fix format * EC-1002 Renamed IPreferencesStorageService to ISynchronousStorageService * EC-1002 Moved get/set Locale to the StateService and added the StorageMediatorService to a new way to interact with the storage. Later the StateService will only interact with this mediator instead of directly with the storage services, with this we have more control inside the mediator and we can have both sync and async methods to interact with storages handled by the mediator
This commit is contained in:
committed by
GitHub
parent
5164762f2e
commit
470e08f165
@@ -51,32 +51,32 @@ namespace Bit.App.Services
|
||||
["bg"] = "български",
|
||||
["ca"] = "català",
|
||||
["cs"] = "čeština",
|
||||
["da"] = "dansk",
|
||||
["da"] = "Dansk",
|
||||
["de"] = "Deutsch",
|
||||
["el"] = "Ελληνικά",
|
||||
["en"] = "English",
|
||||
["en-GB"] = "English (British)",
|
||||
["eo"] = "Esperanto",
|
||||
["es"] = "español",
|
||||
["es"] = "Español",
|
||||
["et"] = "eesti",
|
||||
["fa"] = "فارسی",
|
||||
["fi"] = "suomi",
|
||||
["fr"] = "français",
|
||||
["fr"] = "Français",
|
||||
["he"] = "עברית",
|
||||
["hi"] = "हिन्दी",
|
||||
["hr"] = "hrvatski",
|
||||
["hu"] = "magyar",
|
||||
["id"] = "Bahasa Indonesia",
|
||||
["it"] = "italiano",
|
||||
["it"] = "Italiano",
|
||||
["ja"] = "日本語",
|
||||
["ko"] = "한국어",
|
||||
["lv"] = "Latvietis",
|
||||
["ml"] = "മലയാളം",
|
||||
["nb"] = "norsk (bokmål)",
|
||||
["nl"] = "Nederlands",
|
||||
["pl"] = "polski",
|
||||
["pt-BR"] = "português do Brasil",
|
||||
["pt-PT"] = "português",
|
||||
["pl"] = "Polski",
|
||||
["pt-BR"] = "Português do Brasil",
|
||||
["pt-PT"] = "Português",
|
||||
["ro"] = "română",
|
||||
["ru"] = "русский",
|
||||
["sk"] = "slovenčina",
|
||||
@@ -100,10 +100,16 @@ namespace Bit.App.Services
|
||||
throw new Exception("I18n already inited.");
|
||||
}
|
||||
_inited = true;
|
||||
SetCurrentCulture(culture);
|
||||
}
|
||||
|
||||
public void SetCurrentCulture(CultureInfo culture)
|
||||
{
|
||||
if (culture != null)
|
||||
{
|
||||
Culture = culture;
|
||||
}
|
||||
|
||||
AppResources.Culture = Culture;
|
||||
Thread.CurrentThread.CurrentCulture = Culture;
|
||||
Thread.CurrentThread.CurrentUICulture = Culture;
|
||||
|
||||
@@ -6,7 +6,7 @@ using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class PreferencesStorageService : IStorageService
|
||||
public class PreferencesStorageService : IStorageService, ISynchronousStorageService
|
||||
{
|
||||
public static string KeyFormat = "bwPreferencesStorage:{0}";
|
||||
|
||||
@@ -22,57 +22,72 @@ namespace Bit.App.Services
|
||||
_sharedName = sharedName;
|
||||
}
|
||||
|
||||
public Task<T> GetAsync<T>(string key)
|
||||
public Task<T> GetAsync<T>(string key) => Task.FromResult(Get<T>(key));
|
||||
|
||||
public Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
Save(key, obj);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
Remove(key);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
var formattedKey = string.Format(KeyFormat, key);
|
||||
if (!Xamarin.Essentials.Preferences.ContainsKey(formattedKey, _sharedName))
|
||||
{
|
||||
return Task.FromResult(default(T));
|
||||
return default(T);
|
||||
}
|
||||
|
||||
var objType = typeof(T);
|
||||
if (objType == typeof(string))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string), _sharedName);
|
||||
return Task.FromResult((T)(object)val);
|
||||
return (T)(object)val;
|
||||
}
|
||||
else if (objType == typeof(bool) || objType == typeof(bool?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(bool), _sharedName);
|
||||
return Task.FromResult(ChangeType<T>(val));
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else if (objType == typeof(int) || objType == typeof(int?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(int), _sharedName);
|
||||
return Task.FromResult(ChangeType<T>(val));
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else if (objType == typeof(long) || objType == typeof(long?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(long), _sharedName);
|
||||
return Task.FromResult(ChangeType<T>(val));
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else if (objType == typeof(double) || objType == typeof(double?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(double), _sharedName);
|
||||
return Task.FromResult(ChangeType<T>(val));
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else if (objType == typeof(DateTime) || objType == typeof(DateTime?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(DateTime), _sharedName);
|
||||
return Task.FromResult(ChangeType<T>(val));
|
||||
return ChangeType<T>(val);
|
||||
}
|
||||
else
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string), _sharedName);
|
||||
return Task.FromResult(JsonConvert.DeserializeObject<T>(val, _jsonSettings));
|
||||
return JsonConvert.DeserializeObject<T>(val, _jsonSettings);
|
||||
}
|
||||
}
|
||||
|
||||
public Task SaveAsync<T>(string key, T obj)
|
||||
public void Save<T>(string key, T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return RemoveAsync(key);
|
||||
Remove(key);
|
||||
return;
|
||||
}
|
||||
|
||||
var formattedKey = string.Format(KeyFormat, key);
|
||||
@@ -106,17 +121,15 @@ namespace Bit.App.Services
|
||||
Xamarin.Essentials.Preferences.Set(formattedKey, JsonConvert.SerializeObject(obj, _jsonSettings),
|
||||
_sharedName);
|
||||
}
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
public void Remove(string key)
|
||||
{
|
||||
var formattedKey = string.Format(KeyFormat, key);
|
||||
if (Xamarin.Essentials.Preferences.ContainsKey(formattedKey, _sharedName))
|
||||
{
|
||||
Xamarin.Essentials.Preferences.Remove(formattedKey, _sharedName);
|
||||
}
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
private static T ChangeType<T>(object value)
|
||||
|
||||
Reference in New Issue
Block a user