73 lines
2.4 KiB
C#
73 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Http;
|
|
using Step.Model.ConfigModels;
|
|
using Step.Model.DTOModels;
|
|
using static Step.Config.ServerConfig;
|
|
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, MaintenancesConfig);
|
|
|
|
// Concat 2 array
|
|
translations = translations.Concat(maintenance).ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
if (translations == null)
|
|
return InternalServerError();
|
|
|
|
return Ok(translations);
|
|
}
|
|
|
|
|
|
public static Dictionary<string, string> GetLocalizeMaintenanceName(string language, List<MaintenanceConfigModel> maitenancesConfig)
|
|
{
|
|
Dictionary<string, string> returnDictionary = new Dictionary<string, string>();
|
|
|
|
return MaintenancesConfig
|
|
.ToDictionary(
|
|
x => "MANT_" + x.Id.ToString(),
|
|
x => GetValueFromMaintenanceNameList(x.LocalizedNames, language, x.Id)
|
|
);
|
|
}
|
|
|
|
public static string GetValueFromMaintenanceNameList(Dictionary<string, string> localizedNames, string language, int maintenanceId)
|
|
{
|
|
var value = localizedNames.Where(y => y.Key == language).FirstOrDefault();
|
|
|
|
if (value.Key == null)
|
|
return "Maintenance string not found for id: " + maintenanceId;
|
|
else
|
|
return value.Value;
|
|
}
|
|
}
|
|
}
|