Revise TranslationMap, UserSettings
- Move Master.txt translation file to Translations/en/Translation.txt
- Threadsafe MatterControlTranslationMap initialization
- Refactor UserSettings:
- Threadsafe initialization
- Concise initialization
- UseTryGetValue for dictionary lookups
- Use backing fields
- Cache frequently accessed Language value as local property
This commit is contained in:
parent
f5488b04b9
commit
8600a35576
4 changed files with 66 additions and 58 deletions
|
|
@ -38,29 +38,30 @@ namespace MatterHackers.Localizations
|
|||
{
|
||||
private static TranslationMap MatterControlTranslationMap;
|
||||
|
||||
private static readonly object syncRoot = new object();
|
||||
|
||||
static LocalizedString()
|
||||
{
|
||||
lock(syncRoot)
|
||||
{
|
||||
if (MatterControlTranslationMap == null)
|
||||
{
|
||||
MatterControlTranslationMap = new TranslationMap("Translations", UserSettings.Instance.Language);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string Get(string englishText)
|
||||
{
|
||||
string language = UserSettings.Instance.get("Language");
|
||||
if (language == null)
|
||||
{
|
||||
language = "en";
|
||||
UserSettings.Instance.set("Language", "en");
|
||||
}
|
||||
|
||||
if (MatterControlTranslationMap == null)
|
||||
{
|
||||
string pathToTranslationsFolder = "Translations";
|
||||
MatterControlTranslationMap = new TranslationMap(pathToTranslationsFolder, language);
|
||||
}
|
||||
#if DEBUG_SHOW_TRANSLATED_STRINGS && DEBUG
|
||||
return "El " + englishText + " o";
|
||||
return "El " + englishText + " o";
|
||||
#endif
|
||||
return MatterControlTranslationMap.Translate(englishText);
|
||||
}
|
||||
|
||||
public static void ResetTranslationMap()
|
||||
{
|
||||
MatterControlTranslationMap = null;
|
||||
MatterControlTranslationMap = new TranslationMap("Translations", UserSettings.Instance.Language);
|
||||
}
|
||||
|
||||
public static string Localize(this string englishString)
|
||||
|
|
|
|||
|
|
@ -1,17 +1,31 @@
|
|||
using System.Collections.Generic;
|
||||
using MatterHackers.MatterControl.DataStorage;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MatterHackers.MatterControl
|
||||
{
|
||||
public class UserSettings
|
||||
{
|
||||
private static UserSettings globalInstance = null;
|
||||
public Dictionary<string, DataStorage.UserSetting> settingsDictionary;
|
||||
|
||||
private UserSettingsFields fields = new UserSettingsFields();
|
||||
private static readonly object syncRoot = new object();
|
||||
|
||||
public UserSettingsFields Fields
|
||||
private Dictionary<string, UserSetting> settingsDictionary;
|
||||
|
||||
private UserSettings()
|
||||
{
|
||||
get { return fields; }
|
||||
// Load the UserSettings from the database
|
||||
settingsDictionary = (from setting in Datastore.Instance.dbSQLite.Query<UserSetting>("SELECT * FROM UserSetting;")
|
||||
select setting).ToDictionary(s => s.Name, s => s);
|
||||
|
||||
// Set English as default language if unset
|
||||
if (string.IsNullOrEmpty(this.get("Language")))
|
||||
{
|
||||
UserSettings.Instance.set("Language", "en");
|
||||
}
|
||||
|
||||
// Propagate Language to local property
|
||||
this.Language = this.get("Language");
|
||||
}
|
||||
|
||||
public static UserSettings Instance
|
||||
|
|
@ -20,63 +34,56 @@ namespace MatterHackers.MatterControl
|
|||
{
|
||||
if (globalInstance == null)
|
||||
{
|
||||
globalInstance = new UserSettings();
|
||||
globalInstance.LoadData();
|
||||
lock(syncRoot)
|
||||
{
|
||||
if (globalInstance == null)
|
||||
{
|
||||
globalInstance = new UserSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return globalInstance;
|
||||
}
|
||||
}
|
||||
|
||||
public string Language { get; private set; }
|
||||
|
||||
public UserSettingsFields Fields { get; private set; } = new UserSettingsFields();
|
||||
|
||||
public string get(string key)
|
||||
{
|
||||
string result;
|
||||
if (settingsDictionary.ContainsKey(key))
|
||||
{
|
||||
result = settingsDictionary[key].Value;
|
||||
UserSetting userSetting;
|
||||
if (settingsDictionary.TryGetValue(key, out userSetting))
|
||||
{
|
||||
return userSetting.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
return result;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void set(string key, string value)
|
||||
{
|
||||
DataStorage.UserSetting setting;
|
||||
if (settingsDictionary.ContainsKey(key))
|
||||
{
|
||||
setting = settingsDictionary[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
setting = new DataStorage.UserSetting();
|
||||
setting.Name = key;
|
||||
UserSetting setting;
|
||||
|
||||
if(!settingsDictionary.TryGetValue(key, out setting))
|
||||
{
|
||||
// If the setting for the given key doesn't exist, create it
|
||||
setting = new UserSetting()
|
||||
{
|
||||
Name = key
|
||||
};
|
||||
settingsDictionary[key] = setting;
|
||||
}
|
||||
|
||||
// Special case to propagate Language to local property on assignment
|
||||
if(key == "Language")
|
||||
{
|
||||
this.Language = value;
|
||||
}
|
||||
|
||||
setting.Value = value;
|
||||
setting.Commit();
|
||||
}
|
||||
|
||||
private void LoadData()
|
||||
{
|
||||
IEnumerable<DataStorage.UserSetting> settingsList = GetApplicationSettings();
|
||||
settingsDictionary = new Dictionary<string, DataStorage.UserSetting>();
|
||||
foreach (DataStorage.UserSetting s in settingsList)
|
||||
{
|
||||
settingsDictionary[s.Name] = s;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<DataStorage.UserSetting> GetApplicationSettings()
|
||||
{
|
||||
//Retrieve a list of settings from the Datastore
|
||||
string query = string.Format("SELECT * FROM UserSetting;");
|
||||
IEnumerable<DataStorage.UserSetting> result = (IEnumerable<DataStorage.UserSetting>)DataStorage.Datastore.Instance.dbSQLite.Query<DataStorage.UserSetting>(query);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +1 @@
|
|||
Subproject commit 9585f7795bba4a88274337dcdc159fd5ff4afe8e
|
||||
Subproject commit 241e12005ff538a95cd6ad2cb917d8b4d04b27a3
|
||||
Loading…
Add table
Add a link
Reference in a new issue