80 lines
2.9 KiB
C#
80 lines
2.9 KiB
C#
using Step.Model.ConfigModels;
|
|
using Step.Model.DTOModels;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web.Http;
|
|
using static Step.Config.ServerConfig;
|
|
using static Step.Utils.Constants;
|
|
using static Step.Utils.LanguageController;
|
|
|
|
namespace Step.Controllers.WebApi
|
|
{
|
|
[RoutePrefix("api/language")]
|
|
public class LanguageController : ApiController
|
|
{
|
|
[Route("languages"), HttpGet]
|
|
public IHttpActionResult GetLanguageList()
|
|
{
|
|
List<DTOLanguageModel> availableLanguages = GetLanguageListFromDirectory();
|
|
if (availableLanguages == null)
|
|
return NotFound();
|
|
|
|
return Ok(availableLanguages);
|
|
}
|
|
|
|
[Route("{language}"), HttpGet()]
|
|
public IHttpActionResult GetTranslations(string language)
|
|
{
|
|
if (!IsValidLanguage(language))
|
|
return BadRequest("Language not exists");
|
|
|
|
if (!LanguageIsAvailable(language))
|
|
return NotFound();
|
|
|
|
Dictionary<string, string> translations = GetTranslationsFromFile(language);
|
|
|
|
// Get Maintenance translations
|
|
Dictionary<string, string> maintenance = GetLocalizeMaintenanceName(language);
|
|
Dictionary<string, string> softKeys = GetLocalizedSoftKeysNames(language);
|
|
|
|
// Concat maintenances dictionary with translations dictionary
|
|
translations = translations.Concat(maintenance).ToDictionary(x => x.Key, x => x.Value);
|
|
translations = translations.Concat(softKeys).ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
if (translations == null)
|
|
return InternalServerError();
|
|
|
|
return Ok(translations);
|
|
}
|
|
|
|
public static Dictionary<string, string> GetLocalizeMaintenanceName(string language)
|
|
{
|
|
return MaintenancesConfig
|
|
.ToDictionary(
|
|
x => MAINTENANCE_PREFIX_ID + x.Id.ToString(),
|
|
x => GetValueFromMaintenanceNameList(x.LocalizedNames, language, "Maintenance_" + x.Id)
|
|
);
|
|
}
|
|
|
|
|
|
public static Dictionary<string, string> GetLocalizedSoftKeysNames(string language)
|
|
{
|
|
return SoftKeysConfig
|
|
.ToDictionary(
|
|
x => SOFTKEY_PREFIX_ID + x.Id.ToString(),
|
|
x => GetValueFromMaintenanceNameList(x.LocalizedNames, language, "SoftKey_" + x.Id)
|
|
);
|
|
}
|
|
|
|
public static string GetValueFromMaintenanceNameList(Dictionary<string, string> localizedNames, string language, string defaultString)
|
|
{
|
|
// Find text from names by language id
|
|
var value = localizedNames.Where(y => y.Key == language).FirstOrDefault();
|
|
// Set default value
|
|
if (value.Key == null)
|
|
return defaultString;
|
|
else
|
|
return value.Value;
|
|
}
|
|
}
|
|
} |