Fixes #12904 UmbracoHelper.GetDictionaryValue defaults to en-US when used in non-front end code (#12942)
* adding new overload/rename a method * remove this keyword * fix comment * remove space * commit * revert * replace param name * public backward compatibility * Minor style tweaks * Don't change default culture in UmbracoCultureDictionary --------- Co-authored-by: Nikolaj <nikolajlauridsen@protonmail.ch>
This commit is contained in:
committed by
GitHub
parent
41805af0d9
commit
0da33d064a
@@ -1,6 +1,10 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Umbraco.Cms.Core.Dictionary;
|
||||
|
||||
public interface ICultureDictionaryFactory
|
||||
{
|
||||
ICultureDictionary CreateDictionary();
|
||||
|
||||
ICultureDictionary CreateDictionary(CultureInfo specificCulture) => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
@@ -47,7 +48,7 @@ internal class DefaultCultureDictionary : ICultureDictionary
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current culture
|
||||
/// Returns the defualt umbraco's back office culture
|
||||
/// </summary>
|
||||
public CultureInfo Culture => _specificCulture ?? Thread.CurrentThread.CurrentUICulture;
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Globalization;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
|
||||
@@ -23,4 +24,7 @@ public class DefaultCultureDictionaryFactory : ICultureDictionaryFactory
|
||||
|
||||
public ICultureDictionary CreateDictionary() =>
|
||||
new DefaultCultureDictionary(_localizationService, _appCaches.RequestCache);
|
||||
|
||||
public ICultureDictionary CreateDictionary(CultureInfo specificCulture) =>
|
||||
new DefaultCultureDictionary(specificCulture, _localizationService, _appCaches.RequestCache);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Globalization;
|
||||
using System.Xml.XPath;
|
||||
using Serilog.Events;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Dictionary;
|
||||
using Umbraco.Cms.Core.Models.PublishedContent;
|
||||
@@ -139,12 +141,43 @@ public class UmbracoHelper
|
||||
/// <returns></returns>
|
||||
public string? GetDictionaryValue(string key) => CultureDictionary[key];
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns the dictionary value for the key specified, and if empty returns the specified default fall back value
|
||||
/// </summary>
|
||||
/// <param name="key">key of dictionary item</param>
|
||||
/// <param name="specificCulture">the specific culture on which the result well be back upon</param>
|
||||
/// <returns></returns>
|
||||
public string? GetDictionaryValue(string key, CultureInfo specificCulture)
|
||||
{
|
||||
_cultureDictionary = _cultureDictionaryFactory.CreateDictionary(specificCulture);
|
||||
return GetDictionaryValue(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the dictionary value for the key specified, and if empty returns the specified default fall back value
|
||||
/// </summary>
|
||||
/// <param name="key">key of dictionary item</param>
|
||||
/// <param name="defaultValue">fall back text if dictionary item is empty - Name altText to match Umbraco.Field</param>
|
||||
/// <returns></returns>
|
||||
public string GetDictionaryValueOrDefault(string key, string defaultValue)
|
||||
{
|
||||
var dictionaryValue = GetDictionaryValue(key);
|
||||
if (string.IsNullOrWhiteSpace(dictionaryValue))
|
||||
{
|
||||
dictionaryValue = defaultValue;
|
||||
}
|
||||
|
||||
return dictionaryValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the dictionary value for the key specified, and if empty returns the specified default fall back value
|
||||
/// </summary>
|
||||
/// <param name="key">key of dictionary item</param>
|
||||
/// <param name="altText">fall back text if dictionary item is empty - Name altText to match Umbraco.Field</param>
|
||||
/// <returns></returns>
|
||||
[Obsolete("Use GetDictionaryValueOrDefault instead, scheduled for removal in v14.")]
|
||||
public string GetDictionaryValue(string key, string altText)
|
||||
{
|
||||
var dictionaryValue = GetDictionaryValue(key);
|
||||
@@ -156,6 +189,25 @@ public class UmbracoHelper
|
||||
return dictionaryValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the dictionary value for the key specified, and if empty returns the specified default fall back value
|
||||
/// </summary>
|
||||
/// <param name="key">key of dictionary item</param>
|
||||
/// <param name="specificCulture">the specific culture on which the result well be back upon</param>
|
||||
/// <param name="defaultValue">fall back text if dictionary item is empty - Name altText to match Umbraco.Field</param>
|
||||
/// <returns></returns>
|
||||
public string GetDictionaryValueOrDefault(string key, CultureInfo specificCulture, string defaultValue)
|
||||
{
|
||||
_cultureDictionary = _cultureDictionaryFactory.CreateDictionary(specificCulture);
|
||||
var dictionaryValue = GetDictionaryValue(key);
|
||||
if (string.IsNullOrWhiteSpace(dictionaryValue))
|
||||
{
|
||||
dictionaryValue = defaultValue;
|
||||
}
|
||||
return dictionaryValue;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns the ICultureDictionary for access to dictionary items
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user