using System.Globalization; namespace Convert_It_Online.Services { public class UrlTranslationService : IUrlTranslationService { private readonly Dictionary> _areaTranslations; private readonly Dictionary> _controllerTranslations; private readonly Dictionary> _reverseAreaTranslations; private readonly Dictionary> _reverseControllerTranslations; public UrlTranslationService() { _areaTranslations = new Dictionary> { ["pt-BR"] = new Dictionary { ["TextTools"] = "ferramentas-de-texto", ["ImageConverters"] = "conversores-de-imagem" }, ["es-MX"] = new Dictionary { ["TextTools"] = "herramientas-de-texto", ["ImageConverters"] = "convertidores-de-imagen" }, ["es-CL"] = new Dictionary { ["TextTools"] = "herramientas-de-texto", ["ImageConverters"] = "convertidores-de-imagen" }, ["es-PY"] = new Dictionary { ["TextTools"] = "herramientas-de-texto", ["ImageConverters"] = "convertidores-de-imagen" } }; _controllerTranslations = new Dictionary> { ["pt-BR"] = new Dictionary { ["CaseConverter"] = "conversor-de-maiusculas-minusculas", ["JpgToWebp"] = "jpg-para-webp" }, ["es-MX"] = new Dictionary { ["CaseConverter"] = "conversor-de-mayusculas-minusculas", ["JpgToWebp"] = "jpg-a-webp" }, ["es-CL"] = new Dictionary { ["CaseConverter"] = "conversor-de-mayusculas-minusculas", ["JpgToWebp"] = "jpg-a-webp" }, ["es-PY"] = new Dictionary { ["CaseConverter"] = "conversor-de-mayusculas-minusculas", ["JpgToWebp"] = "jpg-a-webp" } }; // Create reverse mappings for lookup _reverseAreaTranslations = CreateReverseMappings(_areaTranslations); _reverseControllerTranslations = CreateReverseMappings(_controllerTranslations); } private Dictionary> CreateReverseMappings( Dictionary> original) { var reverse = new Dictionary>(); foreach (var culture in original.Keys) { reverse[culture] = new Dictionary(); foreach (var kvp in original[culture]) { reverse[culture][kvp.Value] = kvp.Key; } } return reverse; } public string TranslateArea(string area, CultureInfo culture) { var cultureName = culture.Name; if (_areaTranslations.TryGetValue(cultureName, out var areaDict) && areaDict.TryGetValue(area, out var translatedArea)) { return translatedArea; } return area.ToLowerInvariant(); } public string TranslateController(string controller, CultureInfo culture) { var cultureName = culture.Name; if (_controllerTranslations.TryGetValue(cultureName, out var controllerDict) && controllerDict.TryGetValue(controller, out var translatedController)) { return translatedController; } return controller.ToLowerInvariant(); } public string? GetOriginalArea(string translatedArea, CultureInfo culture) { var cultureName = culture.Name; if (_reverseAreaTranslations.TryGetValue(cultureName, out var areaDict) && areaDict.TryGetValue(translatedArea, out var originalArea)) { return originalArea; } return null; } public string? GetOriginalController(string translatedController, CultureInfo culture) { var cultureName = culture.Name; if (_reverseControllerTranslations.TryGetValue(cultureName, out var controllerDict) && controllerDict.TryGetValue(translatedController, out var originalController)) { return originalController; } return null; } public string GetLocalizedUrl(string area, string controller, string action, CultureInfo culture) { var translatedArea = TranslateArea(area, culture); var translatedController = TranslateController(controller, culture); return $"/{culture.Name}/{translatedArea}/{translatedController}"; } } }