This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
367407b
commit 72ea4fc
Showing
37 changed files
with
631 additions
and
81 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
Askmethat.Aspnet.JsonLocalizer/Askmethat.Aspnet.JsonLocalizer.csproj
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
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
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
8 changes: 8 additions & 0 deletions
8
Askmethat.Aspnet.JsonLocalizer/JsonOptions/LocalizationMode.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,8 @@ | ||
namespace Askmethat.Aspnet.JsonLocalizer.JsonOptions | ||
{ | ||
public enum LocalizationMode | ||
{ | ||
Basic, | ||
I18n | ||
} | ||
} |
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
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
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
2 changes: 1 addition & 1 deletion
2
Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerOfT.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
15 changes: 15 additions & 0 deletions
15
Askmethat.Aspnet.JsonLocalizer/Localizer/Modes/ILocalisationModeGenerator.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,15 @@ | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Askmethat.Aspnet.JsonLocalizer.Format; | ||
using Askmethat.Aspnet.JsonLocalizer.JsonOptions; | ||
|
||
namespace Askmethat.Aspnet.JsonLocalizer.Localizer.Modes | ||
{ | ||
internal interface ILocalisationModeGenerator | ||
{ | ||
ConcurrentDictionary<string, LocalizatedFormat> ConstructLocalization( | ||
IEnumerable<string> myFiles, CultureInfo currentCulture, JsonLocalizationOptions options); | ||
|
||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
Askmethat.Aspnet.JsonLocalizer/Localizer/Modes/LocalisationBasicModeGenerator.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,83 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using Askmethat.Aspnet.JsonLocalizer.Format; | ||
using Askmethat.Aspnet.JsonLocalizer.JsonOptions; | ||
using Newtonsoft.Json; | ||
|
||
namespace Askmethat.Aspnet.JsonLocalizer.Localizer.Modes | ||
{ | ||
internal class LocalisationBasicModeGenerator : ILocalisationModeGenerator | ||
{ | ||
private ConcurrentDictionary<string, LocalizatedFormat> localization = | ||
new ConcurrentDictionary<string, LocalizatedFormat>(); | ||
|
||
private JsonLocalizationOptions _options; | ||
|
||
public ConcurrentDictionary<string, LocalizatedFormat> ConstructLocalization( | ||
IEnumerable<string> myFiles, CultureInfo currentCulture, JsonLocalizationOptions options) | ||
{ | ||
_options = options; | ||
|
||
foreach (string file in myFiles) | ||
{ | ||
ConcurrentDictionary<string, JsonLocalizationFormat> tempLocalization = LocalisationModeHelpers.ReadAndDeserializeFile<string,JsonLocalizationFormat>(file, options.FileEncoding); | ||
|
||
if (tempLocalization == null) | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (KeyValuePair<string, JsonLocalizationFormat> temp in tempLocalization) | ||
{ | ||
LocalizatedFormat localizedValue = GetLocalizedValue(currentCulture, temp); | ||
if (!(localizedValue.Value is null)) | ||
{ | ||
if (!localization.ContainsKey(temp.Key)) | ||
{ | ||
localization.TryAdd(temp.Key, localizedValue); | ||
} | ||
else if (localization[temp.Key].IsParent) | ||
{ | ||
localization[temp.Key] = localizedValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return localization; | ||
} | ||
|
||
private LocalizatedFormat GetLocalizedValue(CultureInfo currentCulture, | ||
KeyValuePair<string, JsonLocalizationFormat> temp) | ||
{ | ||
bool isParent = false; | ||
string value = temp.Value.Values.FirstOrDefault(s => | ||
string.Equals(s.Key, currentCulture.Name, StringComparison.OrdinalIgnoreCase)).Value; | ||
if (value is null) | ||
{ | ||
isParent = true; | ||
value = temp.Value.Values.FirstOrDefault(s => | ||
string.Equals(s.Key, currentCulture.Parent.Name, StringComparison.OrdinalIgnoreCase)).Value; | ||
if (value is null) | ||
{ | ||
value = temp.Value.Values.FirstOrDefault(s => string.IsNullOrWhiteSpace(s.Key)).Value; | ||
if (value is null && _options.DefaultCulture != null) | ||
{ | ||
value = temp.Value.Values.FirstOrDefault(s => string.Equals(s.Key, | ||
_options.DefaultCulture.Name, StringComparison.OrdinalIgnoreCase)).Value; | ||
} | ||
} | ||
} | ||
|
||
return new LocalizatedFormat() | ||
{ | ||
IsParent = isParent, | ||
Value = value | ||
}; | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
Askmethat.Aspnet.JsonLocalizer/Localizer/Modes/LocalisationI18nModeGenerator.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,96 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using Askmethat.Aspnet.JsonLocalizer.Format; | ||
using Askmethat.Aspnet.JsonLocalizer.JsonOptions; | ||
using Newtonsoft.Json; | ||
|
||
namespace Askmethat.Aspnet.JsonLocalizer.Localizer.Modes | ||
{ | ||
internal class LocalisationI18nModeGenerator : ILocalisationModeGenerator | ||
{ | ||
private ConcurrentDictionary<string, LocalizatedFormat> localization = | ||
new ConcurrentDictionary<string, LocalizatedFormat>(); | ||
|
||
private JsonLocalizationOptions _options; | ||
|
||
public ConcurrentDictionary<string, LocalizatedFormat> ConstructLocalization(IEnumerable<string> myFiles, | ||
CultureInfo currentCulture, | ||
JsonLocalizationOptions options) | ||
{ | ||
_options = options; | ||
|
||
var neutralFile = myFiles.FirstOrDefault(file => file.Split("/") | ||
.Last().Count(s => s.CompareTo('.') == 0) == 1); | ||
|
||
var cultureExistInFiles = myFiles.Any(file => file.Split("/").Any( | ||
s => s.Contains(currentCulture.Name, StringComparison.OrdinalIgnoreCase) | ||
|| s.Contains(currentCulture.Parent.Name, StringComparison.OrdinalIgnoreCase) | ||
)) && currentCulture.DisplayName != CultureInfo.InvariantCulture.ThreeLetterISOLanguageName; | ||
|
||
if (cultureExistInFiles) | ||
{ | ||
foreach (string file in myFiles) | ||
{ | ||
var splittedFiles = file.Split("/"); | ||
var fileCulture = new CultureInfo(splittedFiles[^1].Split(".")[1]); | ||
|
||
var isParent = | ||
fileCulture.Name.Equals(currentCulture.Parent.Name, StringComparison.OrdinalIgnoreCase); | ||
|
||
if (fileCulture.Name.Equals(currentCulture.Name, StringComparison.OrdinalIgnoreCase) || | ||
isParent && fileCulture.Name != "json") | ||
{ | ||
AddValueToLocalization(options, file, isParent); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
AddValueToLocalization(options, neutralFile, true); | ||
} | ||
|
||
return localization; | ||
} | ||
|
||
private void AddValueToLocalization(JsonLocalizationOptions options, string file, bool isParent) | ||
{ | ||
ConcurrentDictionary<string, string> tempLocalization = | ||
LocalisationModeHelpers.ReadAndDeserializeFile<string, string>(file, options.FileEncoding); | ||
|
||
if (tempLocalization == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var temp in tempLocalization) | ||
{ | ||
LocalizatedFormat localizedValue = GetLocalizedValue(temp, isParent); | ||
|
||
if (!(localizedValue.Value is null)) | ||
{ | ||
if (!localization.ContainsKey(temp.Key)) | ||
{ | ||
localization.TryAdd(temp.Key, localizedValue); | ||
} | ||
else if (localization[temp.Key].IsParent) | ||
{ | ||
localization[temp.Key] = localizedValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private LocalizatedFormat GetLocalizedValue(KeyValuePair<string, string> temp, bool isParent) | ||
{ | ||
return new LocalizatedFormat() | ||
{ | ||
IsParent = isParent, | ||
Value = temp.Value | ||
}; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Askmethat.Aspnet.JsonLocalizer/Localizer/Modes/LocalisationModeHelpers.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,18 @@ | ||
using System.Collections.Concurrent; | ||
using System.IO; | ||
using System.Text; | ||
using Askmethat.Aspnet.JsonLocalizer.JsonOptions; | ||
using Newtonsoft.Json; | ||
|
||
namespace Askmethat.Aspnet.JsonLocalizer.Localizer.Modes | ||
{ | ||
public static class LocalisationModeHelpers | ||
{ | ||
public static ConcurrentDictionary<T, U> ReadAndDeserializeFile<T,U>(string file, Encoding encoding) | ||
{ | ||
return | ||
JsonConvert.DeserializeObject<ConcurrentDictionary<T, U>>( | ||
File.ReadAllText(file, encoding)); | ||
} | ||
} | ||
} |
Oops, something went wrong.