Skip to content

Commit 761115c

Browse files
committed
added and fixed fallback behaviour
1 parent 2f6fc1d commit 761115c

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

Localization.AspNetCore.EntityFramework.Test/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void ConfigureServices(IServiceCollection services)
4242
services.AddLocalization<ApplicationDbContext>(options =>
4343
{
4444
{
45-
options.ReturnKeyNameIfNoTranslation = true;
45+
options.FallBackBehavior = FallBackBehaviorEnum.DefaultCulture;
4646
options.CreateMissingTranslationsIfNotFound = true;
4747
options.NamingConvention = NamingConventionEnum.FullTypeName;
4848
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Localization.AspNetCore.EntityFramework.Enums
2+
{
3+
public enum FallBackBehaviorEnum
4+
{
5+
DefaultCulture,
6+
KeyName
7+
}
8+
}

Localization.AspNetCore.EntityFramework/Factories/LocalizerFactory.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public LocalizerFactory(IServiceProvider serviceProvider,
3838
}
3939

4040
private string CurrentLanguage => CultureInfo.CurrentUICulture.Name;
41+
private string DefaultLanguage => _requestLocalizationSettings.DefaultRequestCulture.UICulture.Name;
4142

4243
public void ResetCache()
4344
{
@@ -52,12 +53,12 @@ public void ResetCache()
5253
}
5354
}
5455
}
55-
56+
5657
public void Import(IList<LocalizationResource> source)
5758
{
5859
if (source == null)
5960
throw new ArgumentException(nameof(source));
60-
61+
6162
using (var scope = _serviceProvider.GetScopedService(out T context))
6263
{
6364
// clear all resources
@@ -66,7 +67,7 @@ public void Import(IList<LocalizationResource> source)
6667
// add import
6768
context.Set<LocalizationResource>()
6869
.AddRange(source);
69-
70+
7071
context.SaveChanges();
7172
}
7273
}
@@ -99,18 +100,30 @@ public LocalizedString GetResource(string resourceKey)
99100
p.Value
100101
})
101102
.SingleOrDefault();
102-
103-
var result = item?.Value;
104-
105-
if (_localizerSettings.CreateMissingTranslationsIfNotFound &&
106-
item == null)
107-
{
103+
104+
if (item == null && _localizerSettings.CreateMissingTranslationsIfNotFound)
108105
AddMissingResourceKeys(resourceKey);
109-
result = GetResource(resourceKey);
110-
}
111106

112-
if (_localizerSettings.ReturnKeyNameIfNoTranslation && string.IsNullOrWhiteSpace(result))
113-
result = resourceKey;
107+
var result = item?.Value ?? String.Empty;
108+
109+
if (string.IsNullOrWhiteSpace(result))
110+
{
111+
switch (_localizerSettings.FallBackBehavior)
112+
{
113+
case FallBackBehaviorEnum.KeyName:
114+
result = resourceKey;
115+
break;
116+
117+
case FallBackBehaviorEnum.DefaultCulture:
118+
result = _cache
119+
.SelectMany(r => r.Translations)
120+
.Where(t => t.Resource.ResourceKey == resourceKey
121+
&& t.Language == DefaultLanguage)
122+
.Select(t => t.Value ?? String.Empty)
123+
.SingleOrDefault();
124+
break;
125+
}
126+
}
114127

115128
return new LocalizedString(resourceKey, result!);
116129
}
@@ -147,7 +160,7 @@ public IStringLocalizer Create(string baseName, string location)
147160
sourceName = Shared;
148161
break;
149162
case NamingConventionEnum.FullTypeName:
150-
sourceName = $"{location}_{baseName}";
163+
sourceName = $"{location}_{baseName}";
151164
break;
152165
}
153166

Localization.AspNetCore.EntityFramework/Settings/LocalizerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace Localization.AspNetCore.EntityFramework.Settings
44
{
55
public class LocalizerOptions
66
{
7+
public FallBackBehaviorEnum FallBackBehavior { get; set; } = FallBackBehaviorEnum.DefaultCulture;
78
public NamingConventionEnum NamingConvention { get; set; } = NamingConventionEnum.TypeName;
8-
public bool ReturnKeyNameIfNoTranslation { get; set; } = true;
99
public bool CreateMissingTranslationsIfNotFound { get; set; } = true;
1010
}
1111
}

0 commit comments

Comments
 (0)