From d069da8a4ff27538cf61059bed96bbbbdd6d40ca Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Tue, 15 Dec 2015 12:51:03 +0100 Subject: [PATCH] Make LocalizedTextService fall back to en-GB if the key is not found in the current file --- .../Services/LocalizedTextService.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Umbraco.Core/Services/LocalizedTextService.cs b/src/Umbraco.Core/Services/LocalizedTextService.cs index cc789434f8..1e8d170b89 100644 --- a/src/Umbraco.Core/Services/LocalizedTextService.cs +++ b/src/Umbraco.Core/Services/LocalizedTextService.cs @@ -254,23 +254,35 @@ namespace Umbraco.Core.Services return "[" + key + "]"; } - var cultureSource = xmlSource[culture].Value; - - var xpath = area.IsNullOrWhiteSpace() - ? string.Format("//key [@alias = '{0}']", key) - : string.Format("//area [@alias = '{0}']/key [@alias = '{1}']", area, key); - - var found = cultureSource.XPathSelectElement(xpath); + var found = FindTranslation(xmlSource, culture, area, key); if (found != null) { return ParseTokens(found.Value, tokens); } + + // Fall back to English by default if we can't find the key + found = FindTranslation(xmlSource, new CultureInfo("en-GB"), area, key); + if (found != null) + return ParseTokens(found.Value, tokens); - //NOTE: Based on how legacy works, the default text does not contain the area, just the key + // If it can't be found in either file, fall back to the default, showing just the key in square brackets + // NOTE: Based on how legacy works, the default text does not contain the area, just the key return "[" + key + "]"; } + private XElement FindTranslation(IDictionary> xmlSource, CultureInfo culture, string area, string key) + { + var cultureSource = xmlSource[culture].Value; + + var xpath = area.IsNullOrWhiteSpace() + ? string.Format("//key [@alias = '{0}']", key) + : string.Format("//area [@alias = '{0}']/key [@alias = '{1}']", area, key); + + var found = cultureSource.XPathSelectElement(xpath); + return found; + } + /// /// Parses the tokens in the value ///