Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
added proper logging for missing resource keys
Browse files Browse the repository at this point in the history
  • Loading branch information
valdisiljuconoks committed Jun 30, 2019
1 parent 4191648 commit 3fa7738
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand All @@ -26,29 +27,41 @@
using DbLocalizationProvider.EPiServer.Queries;
using DbLocalizationProvider.Queries;
using EPiServer.Framework.Localization;
using EPiServer.Logging;

namespace DbLocalizationProvider.EPiServer
{
public class DatabaseLocalizationProvider : global::EPiServer.Framework.Localization.LocalizationProvider
{
private readonly IQueryHandler<GetTranslation.Query, string> _originalHandler;
private readonly ILogger _logger;

// we have to move to Lazy<T> because configuration settings will not be set during DatabaseProvider constructor
// as this type is created pretty early in the pipeline and none of initialization modules have been run yet
private readonly Lazy<IQueryHandler<GetTranslation.Query, string>> _originalHandler
= new Lazy<IQueryHandler<GetTranslation.Query, string>>(() =>
{
if(ConfigurationContext.Current.DiagnosticsEnabled)
return new EPiServerGetTranslation.HandlerWithLogging(new GetTranslationHandler());

return new GetTranslationHandler();
});

public DatabaseLocalizationProvider()
{
_originalHandler = new GetTranslationHandler();
if(ConfigurationContext.Current.DiagnosticsEnabled)
_originalHandler = new EPiServerGetTranslation.HandlerWithLogging(new GetTranslationHandler());
_logger = LogManager.GetLogger(typeof(DatabaseLocalizationProvider));
}

public override IEnumerable<CultureInfo> AvailableLanguages => new AvailableLanguages.Query().Execute();

public override string GetString(string originalKey, string[] normalizedKey, CultureInfo culture)
{
_logger.Debug($"Executing query for resource key `{originalKey}` for language: `{culture.Name}...");

// we need to call handler directly here
// if we would dispatch query and ask registered handler to execute
// we would end up in stack-overflow as in EPiServer context
// the same database localization provider is registered as the query handler
var foundTranslation = _originalHandler.Execute(new GetTranslation.Query(originalKey, culture, false));
var foundTranslation = _originalHandler.Value.Execute(new GetTranslation.Query(originalKey, culture, false));

// this is last chance for Episerver to find translation (asked in translation fallback language)
// if no match is found and invariant fallback is configured - return invariant culture translation
Expand All @@ -57,7 +70,8 @@ public override string GetString(string originalKey, string[] normalizedKey, Cul
&& ConfigurationContext.Current.EnableInvariantCultureFallback
&& (Equals(culture, LocalizationService.Current.FallbackCulture) || Equals(culture.Parent, CultureInfo.InvariantCulture)))
{
return _originalHandler.Execute(new GetTranslation.Query(originalKey, CultureInfo.InvariantCulture, false));
_logger.Debug($"Null returned for resource key `{originalKey}` for language: `{culture.Name} Executing InvariantCulture fallback.");
return _originalHandler.Value.Execute(new GetTranslation.Query(originalKey, CultureInfo.InvariantCulture, false));
}

return foundTranslation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ public class EPiServerGetTranslation
public class Handler : IQueryHandler<GetTranslation.Query, string>
{
private readonly IQueryHandler<GetTranslation.Query, string> _originalHandler;
private readonly ILogger _logger;

public Handler()
{
_originalHandler = new GetTranslationHandler();
_logger = LogManager.GetLogger(typeof(EPiServerGetTranslation));
}

public string Execute(GetTranslation.Query query)
{
_logger.Debug($"Executing query for resource key `{query.Key}` for language: `{query.Language.Name}...");

var service = ServiceLocator.Current.GetInstance<LocalizationService>();
var foundTranslation = service.GetStringByCulture(query.Key, query.Language);

Expand All @@ -58,6 +62,8 @@ public string Execute(GetTranslation.Query query)
{
// no translation found for this language
// we need to respect fallback settings
_logger.Debug($"Null returned for resource key `{query.Key}` for language: `{query.Language.Name} Executing InvariantCulture fallback.");

return _originalHandler.Execute(new GetTranslation.Query(query.Key, CultureInfo.InvariantCulture, false));
}

Expand Down

0 comments on commit 3fa7738

Please sign in to comment.