using CMS_CORE_Library.Models; using Step.Model.DTOModels; using Step.NC; using System.Collections.Generic; using System.Linq; using System.Web.Http; using static CMS_CORE_Library.Models.DataStructures; using static Step.Config.ServerConfig; using static Step.Model.Constants; using static Step.Utils.LanguageController; namespace Step.Controllers.WebApi { [RoutePrefix("api/language")] public class LanguageController : ApiController { [Route("languages"), HttpGet] public IHttpActionResult GetLanguageList() { List 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 translations = GetTranslationsFromFile(language); // Get Maintenance, Softkey, Plc Alarm translations Dictionary maintenanceNames = GetLocalizeMaintenanceName(language); Dictionary softKeysNames = GetLocalizedSoftKeysNames(language); Dictionary alarmsNames = GetPlcAlarmsTranslations(language); Dictionary headsNames = GetLocalizedHeadsNames(language); Dictionary coolingNames = GetCoolingTranslations(language); Dictionary magazinesNames = GetMagazineTranslations(language); // Concat maintenances dictionary with translations dictionary translations = translations.Concat(maintenanceNames).ToDictionary(x => x.Key, x => x.Value); // Softkeys translations = translations.Concat(softKeysNames).ToDictionary(x => x.Key, x => x.Value); // Alarms translations = translations.Concat(alarmsNames).ToDictionary(x => x.Key, x => x.Value); // Heads translations = translations.Concat(headsNames).ToDictionary(x => x.Key, x => x.Value); // Cooling foreach (var coolingItem in coolingNames) { translations[coolingItem.Key] = coolingItem.Value; } // Magazines translations = translations.Concat(magazinesNames).ToDictionary(x => x.Key, x => x.Value); if (translations == null) return InternalServerError(); return Ok(translations); } private static Dictionary GetLocalizeMaintenanceName(string language) { Dictionary localizedStrings = new Dictionary(); // Get localized name localizedStrings = MaintenancesConfig .ToDictionary( x => MAINTENANCE_PREFIX_ID + x.Id.ToString(), // Prefix + maintenance id x => GetValueFromLocalizationList(x.LocalizedName, language, "Maintenance_" + x.Id) ); // Get localized description localizedStrings = localizedStrings.Concat(MaintenancesConfig .ToDictionary( x => MAINTENANCE_DESC_PREFIX_ID + x.Id.ToString(), // Description Prefix + maintenance id x => GetValueFromLocalizationList(x.LocalizedDescription, language, "Maintenance_" + x.Id) )).ToDictionary(x => x.Key, x => x.Value); return localizedStrings; } private static Dictionary GetLocalizedSoftKeysNames(string language) { return SoftKeysConfig .ToDictionary( x => SOFTKEY_PREFIX_ID + x.Id.ToString(), // PREFIX + softkey Id x => GetValueFromLocalizationList(x.LocalizedNames, language, "SoftKey_" + x.Id) ); } private static Dictionary GetLocalizedHeadsNames(string language) { return HeadsConfig .ToDictionary( x => SOFTKEY_HEAD_ID + x.Id.ToString(), // PREFIX + softkey Id x => GetValueFromLocalizationList(x.LocalizedNames, language, "Head_" + x.Id) ); } private static string GetValueFromLocalizationList(Dictionary 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; } private static Dictionary GetPlcAlarmsTranslations(string language) { using (NcHandler ncHandler = new NcHandler()) { CmsError cmsError = ncHandler.Connect(); Dictionary returnValue = new Dictionary(); Dictionary messages = new Dictionary(); // Read data from CN cmsError = ncHandler.numericalControl.NC_GetTranslatedPlcMessages(language, ref messages); // Id start from 1 for (int i = 1; i <= 1024; i++) { // Get configurated alarms var tmpAlarmConfig = InitialAlarmsConfig.Where(x => x.PlcId == i).FirstOrDefault(); // Default string string message = "Alarm_id_" + i + ": Message not configurated"; // If is configurated if (tmpAlarmConfig != null) { // Find translated string message = messages.Where(x => x.Key == tmpAlarmConfig.AlarmId).FirstOrDefault().Value; if (message == null) message = "Alarm_id_" + i + ": Message not found"; } // Add to dictionary returnValue.Add( ALARM_PREFIX + i, message ); } return returnValue; } } private static Dictionary GetCoolingTranslations(string language) { Dictionary dictionary = new Dictionary(); // Find translated key var key = ToolManagerConfig.CooligsTranslations.Cooling.Where(x => x.Key == language).FirstOrDefault(); if (key.Value != null) { // Add translated value \ dictionary.Add("tooling_family_param_cooling", key.Value); dictionary.Add("tooling_equipment_param_cooling", key.Value); } key = ToolManagerConfig.CooligsTranslations.Cooling1.Where(x => x.Key == language).FirstOrDefault(); if (key.Value != null) { dictionary.Add("tooling_family_param_cooling1", key.Value); dictionary.Add("tooling_equipment_param_cooling1", key.Value); } key = ToolManagerConfig.CooligsTranslations.Cooling2.Where(x => x.Key == language).FirstOrDefault(); if (key.Value != null) { dictionary.Add("tooling_family_param_cooling2", key.Value); dictionary.Add("tooling_equipment_param_cooling2", key.Value); } key = ToolManagerConfig.CooligsTranslations.Cooling3.Where(x => x.Key == language).FirstOrDefault(); if (key.Value != null) { dictionary.Add("tooling_family_param_cooling3", key.Value); dictionary.Add("tooling_equipment_param_cooling3", key.Value); } key = ToolManagerConfig.CooligsTranslations.Cooling4.Where(x => x.Key == language).FirstOrDefault(); if (key.Value != null) { dictionary.Add("tooling_family_param_cooling4", key.Value); dictionary.Add("tooling_equipment_param_cooling4", key.Value); } key = ToolManagerConfig.CooligsTranslations.Cooling5.Where(x => x.Key == language).FirstOrDefault(); if (key.Value != null) { dictionary.Add("tooling_family_param_cooling5", key.Value); dictionary.Add("tooling_equipment_param_cooling5", key.Value); } key = ToolManagerConfig.CooligsTranslations.Cooling6.Where(x => x.Key == language).FirstOrDefault(); if (key.Value != null) { dictionary.Add("tooling_family_param_cooling6", key.Value); dictionary.Add("tooling_equipment_param_cooling6", key.Value); } key = ToolManagerConfig.CooligsTranslations.Cooling7.Where(x => x.Key == language).FirstOrDefault(); if (key.Value != null) { dictionary.Add("tooling_family_param_cooling7", key.Value); dictionary.Add("tooling_equipment_param_cooling7", key.Value); } return dictionary; } private static Dictionary GetMagazineTranslations(string language) { Dictionary translatedNames = new Dictionary(); foreach (var mag in ToolManagerConfig.MagazineNames) { translatedNames.Add( "magazine_name_" + mag.MagazineId, GetValueFromLocalizationList(mag.LocalizedNames, language, "Magazine_" + mag.MagazineId) ); } return translatedNames; } } }