Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #55 from AlexTeixeira/development
Browse files Browse the repository at this point in the history
Big update
  • Loading branch information
AlexTeixeira authored Jun 4, 2019
2 parents 644132f + c7c8dfe commit 62809ca
Show file tree
Hide file tree
Showing 44 changed files with 606 additions and 496 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/test/Askmethat.Aspnet.JsonLocalizer.TestSample/bin/Debug/netcoreapp2.0/Askmethat.Aspnet.JsonLocalizer.TestSample.dll",
"program": "${workspaceFolder}/test/Askmethat.Aspnet.JsonLocalizer.TestSample/bin/Debug/netcoreapp2.2/Askmethat.Aspnet.JsonLocalizer.TestSample.dll",
"args": [],
"cwd": "${workspaceFolder}/test/Askmethat.Aspnet.JsonLocalizer.TestSample",
"stopAtEntry": false,
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet-test-explorer.testProjectPath": "test/**/*Test.csproj"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
<RepositoryType>Git</RepositoryType>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.*" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.*" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="2.*" />
<PackageReference Include="Microsoft.AspNetCore.Localization" Version="2.*" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,36 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Askmethat.Aspnet.JsonLocalizer.Extensions
{
public class JsonLocalizationOptions : LocalizationOptions
{

const char PLURAL_SEPARATOR = '|';
const string DEFAULT_RESOURCES = "Resources";
const string DEFAULT_CULTURE = "en-US";
private const char PLURAL_SEPARATOR = '|';
private const string DEFAULT_RESOURCES = "Resources";
private const string DEFAULT_CULTURE = "en-US";

public new string ResourcesPath { get; set; } = DEFAULT_RESOURCES;
/// We cache all values to memory to avoid loading files for each request, this parameter defines the time after which the cache is refreshed.
public TimeSpan CacheDuration { get; set; } = TimeSpan.FromMinutes(30);

/// <summary>
/// This property stores the MemoryCache for the cached translations.
/// </summary>
public IMemoryCache Caching { get; set; } = new MemoryCache(new MemoryCacheOptions
{
});

private CultureInfo defaultCulture = new CultureInfo(DEFAULT_CULTURE);

/// <summary>
/// Sets the default culture to use.
/// </summary>
public CultureInfo DefaultCulture
{
get
{
return defaultCulture;
}
set
get => defaultCulture;
set
{
if (value != defaultCulture)
{
Expand All @@ -40,15 +43,15 @@ public CultureInfo DefaultCulture

private HashSet<CultureInfo> supportedCultureInfos = new HashSet<CultureInfo>
{

};

/// <summary>
/// Optional array of cultures that you should provide to plugin. (Like RequestLocalizationOptions)
/// </summary>
public HashSet<CultureInfo> SupportedCultureInfos
{
get
{
return supportedCultureInfos;
}
get => supportedCultureInfos;
set
{
if (value != supportedCultureInfos)
Expand All @@ -59,11 +62,22 @@ public HashSet<CultureInfo> SupportedCultureInfos
}


/// <summary>
/// Look for an absolute path instead of project path.
/// </summary>
public bool IsAbsolutePath { get; set; } = false;

/// <summary>
/// Specify the file encoding.
/// </summary>
public Encoding FileEncoding { get; set; } = Encoding.UTF8;

/// <summary>
/// Use base name location for Views and constructors like default Resx localization in ResourcePathFolder.
/// </summary>
public bool UseBaseName { get; set; } = false;
/// <summary>
/// Seperator used to get singular or pluralized version of localization.
/// </summary>
public char PluralSeparator { get; set; } = PLURAL_SEPARATOR;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Askmethat.Aspnet.JsonLocalizer.Localizer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.Text;

namespace Askmethat.Aspnet.JsonLocalizer.Extensions
{
Expand All @@ -25,7 +22,7 @@ public static IServiceCollection AddJsonLocalization(this IServiceCollection ser
throw new ArgumentNullException(nameof(services));
}

services.AddOptions();
_ = services.AddOptions();

AddJsonLocalizationServices(services);

Expand Down Expand Up @@ -60,9 +57,9 @@ public static IServiceCollection AddJsonLocalization(

internal static void AddJsonLocalizationServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddSingleton<IStringLocalizerFactory, JsonStringLocalizerFactory>();
services.AddScoped<IStringLocalizer, JsonStringLocalizer>();
_ = services.AddMemoryCache();
_ = services.AddSingleton<IStringLocalizerFactory, JsonStringLocalizerFactory>();
_ = services.AddScoped<IStringLocalizer, JsonStringLocalizer>();
}

/// <summary>
Expand All @@ -75,7 +72,7 @@ internal static void AddJsonLocalizationServices(
Action<JsonLocalizationOptions> setupAction)
{
AddJsonLocalizationServices(services);
services.Configure(setupAction);
_ = services.Configure(setupAction);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;

namespace Askmethat.Aspnet.JsonLocalizer.Format
{
Expand Down
41 changes: 18 additions & 23 deletions Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Askmethat.Aspnet.JsonLocalizer.Extensions;
using Askmethat.Aspnet.JsonLocalizer.Format;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -15,18 +13,16 @@ namespace Askmethat.Aspnet.JsonLocalizer.Localizer
{
internal class JsonStringLocalizer : JsonStringLocalizerBase, IStringLocalizer
{
readonly IHostingEnvironment _env;
private readonly IHostingEnvironment _env;

public JsonStringLocalizer(IOptions<JsonLocalizationOptions> localizationOptions, IHostingEnvironment env, string baseName = null) : base(localizationOptions, baseName)
{
_env = env;
_resourcesRelativePath = GetJsonRelativePath(_localizationOptions.Value.ResourcesPath);
resourcesRelativePath = GetJsonRelativePath(_localizationOptions.Value.ResourcesPath);

InitJsonStringLocalizer();
}



public LocalizedString this[string name]
{
get
Expand All @@ -49,24 +45,24 @@ public LocalizedString this[string name]
private string GetPluralLocalization(string name, string format, object[] arguments)
{
object last = arguments.LastOrDefault();
string value = string.Empty;
if (last != null && last is bool)
string value;
if (last != null && last is bool boolean)
{
bool isPlural = (bool)last;
bool isPlural = boolean;
value = GetString(name);
if (value.Contains(_localizationOptions.Value.PluralSeparator))
if (!string.IsNullOrEmpty(value) && value.Contains(_localizationOptions.Value.PluralSeparator))
{
int index = (isPlural ? 1 : 0);
int index = isPlural ? 1 : 0;
value = value.Split(_localizationOptions.Value.PluralSeparator)[index];
}
else
{
value = String.Format(format ?? name, arguments);
value = string.Format(format ?? name, arguments);
}
}
else
{
value = String.Format(format ?? name, arguments);
value = string.Format(format ?? name, arguments);
}

return value;
Expand Down Expand Up @@ -99,23 +95,22 @@ public IStringLocalizer WithCulture(CultureInfo culture)
return new JsonStringLocalizer(_localizationOptions, _env);
}

string GetString(string name, bool shouldTryDefaultCulture = true)
private string GetString(string name, bool shouldTryDefaultCulture = true)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}

LocalizatedFormat localizedValue = null;

if (shouldTryDefaultCulture && !IsUICultureCurrentCulture(CultureInfo.CurrentUICulture))
{
InitJsonStringLocalizer(CultureInfo.CurrentUICulture);
AddMissingCultureToSupportedCulture(CultureInfo.CurrentUICulture);
GetCultureToUse(CultureInfo.CurrentUICulture);
}

if (localization != null && localization.TryGetValue(name, out localizedValue))

if (localization != null && localization.TryGetValue(name, out LocalizatedFormat localizedValue))
{
return localizedValue.Value;
}
Expand All @@ -128,28 +123,28 @@ string GetString(string name, bool shouldTryDefaultCulture = true)

//advert user that current name string does not
//contains any translation
Console.Error.WriteLine($"{name} does not contains any translation");
Console.Error.WriteLine($"{name} does not contain any translation");
return null;
}

/// <summary>
/// Get path of json
/// </summary>
/// <returns>JSON relative path</returns>
string GetJsonRelativePath(string path)
private string GetJsonRelativePath(string path)
{
string fullPath = string.Empty;
if (this._localizationOptions.Value.IsAbsolutePath)
if (_localizationOptions.Value.IsAbsolutePath)
{
fullPath = path;
}
if (!this._localizationOptions.Value.IsAbsolutePath && string.IsNullOrEmpty(path))
if (!_localizationOptions.Value.IsAbsolutePath && string.IsNullOrEmpty(path))
{
fullPath = Path.Combine(_env.ContentRootPath, "Resources");
}
else if (!this._localizationOptions.Value.IsAbsolutePath && !string.IsNullOrEmpty(path))
else if (!_localizationOptions.Value.IsAbsolutePath && !string.IsNullOrEmpty(path))
{
fullPath = Path.Combine(AppContext.BaseDirectory,path);
fullPath = Path.Combine(AppContext.BaseDirectory, path2: path);
}
return fullPath;
}
Expand Down
Loading

0 comments on commit 62809ca

Please sign in to comment.