Updates UI to allow updating a culture for a language and warn the user if they are changing it, updates the controller to be able to udpate a culture on a lang
This commit is contained in:
@@ -266,13 +266,21 @@ namespace Umbraco.Web.Cache
|
||||
public static void RefreshLanguageCache(this DistributedCache dc, ILanguage language)
|
||||
{
|
||||
if (language == null) return;
|
||||
dc.Refresh(LanguageCacheRefresher.UniqueId, language.Id);
|
||||
|
||||
var payload = new LanguageCacheRefresher.JsonPayload(language.Id, language.IsoCode,
|
||||
language.WasPropertyDirty(nameof(ILanguage.IsoCode))
|
||||
? LanguageCacheRefresher.JsonPayload.LanguageChangeType.ChangeCulture
|
||||
: LanguageCacheRefresher.JsonPayload.LanguageChangeType.Update);
|
||||
|
||||
dc.RefreshByPayload(LanguageCacheRefresher.UniqueId, new[] { payload });
|
||||
}
|
||||
|
||||
public static void RemoveLanguageCache(this DistributedCache dc, ILanguage language)
|
||||
{
|
||||
if (language == null) return;
|
||||
dc.Remove(LanguageCacheRefresher.UniqueId, language.Id);
|
||||
|
||||
var payload = new LanguageCacheRefresher.JsonPayload(language.Id, language.IsoCode, LanguageCacheRefresher.JsonPayload.LanguageChangeType.Remove);
|
||||
dc.RefreshByPayload(LanguageCacheRefresher.UniqueId, new[] { payload });
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -99,24 +99,28 @@ namespace Umbraco.Web.Editors
|
||||
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
|
||||
|
||||
// this is prone to race conditions but the service will not let us proceed anyways
|
||||
var existing = Services.LocalizationService.GetLanguageByIsoCode(language.IsoCode);
|
||||
var existingByCulture = Services.LocalizationService.GetLanguageByIsoCode(language.IsoCode);
|
||||
|
||||
// the localization service might return the generic language even when queried for specific ones (e.g. "da" when queried for "da-DK")
|
||||
// - we need to handle that explicitly
|
||||
if (existing?.IsoCode != language.IsoCode)
|
||||
if (existingByCulture?.IsoCode != language.IsoCode)
|
||||
{
|
||||
existing = null;
|
||||
existingByCulture = null;
|
||||
}
|
||||
|
||||
if (existing != null && language.Id != existing.Id)
|
||||
if (existingByCulture != null && language.Id != existingByCulture.Id)
|
||||
{
|
||||
//someone is trying to create a language that already exist
|
||||
ModelState.AddModelError("IsoCode", "The language " + language.IsoCode + " already exists");
|
||||
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
|
||||
}
|
||||
|
||||
if (existing == null)
|
||||
var existingById = language.Id != default ? Services.LocalizationService.GetLanguageById(language.Id) : null;
|
||||
|
||||
if (existingById == null)
|
||||
{
|
||||
//Creating a new lang...
|
||||
|
||||
CultureInfo culture;
|
||||
try
|
||||
{
|
||||
@@ -141,38 +145,39 @@ namespace Umbraco.Web.Editors
|
||||
return Mapper.Map<Language>(newLang);
|
||||
}
|
||||
|
||||
existing.IsMandatory = language.IsMandatory;
|
||||
existingById.IsMandatory = language.IsMandatory;
|
||||
|
||||
// note that the service will prevent the default language from being "un-defaulted"
|
||||
// but does not hurt to test here - though the UI should prevent it too
|
||||
if (existing.IsDefault && !language.IsDefault)
|
||||
if (existingById.IsDefault && !language.IsDefault)
|
||||
{
|
||||
ModelState.AddModelError("IsDefault", "Cannot un-default the default language.");
|
||||
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
|
||||
}
|
||||
|
||||
existing.IsDefault = language.IsDefault;
|
||||
existing.FallbackLanguageId = language.FallbackLanguageId;
|
||||
existingById.IsDefault = language.IsDefault;
|
||||
existingById.FallbackLanguageId = language.FallbackLanguageId;
|
||||
existingById.IsoCode = language.IsoCode;
|
||||
|
||||
// modifying an existing language can create a fallback, verify
|
||||
// note that the service will check again, dealing with race conditions
|
||||
if (existing.FallbackLanguageId.HasValue)
|
||||
if (existingById.FallbackLanguageId.HasValue)
|
||||
{
|
||||
var languages = Services.LocalizationService.GetAllLanguages().ToDictionary(x => x.Id, x => x);
|
||||
if (!languages.ContainsKey(existing.FallbackLanguageId.Value))
|
||||
if (!languages.ContainsKey(existingById.FallbackLanguageId.Value))
|
||||
{
|
||||
ModelState.AddModelError("FallbackLanguage", "The selected fall back language does not exist.");
|
||||
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
|
||||
}
|
||||
if (CreatesCycle(existing, languages))
|
||||
if (CreatesCycle(existingById, languages))
|
||||
{
|
||||
ModelState.AddModelError("FallbackLanguage", $"The selected fall back language {languages[existing.FallbackLanguageId.Value].IsoCode} would create a circular path.");
|
||||
ModelState.AddModelError("FallbackLanguage", $"The selected fall back language {languages[existingById.FallbackLanguageId.Value].IsoCode} would create a circular path.");
|
||||
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
|
||||
}
|
||||
}
|
||||
|
||||
Services.LocalizationService.Save(existing);
|
||||
return Mapper.Map<Language>(existing);
|
||||
Services.LocalizationService.Save(existingById);
|
||||
return Mapper.Map<Language>(existingById);
|
||||
}
|
||||
|
||||
// see LocalizationService
|
||||
|
||||
Reference in New Issue
Block a user