-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
fd3b131
commit 6fa5047
Showing
35 changed files
with
850 additions
and
320 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
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,10 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors. | ||
// All Rights Reserved. | ||
|
||
global using Microsoft.Extensions.DependencyInjection; | ||
global using Microsoft.Extensions.Localization; | ||
global using System; | ||
global using System.Collections.Generic; | ||
global using System.Linq; |
41 changes: 41 additions & 0 deletions
41
src/Lepo.i18n.DependencyInjection/Lepo.i18n.DependencyInjection.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>Lepo.i18n.DependencyInjection</PackageId> | ||
<TargetFrameworks>netstandard2.0;netstandard2.1;net462;net6.0;net8.0</TargetFrameworks> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<IsTrimmable>true</IsTrimmable> | ||
<EnableTrimAnalyzer>true</EnableTrimAnalyzer> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'"> | ||
<PublishAot>true</PublishAot> | ||
<StripSymbols>true</StripSymbols> | ||
<OptimizationPreference>Speed</OptimizationPreference> | ||
</PropertyGroup> | ||
|
||
<!-- Necessary polyfills --> | ||
<PropertyGroup> | ||
<PolySharpIncludeGeneratedTypes> | ||
System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute; | ||
System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute; | ||
System.Diagnostics.CodeAnalysis.MemberNotNullAttribute; | ||
System.Diagnostics.CodeAnalysis.NotNullAttribute; | ||
System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute; | ||
System.Diagnostics.CodeAnalysis.NotNullWhenAttribute; | ||
System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute; | ||
System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute; | ||
System.Runtime.CompilerServices.CallerArgumentExpressionAttribute; | ||
System.Runtime.CompilerServices.IsExternalInit; | ||
System.Runtime.CompilerServices.SkipLocalsInitAttribute; | ||
</PolySharpIncludeGeneratedTypes> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" /> | ||
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Lepo.i18n\Lepo.i18n.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors. | ||
// All Rights Reserved. | ||
|
||
global using System; | ||
global using System.Collections.Generic; | ||
global using System.Globalization; | ||
global using System.Text; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors. | ||
// All Rights Reserved. | ||
|
||
using System.Security.Cryptography; | ||
|
||
namespace Lepo.i18n.Yaml; | ||
|
||
/// <summary> | ||
/// Some weird YAML implementation. Don't ask me, it was supposed to be simple... | ||
/// </summary> | ||
internal class YamlDecoder | ||
{ | ||
/// <summary> | ||
/// Used to calculate a simple hash of a key in a dictionary to make searches faster. | ||
/// </summary> | ||
private static readonly MD5 Hasher = MD5.Create(); | ||
|
||
/// <summary> | ||
/// Creates a hashed <see langword="int"/> representation of <see langword="string"/>. | ||
/// </summary> | ||
/// <param name="value">Value to be hashed.</param> | ||
/// <returns></returns> | ||
public static uint Map(string value) | ||
{ | ||
return BitConverter.ToUInt32(Hasher.ComputeHash(Encoding.UTF8.GetBytes(value)), 0); | ||
} | ||
|
||
/// <summary> | ||
/// Creates new collection of mapped keys with translated values. | ||
/// </summary> | ||
/// <param name="rawYamlContent">String containing Yaml.</param> | ||
public static IDictionary<uint, string> FromString(string rawYamlContent) | ||
{ | ||
Dictionary<uint, string> keyValueCollection = new() { }; | ||
|
||
if (String.IsNullOrEmpty(rawYamlContent)) | ||
return keyValueCollection; | ||
|
||
string[] splittedYamlLines = rawYamlContent.Split( | ||
new[] { "\r\n", "\r", "\n" }, | ||
StringSplitOptions.None | ||
); | ||
|
||
// TODO: Recognize tab stops as subsections | ||
|
||
if (splittedYamlLines.Length < 1) | ||
return keyValueCollection; | ||
|
||
foreach (string yamlLine in splittedYamlLines) | ||
{ | ||
if (yamlLine.StartsWith("#") || String.IsNullOrEmpty(yamlLine)) | ||
continue; | ||
|
||
string[] pair = yamlLine.Split(new[] { ':' }, 2); | ||
|
||
if (pair.Length < 2) | ||
continue; | ||
|
||
uint mappedKey = Map(pair[0].Trim()); | ||
|
||
var translatedValue = pair[1].Trim(); | ||
|
||
if ( | ||
translatedValue.StartsWith("'") && translatedValue.EndsWith("'") | ||
|| translatedValue.StartsWith("\"") && translatedValue.EndsWith("\"") | ||
) | ||
translatedValue = translatedValue.Substring(1, translatedValue.Length - 2); | ||
|
||
if (!keyValueCollection.ContainsKey(mappedKey)) | ||
keyValueCollection.Add(mappedKey, translatedValue); | ||
} | ||
|
||
return keyValueCollection; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors. | ||
// All Rights Reserved. | ||
|
||
namespace Lepo.i18n; | ||
|
||
/// <summary> | ||
/// Provides functionality to retrieve localization sets for specific cultures. | ||
/// </summary> | ||
public interface ILocalizationProvider | ||
{ | ||
/// <summary> | ||
/// Retrieves the localization set for the specified culture. | ||
/// </summary> | ||
/// <param name="culture">The culture to get the localization set for.</param> | ||
/// <returns>The localization set for the specified culture, or null if no localization set is found.</returns> | ||
|
||
LocalizationSet? Get(CultureInfo culture); | ||
|
||
/// <summary> | ||
/// Retrieves the localization set with the specified name for the specified culture. | ||
/// </summary> | ||
/// <param name="name">The name of the localization set to get.</param> | ||
/// <param name="culture">The culture to get the localization set for.</param> | ||
/// <returns>The localization set with the specified name for the specified culture, or null if no localization set is found.</returns> | ||
|
||
LocalizationSet? Get(string name, CultureInfo culture); | ||
} |
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
Oops, something went wrong.