Read my blog here
-
-
Save jstemerdink/06939ad19f917ae275b840c1f9387504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Globalization; | |
using System.Linq; | |
using System.Web.Http; | |
using EPiServer.Framework.Localization; | |
using EPiServer.ServiceLocation; | |
/// <summary> | |
/// The Translation api controller. | |
/// </summary> | |
[DisplayName("Translation Api")] | |
[RoutePrefix("api/translation")] | |
public class TranslationApiController : ApiController | |
{ | |
private readonly LocalizationService localizationService; | |
public TranslationApiController() | |
: this(ServiceLocator.Current.GetInstance<LocalizationService>()) | |
{ | |
} | |
public TranslationApiController(LocalizationService localizationService) | |
{ | |
this.localizationService = localizationService; | |
} | |
/// <summary> | |
/// Gets all translations. | |
/// </summary> | |
/// <returns>An array of all translations, by culture.</returns> | |
[HttpGet] | |
[Route("")] | |
public IHttpActionResult GetTranslations() | |
{ | |
Dictionary<string, Dictionary<string, string>> keyValues = this.GetKeyValues(); | |
return this.Json(content: keyValues); | |
} | |
/// <summary> | |
/// Gets the translations. | |
/// </summary> | |
/// <param name="key">The key.</param> | |
/// <returns>An array of translations for the specified <paramref name="key"/>, by culture.</returns> | |
[HttpGet] | |
[Route("{key}")] | |
public IHttpActionResult GetTranslations(string key) | |
{ | |
Dictionary<string, Dictionary<string, string>> keyValues = this.GetKeyValues(key: key); | |
return this.Json(content: keyValues); | |
} | |
/// <summary> | |
/// Gets the translations. | |
/// </summary> | |
/// <param name="culture">The culture.</param> | |
/// <param name="key">The key.</param> | |
/// <returns>An array of translations for the specified <paramref name="culture"/> and <paramref name="key"/>.</returns> | |
[HttpGet] | |
[Route("{culture}/{key}")] | |
public IHttpActionResult GetTranslations(string culture, string key) | |
{ | |
List<ResourceItem> resourceItems = this.localizationService.GetAllStringsByCulture( | |
$"/{key.Replace('.', '/')}", | |
new CultureInfo(name: culture)).ToList(); | |
Dictionary<string, string> keyValues = new Dictionary<string, string>(); | |
foreach (ResourceItem resourceItem in resourceItems) | |
{ | |
AddKeyValue(resourceItem: resourceItem, keyValues: keyValues); | |
} | |
return this.Json(content: keyValues); | |
} | |
private static void AddKeyValue(ResourceItem resourceItem, Dictionary<string, string> keyValues) | |
{ | |
string resourceKey = resourceItem.Key.Replace('/', '.').TrimStart('.'); | |
string resourceValue = resourceItem.Value; | |
if (!keyValues.ContainsKey(key: resourceKey)) | |
{ | |
keyValues.Add(key: resourceKey, value: resourceValue); | |
} | |
} | |
private Dictionary<string, Dictionary<string, string>> GetKeyValues(string key = null) | |
{ | |
List<ResourceItem> resourceItems = string.IsNullOrWhiteSpace(value: key) | |
? this.localizationService.GetAllStrings().ToList() | |
: this.localizationService.GetAllStrings($"/{key.Replace('.', '/')}").ToList(); | |
List<IGrouping<string, ResourceItem>> groupedTranslationsList = resourceItems | |
.GroupBy(u => u.SourceCulture.Name).ToList(); | |
Dictionary<string, Dictionary<string, string>> keyValues = | |
new Dictionary<string, Dictionary<string, string>>(); | |
foreach (IGrouping<string, ResourceItem> group in groupedTranslationsList) | |
{ | |
Dictionary<string, string> cultureDictionary = new Dictionary<string, string>(); | |
foreach (ResourceItem resourceItem in group) | |
{ | |
AddKeyValue(resourceItem: resourceItem, keyValues: cultureDictionary); | |
} | |
keyValues.Add(key: group.Key, value: cultureDictionary); | |
} | |
return keyValues; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment