2017-07-05 17:21:52 +02:00
|
|
|
|
/*
|
2024-03-05 18:13:14 +01:00
|
|
|
|
* Copyright (c) 2023 ETH Zürich, IT Services
|
2017-07-05 17:21:52 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-03-09 17:35:48 +01:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.I18n.Contracts;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2017-07-05 17:21:52 +02:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.I18n
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// <summary>
|
2020-03-09 17:35:48 +01:00
|
|
|
|
/// Default implementation of <see cref="IText"/>.
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// </summary>
|
2017-07-06 18:18:39 +02:00
|
|
|
|
public class Text : IText
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2017-10-10 10:17:28 +02:00
|
|
|
|
private ILogger logger;
|
2020-03-09 17:35:48 +01:00
|
|
|
|
private IDictionary<TextKey, string> text;
|
2017-07-06 10:56:03 +02:00
|
|
|
|
|
2017-10-10 10:17:28 +02:00
|
|
|
|
public Text(ILogger logger)
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2017-10-10 10:17:28 +02:00
|
|
|
|
this.logger = logger;
|
2020-03-09 17:35:48 +01:00
|
|
|
|
this.text = new Dictionary<TextKey, string>();
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 15:35:22 +02:00
|
|
|
|
public string Get(TextKey key)
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2020-03-09 17:35:48 +01:00
|
|
|
|
return text.ContainsKey(key) ? text[key] : $"Could not find text for key '{key}'!";
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
2017-10-10 10:17:28 +02:00
|
|
|
|
|
2020-03-09 17:35:48 +01:00
|
|
|
|
public void Initialize()
|
2017-10-10 10:17:28 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-09 17:35:48 +01:00
|
|
|
|
var assembly = Assembly.GetAssembly(typeof(Text));
|
|
|
|
|
var data = default(Stream);
|
|
|
|
|
var language = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.ToLower();
|
|
|
|
|
var path = $"{typeof(Text).Namespace}.Data";
|
|
|
|
|
var resource = default(ITextResource);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger.Debug($"System language is '{language}', trying to load data...");
|
|
|
|
|
data = assembly.GetManifestResourceStream($"{path}.{language}.xml");
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data == default(Stream))
|
|
|
|
|
{
|
|
|
|
|
logger.Warn($"Could not find data for language '{language}'! Loading default language 'en'...");
|
|
|
|
|
data = assembly.GetManifestResourceStream($"{path}.en.xml");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (data)
|
|
|
|
|
{
|
|
|
|
|
resource = new XmlTextResource(data);
|
|
|
|
|
text = resource.LoadText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Debug("Data successfully loaded.");
|
2017-10-10 10:17:28 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-03-09 17:35:48 +01:00
|
|
|
|
logger.Error("Failed to initialize data!", e);
|
2017-10-10 10:17:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|