-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to use IStringLocalizerFactory/IStringLocalizer implement…
…ation (#37) * Add ability to use IStringLocalizerFactory/IStrinLocilizer implemntations * Update README.md * fix typo
- Loading branch information
Showing
15 changed files
with
660 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/HotChocolate.Extensions.Translation.Tests/TestResourceType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using HotChocolate.Extensions.Translation.Resources; | ||
|
||
namespace HotChocolate.Extensions.Translation.Tests; | ||
|
||
[ResourceTypeAlias(Path)] | ||
public class TestResourceType | ||
{ | ||
public const string Path = "myNodePath"; | ||
} |
50 changes: 50 additions & 0 deletions
50
src/HotChocolate.Extensions.Translation.Tests/TestStringLocalizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Threading; | ||
using HotChocolate.Extensions.Translation.Resources; | ||
using HotChocolate.Extensions.Translation.Tests.Mock; | ||
using Microsoft.Extensions.Localization; | ||
|
||
namespace HotChocolate.Extensions.Translation.Tests; | ||
|
||
public class TestStringLocalizer : IStringLocalizer | ||
{ | ||
private readonly DictionaryResourcesProviderAdapter _dictionaryResourcesProviderAdapter; | ||
|
||
public TestStringLocalizer( | ||
Func<IDictionary<Mock.Language, Dictionary<string, Resource>>> func) | ||
{ | ||
_dictionaryResourcesProviderAdapter = | ||
new DictionaryResourcesProviderAdapter(func()); | ||
} | ||
|
||
public LocalizedString this[string name] | ||
{ | ||
get | ||
{ | ||
CultureInfo culture = Thread.CurrentThread.CurrentCulture; | ||
|
||
string notFound = "*** NOT FOUND ***"; | ||
|
||
var translatedValue = | ||
_dictionaryResourcesProviderAdapter.TryGetTranslationAsStringAsync( | ||
$"{TestResourceType.Path}/{name}", culture, notFound, CancellationToken.None) | ||
.GetAwaiter().GetResult(); | ||
|
||
return new LocalizedString( | ||
name, | ||
translatedValue == notFound ? name : translatedValue, | ||
resourceNotFound: translatedValue == notFound); | ||
} | ||
} | ||
|
||
public LocalizedString this[string name, params object[] arguments] => | ||
new LocalizedString(name, string.Format(this[name].Value, arguments)); | ||
|
||
|
||
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
Oops, something went wrong.