2017-07-05 17:21:52 +02:00
|
|
|
|
/*
|
2018-01-16 08:24:00 +01:00
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
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;
|
2017-07-06 18:18:39 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
2017-10-10 10:17:28 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2017-07-05 17:21:52 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Core.I18n
|
|
|
|
|
{
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Default implementation of the <see cref="IText"/> module.
|
|
|
|
|
/// </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 IDictionary<TextKey, string> cache = new Dictionary<TextKey, string>();
|
|
|
|
|
private ILogger logger;
|
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;
|
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
|
|
|
|
{
|
|
|
|
|
return cache.ContainsKey(key) ? cache[key] : $"Could not find string for key '{key}'!";
|
|
|
|
|
}
|
2017-10-10 10:17:28 +02:00
|
|
|
|
|
|
|
|
|
public void Initialize(ITextResource resource)
|
|
|
|
|
{
|
|
|
|
|
if (resource == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(resource));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
cache = resource.LoadText() ?? new Dictionary<TextKey, string>();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to load text data from provided resource!", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|