Skip to content

Commit

Permalink
Merge branch 'hangy-issue341'
Browse files Browse the repository at this point in the history
  • Loading branch information
valdisiljuconoks committed Nov 27, 2024
2 parents 4c0d66e + b971242 commit 657774e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DbLocalizationProvider.Internal;
using DbLocalizationProvider.Internal;
using DbLocalizationProvider.Queries;
using DbLocalizationProvider.Refactoring;
using DbLocalizationProvider.Sync;
Expand Down Expand Up @@ -44,7 +44,7 @@ public void ConvertToDictionary()

Assert.NotNull(result);
Assert.Single(result);
Assert.True(result.ContainsKey("DbLocalizationProvider.Tests.DictionaryConvertTests.ResourceToDictionaryModel.Property1"));
Assert.True(result.ContainsKey("Property1"));
}


Expand All @@ -54,7 +54,7 @@ public void ConvertToDictionary_HiddenResource_ShouldNotInclude()
var result = _provider.ToDictionary<ResourceToDictionaryModelWithHiddenProperty>();

Assert.Single(result);
Assert.True(result.ContainsKey("DbLocalizationProvider.Tests.DictionaryConvertTests.ResourceToDictionaryModelWithHiddenProperty.Property1"));
Assert.True(result.ContainsKey("Property1"));
}
}

Expand Down
19 changes: 11 additions & 8 deletions common/src/DbLocalizationProvider/LocalizationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class LocalizationProvider : ILocalizationProvider
private readonly ResourceKeyBuilder _keyBuilder;
internal readonly IQueryExecutor _queryExecutor;
private readonly ScanState _scanState;
private readonly JsonConverter _converter;
private readonly JsonSerializer _serializer;

/// <summary>
/// Creates new localization provider with all the required settings and services injected.
Expand All @@ -52,6 +54,12 @@ public LocalizationProvider(
_fallbackCollection = context.Value._fallbackCollection;
_queryExecutor = queryExecutor;
_scanState = scanState;

_converter = new JsonConverter(_queryExecutor, _scanState);
_serializer = new JsonSerializer
{
ContractResolver = new StaticPropertyContractResolver()
};
}

/// <summary>
Expand Down Expand Up @@ -220,25 +228,20 @@ public T Translate<T>()
/// <returns>Translated class</returns>
public T Translate<T>(CultureInfo language)
{
var converter = new JsonConverter(_queryExecutor, _scanState);
var className = typeof(T).FullName;

var json = converter.GetJson(className, language.Name, _fallbackCollection);
var json = _converter.GetJson(className, language.Name, _fallbackCollection);

// get the actual class Json representation (we need to select token through FQN of the class)
// to supported nested classes - we need to fix a bit resource key name
var jsonToken = json.SelectToken(className.Replace("+", "."));
var jsonToken = json.SelectToken(className.Replace('+', '.'));

if (jsonToken == null)
{
return (T)Activator.CreateInstance(typeof(T), new object[] { });
}

return JsonConvert.DeserializeObject<T>(jsonToken.ToString(),
new JsonSerializerSettings
{
ContractResolver = new StaticPropertyContractResolver()
});
return jsonToken.ToObject<T>(_serializer);
}

/// <summary>
Expand Down

0 comments on commit 657774e

Please sign in to comment.