Skip to content

Commit

Permalink
Merge pull request #17 from LazZiya/vNext
Browse files Browse the repository at this point in the history
V next
  • Loading branch information
LazZiya authored Jan 1, 2021
2 parents b33d975 + 91e0c60 commit 278f8ad
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 42 deletions.
24 changes: 15 additions & 9 deletions XLocalizer/Common/ExpressMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ public class ExpressMemoryCache
private readonly IMemoryCache _cache;
private readonly XLocalizerOptions _options;
private readonly MemoryCacheEntryOptions _entryOps;
private string _keyFormat = "_Ex_Loc_{0}:{1}";
/// <summary>
/// _XL_ : XLocalizer
/// {0}: resource full name
/// {1}: culture name
/// {2}: key name
/// </summary>
private readonly string _keyFormat = "_XL_{0}_{1}:{2}";

/// <summary>
/// Create a new instance of ExpressMemoryCache
Expand All @@ -29,11 +35,11 @@ public ExpressMemoryCache(IMemoryCache cache, IOptions<MemoryCacheEntryOptions>
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void Set(string key, string value)
public void Set<T>(string key, string value) where T : class
{
if (_options.UseExpressMemoryCache)
{
var k = CreateFormattedKey(key);
var k = CreateFormattedKey<T>(key);

_cache.Set<string>(k, value, _entryOps);
}
Expand All @@ -43,11 +49,11 @@ public void Set(string key, string value)
/// Remove entry from cache
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
public void Remove<T>(string key) where T : class
{
if (_options.UseExpressMemoryCache)
{
var k = CreateFormattedKey(key);
var k = CreateFormattedKey<T>(key);

_cache.Remove(k);
}
Expand All @@ -59,11 +65,11 @@ public void Remove(string key)
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool TryGetValue(string key, out string value)
public bool TryGetValue<T>(string key, out string value) where T : class
{
if (_options.UseExpressMemoryCache)
{
var k = CreateFormattedKey(key);
var k = CreateFormattedKey<T>(key);

return _cache.TryGetValue(k, out value);
}
Expand All @@ -72,9 +78,9 @@ public bool TryGetValue(string key, out string value)
return false;
}

private string CreateFormattedKey(string key)
private string CreateFormattedKey<T>(string key) where T : class
{
return string.Format(_keyFormat, CultureInfo.CurrentCulture.Name, key);
return string.Format(_keyFormat, typeof(T).FullName, CultureInfo.CurrentCulture.Name, key);
}
}
}
1 change: 0 additions & 1 deletion XLocalizer/DataAnnotations/DataAnnotationsErrorMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public struct DataAnnotationsErrorMessages
/// </summary>
public const string UrlAttribute_Invalid = "The {0} field is not a valid fully-qualified http, https, or ftp URL.";

/// <summary>
/// <summary>
/// The field {0} is invalid.
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion XLocalizer/Resx/ResxWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ public async Task<bool> AddAsync(ResxElement element, bool overWriteExistingKeys
}
catch (Exception e)
{
throw e;
_logger.LogError("Error while adding element to resource file.");
_logger.LogError(e.Message);
tsk.SetException(e);
tsk.SetResult(false);
return await tsk.Task;
}
}

Expand Down
49 changes: 25 additions & 24 deletions XLocalizer/XLocalizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
<PackageIcon>icon.png</PackageIcon>
<PackageTags>asp.net,core,razor,mvc,localization,globalization,client side,validation,translation,autokeyadding</PackageTags>
<PackageReleaseNotes>
- New: `TranslateFromCulture` property to specify the source culture to translate from, if not specified default culture will be used.
- New: All default validation attributes errors, model binding errors and identity errors are customizable/localizable via `XLocalizerOptions` or Json See https://docs.ziyad.info/en/XLocalizer/v1.0/setup-json.md.
Deprected:
- Express validation attributes are deprected. Use default attributes instead. A new approach has been introduced that allows to customize/localize all framework default validation attributes without the need to use Ex valdiaton attributes.
See all docs: https://docs.ziyad.info/en/XLocalizer/v1.0/index.md

Release notes: https://github.com/LazZiya/XLocalizer/releases
</PackageReleaseNotes>
<VersionPrefix>1.0.0</VersionPrefix>
Improvements
- Create localization folder if not exists (XmlResourceProvider)
Fixes:
- Fixed #9 - Failed to find out ResourcesPath when hosted in linux
- Fixed #13 - Similar keys that are stored in different resources caching the same value
See all docs: https://docs.ziyad.info/en/XLocalizer/v1.0/index.md

Release notes: https://github.com/LazZiya/XLocalizer/releases
</PackageReleaseNotes>
<VersionPrefix>1.0.1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<FileVersion>1.0.1.0</FileVersion>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIconUrl>https://github.com/LazZiya/XLocalizer/raw/master/XLocalizer/files/icon.png</PackageIconUrl>
Expand All @@ -39,7 +40,7 @@
<PropertyGroup>
<UseReference>Nugets</UseReference>
</PropertyGroup>

<ItemGroup Condition="'$(UseReference)' == 'Projects'">
</ItemGroup>

Expand Down Expand Up @@ -73,23 +74,23 @@
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<FrameworkReference Include="Microsoft.AspNetCore.App" Version="3.1.0" Exclude="Build,Analyzers" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<FrameworkReference Include="Microsoft.AspNetCore.App" Version="5.0.0" Exclude="Build,Analyzers" />
</ItemGroup>

<ItemGroup>
<None Remove="Resx\ResxTemplate.xml" />
<None Remove="Resx\ResxTemplate.xml" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Templates\ResxTemplate.xml">
<Generator></Generator>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Templates\XmlTemplate.xml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Templates\ResxTemplate.xml">
<Generator></Generator>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Templates\XmlTemplate.xml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
Expand All @@ -103,7 +104,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
<Folder Include="Properties\" />
</ItemGroup>

<Target Name="AfterPack" AfterTargets="Pack">
Expand Down
20 changes: 16 additions & 4 deletions XLocalizer/XLocalizer.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions XLocalizer/XStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private LocalizedString GetLocalizedString(string name, params object[] argument
var availableInTranslate = false;

// Option 1: Look in the cache
bool availableInCache = _cache.TryGetValue(name, out string value);
bool availableInCache = _cache.TryGetValue<TResource>(name, out string value);

if (!availableInCache)
{
Expand Down Expand Up @@ -121,7 +121,7 @@ private LocalizedString GetLocalizedString(string name, params object[] argument
if (availableInSource || availableInTranslate)
{
// Save to cache
_cache.Set(name, value);
_cache.Set<TResource>(name, value);

// Set availability to true
availableInCache = true;
Expand Down
5 changes: 4 additions & 1 deletion XLocalizer/Xml/XmlResourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ private string ResourcePath<TResource>()
{
return _cache.GetOrAdd($"_xml_{typeof(TResource).FullName}", _ =>
{
if (!Directory.Exists(_options.ResourcesPath))
Directory.CreateDirectory(_options.ResourcesPath);

string typeName = ResourceTypeHelper.CreateResourceName(typeof(TResource), _options.ResourcesPath);

return $".\\{_options.ResourcesPath}\\{typeName}.{{0}}.xml";
return $"{_options.ResourcesPath}{Path.DirectorySeparatorChar}{typeName}.{{0}}.xml";
});
}

Expand Down

0 comments on commit 278f8ad

Please sign in to comment.