namespace My.Dxa.Extensions
{
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Sdl.Web.Common.Configuration;
public static class LocalizationExtensions
{
// inspired by Sdl.Web.Common.Configuration.GetConfigValue()
public static IDictionary<string, string> GetConfigValues(this Localization localization)
{
IDictionary<string, string> configValues = new Dictionary<string, string>();
IEnumerable<string> modules = localization.GetResources().Keys.Cast<string>().ToList().Select(k => k.Substring(0, k.LastIndexOf(".", StringComparison.Ordinal))).Distinct().OrderBy(s => s);
foreach (string module in modules)
{
foreach (var configValue in LoadConfigSection(localization, module))
{
configValues.Add(module + "." + configValue.Key, configValue.Value);
}
}
return configValues;
}
// inspired by Sdl.Web.Common.Configuration.GetConfigValue()
public static IDictionary<string, string> GetConfigValues(this Localization localization, string sectionName)
{
// inspired by ...
return LoadConfigSection(localization, sectionName);
}
// inspired by Sdl.Web.Common.Configuration.LoadConfigSection()
private static IDictionary<string, string> LoadConfigSection(Localization localization, string sectionName)
{
string configItemUrl = string.Format("{0}/{1}/config/{2}.json", localization.Path, SiteConfiguration.SystemFolder, sectionName);
string configJson = SiteConfiguration.ContentProvider.GetStaticContentItem(configItemUrl, localization).GetText();
IDictionary<string, string> configSection = JsonConvert.DeserializeObject<Dictionary<string, string>>(configJson);
return configSection;
}
}
}
// put this in the AdminController which comes with DXA
using Sdl.Web.Common;
using Sdl.Web.Common.Configuration;
public ActionResult SiteLocalization(string expand = null)
{
if (expand != null)
{
foreach (Localization siteLocalization in WebRequestContext.Localization.SiteLocalizations)
{
try
{
siteLocalization.EnsureInitialized();
}
catch (DxaItemNotFoundException)
{
}
}
}
Localization localization = WebRequestContext.Localization;
return (ActionResult)this.View("SiteLocalization", localization);
}