Files
Umbraco-CMS/src/Umbraco.Core/Models/DictionaryItemExtensions.cs
Elitsa Marinovska ba423a0108 V12: Add ISO codes to make the migration from language IDs easier (#14567)
* Change the obsoletion messages for language IDs to target V14 instead of V13.

* Wrong Language file

* Add ISO codes required to migrate custom code from language IDs

* Population of the new language FallbackIsoCode prop

* Changing obsoletion msgs from v13 to v14

* Fix breaking changes
2023-07-18 10:53:14 +02:00

48 lines
1.6 KiB
C#

using Umbraco.Cms.Core.Models;
namespace Umbraco.Extensions;
public static class DictionaryItemExtensions
{
/// <summary>
/// Returns the translation value for the language ISO code, if no translation is found it returns an empty string
/// </summary>
/// <param name="d"></param>
/// <param name="isoCode"></param>
/// <returns></returns>
public static string? GetTranslatedValue(this IDictionaryItem d, string isoCode)
{
IDictionaryTranslation? trans = d.Translations.FirstOrDefault(x => x.LanguageIsoCode == isoCode);
return trans == null ? string.Empty : trans.Value;
}
/// <summary>
/// Adds or updates a translation for a dictionary item and language
/// </summary>
/// <param name="item"></param>
/// <param name="language"></param>
/// <param name="value"></param>
public static void AddOrUpdateDictionaryValue(this IDictionaryItem item, ILanguage language, string value)
{
IDictionaryTranslation? existing = item.Translations?.FirstOrDefault(x => x.LanguageIsoCode.Equals(language.IsoCode));
if (existing != null)
{
existing.Value = value;
}
else
{
if (item.Translations is not null)
{
item.Translations = new List<IDictionaryTranslation>(item.Translations)
{
new DictionaryTranslation(language, value),
};
}
else
{
item.Translations = new List<IDictionaryTranslation> { new DictionaryTranslation(language, value) };
}
}
}
}