mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Adjust to Default LocalizedStringManager to be singleton compatible
The dependency on the ICultureManager has been removed As a side-effect, the translations are no longer limited to what has been configured in Orchard But that is a harmless - the configured list is still used when choosing cultures so doesn't need to be enforced at this level --HG-- branch : perf extra : rebase_source : fa086c6c1ed90bc0537fa4566e6be64047d7d62f
This commit is contained in:
@@ -10,7 +10,6 @@ using Orchard.FileSystems.WebSite;
|
|||||||
namespace Orchard.Localization.Services {
|
namespace Orchard.Localization.Services {
|
||||||
public class DefaultLocalizedStringManager : ILocalizedStringManager {
|
public class DefaultLocalizedStringManager : ILocalizedStringManager {
|
||||||
private readonly IWebSiteFolder _webSiteFolder;
|
private readonly IWebSiteFolder _webSiteFolder;
|
||||||
private readonly ICultureManager _cultureManager;
|
|
||||||
private readonly IExtensionManager _extensionManager;
|
private readonly IExtensionManager _extensionManager;
|
||||||
private readonly ICacheManager _cacheManager;
|
private readonly ICacheManager _cacheManager;
|
||||||
private readonly ShellSettings _shellSettings;
|
private readonly ShellSettings _shellSettings;
|
||||||
@@ -22,13 +21,11 @@ namespace Orchard.Localization.Services {
|
|||||||
const string TenantLocalizationFilePathFormat = "~/App_Data/Sites/{0}/Localization/{1}/orchard.po";
|
const string TenantLocalizationFilePathFormat = "~/App_Data/Sites/{0}/Localization/{1}/orchard.po";
|
||||||
|
|
||||||
public DefaultLocalizedStringManager(
|
public DefaultLocalizedStringManager(
|
||||||
ICultureManager cultureManager,
|
IWebSiteFolder webSiteFolder,
|
||||||
IWebSiteFolder webSiteFolder,
|
|
||||||
IExtensionManager extensionManager,
|
IExtensionManager extensionManager,
|
||||||
ICacheManager cacheManager,
|
ICacheManager cacheManager,
|
||||||
ShellSettings shellSettings,
|
ShellSettings shellSettings,
|
||||||
ISignals signals) {
|
ISignals signals) {
|
||||||
_cultureManager = cultureManager;
|
|
||||||
_webSiteFolder = webSiteFolder;
|
_webSiteFolder = webSiteFolder;
|
||||||
_extensionManager = extensionManager;
|
_extensionManager = extensionManager;
|
||||||
_cacheManager = cacheManager;
|
_cacheManager = cacheManager;
|
||||||
@@ -43,45 +40,36 @@ namespace Orchard.Localization.Services {
|
|||||||
// parent culture as defined in the .net culture hierarchy. e.g. fr-FR will fallback to fr.
|
// parent culture as defined in the .net culture hierarchy. e.g. fr-FR will fallback to fr.
|
||||||
// In case it's not found anywhere, the text is returned as is.
|
// In case it's not found anywhere, the text is returned as is.
|
||||||
public string GetLocalizedString(string scope, string text, string cultureName) {
|
public string GetLocalizedString(string scope, string text, string cultureName) {
|
||||||
var cultures = LoadCultures();
|
var culture = LoadCulture(cultureName);
|
||||||
|
|
||||||
foreach (var culture in cultures) {
|
string scopedKey = (scope + "|" + text).ToLowerInvariant();
|
||||||
if (String.Equals(cultureName, culture.CultureName, StringComparison.OrdinalIgnoreCase)) {
|
if (culture.Translations.ContainsKey(scopedKey)) {
|
||||||
string scopedKey = (scope + "|" + text).ToLowerInvariant();
|
return culture.Translations[scopedKey];
|
||||||
if (culture.Translations.ContainsKey(scopedKey)) {
|
|
||||||
return culture.Translations[scopedKey];
|
|
||||||
}
|
|
||||||
|
|
||||||
string genericKey = ("|" + text).ToLowerInvariant();
|
|
||||||
if ( culture.Translations.ContainsKey(genericKey) ) {
|
|
||||||
return culture.Translations[genericKey];
|
|
||||||
}
|
|
||||||
|
|
||||||
return GetParentTranslation(scope, text, cultureName, cultures);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return text;
|
string genericKey = ("|" + text).ToLowerInvariant();
|
||||||
|
if (culture.Translations.ContainsKey(genericKey)) {
|
||||||
|
return culture.Translations[genericKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
return GetParentTranslation(scope, text, cultureName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetParentTranslation(string scope, string text, string cultureName, IEnumerable<CultureDictionary> cultures) {
|
private string GetParentTranslation(string scope, string text, string cultureName) {
|
||||||
string scopedKey = (scope + "|" + text).ToLowerInvariant();
|
string scopedKey = (scope + "|" + text).ToLowerInvariant();
|
||||||
string genericKey = ("|" + text).ToLowerInvariant();
|
string genericKey = ("|" + text).ToLowerInvariant();
|
||||||
try {
|
try {
|
||||||
CultureInfo cultureInfo = CultureInfo.GetCultureInfo(cultureName);
|
CultureInfo cultureInfo = CultureInfo.GetCultureInfo(cultureName);
|
||||||
CultureInfo parentCultureInfo = cultureInfo.Parent;
|
CultureInfo parentCultureInfo = cultureInfo.Parent;
|
||||||
if (parentCultureInfo.IsNeutralCulture) {
|
if (parentCultureInfo.IsNeutralCulture) {
|
||||||
foreach (var culture in cultures) {
|
var culture = LoadCulture(parentCultureInfo.Name);
|
||||||
if (String.Equals(parentCultureInfo.Name, culture.CultureName, StringComparison.OrdinalIgnoreCase)) {
|
if (culture.Translations.ContainsKey(scopedKey)) {
|
||||||
if (culture.Translations.ContainsKey(scopedKey)) {
|
return culture.Translations[scopedKey];
|
||||||
return culture.Translations[scopedKey];
|
|
||||||
}
|
|
||||||
if (culture.Translations.ContainsKey(genericKey)) {
|
|
||||||
return culture.Translations[genericKey];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (culture.Translations.ContainsKey(genericKey)) {
|
||||||
|
return culture.Translations[genericKey];
|
||||||
|
}
|
||||||
|
return text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (CultureNotFoundException) { }
|
catch (CultureNotFoundException) { }
|
||||||
@@ -89,22 +77,17 @@ namespace Orchard.Localization.Services {
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loads the culture dictionaries in memory and caches them.
|
// Loads the culture dictionary in memory and caches it.
|
||||||
// Cache entry will be invalidated any time the directories hosting
|
// Cache entry will be invalidated any time the directories hosting
|
||||||
// the .po files are modified.
|
// the .po files are modified.
|
||||||
private IEnumerable<CultureDictionary> LoadCultures() {
|
private CultureDictionary LoadCulture(string culture) {
|
||||||
return _cacheManager.Get("cultures", ctx => {
|
return _cacheManager.Get(culture, ctx => {
|
||||||
var cultures = new List<CultureDictionary>();
|
|
||||||
foreach (var culture in _cultureManager.ListCultures()) {
|
|
||||||
cultures.Add(new CultureDictionary {
|
|
||||||
CultureName = culture,
|
|
||||||
Translations = LoadTranslationsForCulture(culture, ctx)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
ctx.Monitor(_signals.When("culturesChanged"));
|
ctx.Monitor(_signals.When("culturesChanged"));
|
||||||
return cultures;
|
return new CultureDictionary {
|
||||||
|
CultureName = culture,
|
||||||
|
Translations = LoadTranslationsForCulture(culture, ctx)
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merging occurs from multiple locations:
|
// Merging occurs from multiple locations:
|
||||||
@@ -127,22 +110,22 @@ namespace Orchard.Localization.Services {
|
|||||||
context.Monitor(_webSiteFolder.WhenPathChanges(corePath));
|
context.Monitor(_webSiteFolder.WhenPathChanges(corePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( var module in _extensionManager.AvailableExtensions() ) {
|
foreach (var module in _extensionManager.AvailableExtensions()) {
|
||||||
if ( String.Equals(module.ExtensionType, "Module") ) {
|
if (String.Equals(module.ExtensionType, "Module")) {
|
||||||
string modulePath = string.Format(ModulesLocalizationFilePathFormat, module.Name, culture);
|
string modulePath = string.Format(ModulesLocalizationFilePathFormat, module.Name, culture);
|
||||||
text = _webSiteFolder.ReadFile(modulePath);
|
text = _webSiteFolder.ReadFile(modulePath);
|
||||||
if ( text != null ) {
|
if (text != null) {
|
||||||
ParseLocalizationStream(text, translations, true);
|
ParseLocalizationStream(text, translations, true);
|
||||||
context.Monitor(_webSiteFolder.WhenPathChanges(modulePath));
|
context.Monitor(_webSiteFolder.WhenPathChanges(modulePath));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( var theme in _extensionManager.AvailableExtensions() ) {
|
foreach (var theme in _extensionManager.AvailableExtensions()) {
|
||||||
if ( String.Equals(theme.ExtensionType, "Theme") ) {
|
if (String.Equals(theme.ExtensionType, "Theme")) {
|
||||||
string themePath = string.Format(ThemesLocalizationFilePathFormat, theme.Name, culture);
|
string themePath = string.Format(ThemesLocalizationFilePathFormat, theme.Name, culture);
|
||||||
text = _webSiteFolder.ReadFile(themePath);
|
text = _webSiteFolder.ReadFile(themePath);
|
||||||
if ( text != null ) {
|
if (text != null) {
|
||||||
ParseLocalizationStream(text, translations, true);
|
ParseLocalizationStream(text, translations, true);
|
||||||
context.Monitor(_webSiteFolder.WhenPathChanges(themePath));
|
context.Monitor(_webSiteFolder.WhenPathChanges(themePath));
|
||||||
}
|
}
|
||||||
@@ -184,7 +167,7 @@ namespace Orchard.Localization.Services {
|
|||||||
if (poLine.StartsWith("msgstr")) {
|
if (poLine.StartsWith("msgstr")) {
|
||||||
string translation = ParseTranslation(poLine);
|
string translation = ParseTranslation(poLine);
|
||||||
// ignore incomplete localizations (empty msgid or msgstr)
|
// ignore incomplete localizations (empty msgid or msgstr)
|
||||||
if ( !String.IsNullOrWhiteSpace(id) && !String.IsNullOrWhiteSpace(translation) ) {
|
if (!String.IsNullOrWhiteSpace(id) && !String.IsNullOrWhiteSpace(translation)) {
|
||||||
string scopedKey = (scope + "|" + id).ToLowerInvariant();
|
string scopedKey = (scope + "|" + id).ToLowerInvariant();
|
||||||
if (!translations.ContainsKey(scopedKey)) {
|
if (!translations.ContainsKey(scopedKey)) {
|
||||||
translations.Add(scopedKey, translation);
|
translations.Add(scopedKey, translation);
|
||||||
|
|||||||
Reference in New Issue
Block a user