Added ability to log out missing translations (#8477)

This commit is contained in:
Matteo Piovanelli
2021-05-28 08:44:32 +02:00
committed by GitHub
parent 7f6ccaf240
commit d2d77f78eb
2 changed files with 50 additions and 5 deletions

View File

@@ -34,6 +34,13 @@
<priority value="WARN" />
</logger>
<logger name="Orchard.Localization.Services.DefaultLocalizedStringManager">
<!-- Setting priority higher than INFO will prevent stuff being added to this logger
Because in code this is filled by a Logger.Information -->
<priority value="OFF" />
<appender-ref ref="localization-file" />
</logger>
<logger name="NHibernate.Cache">
<!-- This source is very verbose - setting priority here to avoid flooding trace if root priority is lowered. -->
<priority value="ERROR" />
@@ -104,4 +111,25 @@
</layout>
</appender>
<appender name="localization-file" type="Orchard.Logging.OrchardFileAppender">
<file value="App_Data/Logs/orchard-localization" />
<appendToFile value="true" />
<!-- Allow extended character sets (e.g. Chinese, Russian...) -->
<encoding value="utf-8" />
<!-- Immediate flush on error log, to avoid data loss with sudden termination. -->
<immediateFlush value="true" />
<staticLogFileName value="false" />
<rollingStyle value="Date" />
<datepattern value="-yyyy.MM.dd'.log'" />
<!-- Prevents Orchard.exe from displaying locking debug messages. -->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!-- Only add to this log the information about strings to localize -->
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="##CULTURE##" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %logger - %P{Tenant} - %level% [ExecutionId=%P{ExecutionId}]%newline[%P{Url}]%newline%message%newline " />
</layout>
</appender>
</log4net>

View File

@@ -1,15 +1,14 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Orchard.Caching;
using Orchard.Environment.Configuration;
using Orchard.Environment.Descriptor.Models;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models;
using Orchard.FileSystems.WebSite;
using Orchard.Logging;
using Orchard.Environment.Descriptor.Models;
using System.Linq;
using System.Collections;
using System;
namespace Orchard.Localization.Services {
public class DefaultLocalizedStringManager : ILocalizedStringManager {
@@ -46,12 +45,30 @@ namespace Orchard.Localization.Services {
Logger = NullLogger.Instance;
}
ILogger Logger { get; set; }
public ILogger Logger { get; set; }
public bool DisableMonitoring { get; set; }
public FormatForScope GetLocalizedString(IEnumerable<string> scopes, string text, string cultureName) {
var result = InnerGetLocalizedString(scopes, text, cultureName);
if (result == null) {
/*
* Log out messages that look like what we would have in the .po files
* Prepend that with a line telling the target culture for which the localization is missing
msgctxt "Orchard.Users.Activities.CreateUserActivity"
msgid "InvalidPassword"
msgstr "**InvalidPassword**"
*/
var sb = new StringBuilder();
// The configured appender for the localizations file will only consider
// messages that contain "##CULTURE##"
sb.AppendLine("##CULTURE## " + cultureName);
// These three lines math those found in .po files.
sb.AppendLine("msgctxt \"" + scopes.FirstOrDefault() + "\"");
sb.AppendLine("msgid \"" + text + "\"");
sb.AppendLine("msgstr \"**" + text + "**\"");
// to enable/disable logging this information, search in log4net.config
// for the logger named Orchard.Localization.Services.DefaultLocalizedStringManager
Logger.Information(sb.ToString());
return new FormatForScope(text, scopes.FirstOrDefault());
}
return result;