add culture based caching -> 0.0.4

This commit is contained in:
2023-05-20 11:30:33 +00:00
parent 37a7c50e4d
commit 2642f54334
19 changed files with 186 additions and 55 deletions

View File

@@ -93,6 +93,16 @@ namespace MyTestConsoleApp
); );
public static ResourceManager ResourceManager => _ResourceManager.Value; public static ResourceManager ResourceManager => _ResourceManager.Value;
private static CultureInfo GetDefaultCulture()
{
return CultureInfo.CurrentCulture;
}
private static IResourceString AddToCultureCache(IResourceString source)
{
return new CultureBasedCachedString(source, GetDefaultCulture);
}
#endregion // ResourceManager #endregion // ResourceManager
@@ -101,16 +111,17 @@ namespace MyTestConsoleApp
internal static class Greetings internal static class Greetings
{ {
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("Greetings", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("Greetings", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
public static IResourceString Format => LazyFormat.Value; public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString name) => new FormattedResourceString( public static IResourceString From(IResourceString name) => AddToCultureCache(new FormattedResourceString(
Format, Format,
GetDefaultCulture,
name name
); ));
} }
@@ -119,7 +130,7 @@ namespace MyTestConsoleApp
#region World #region World
private static readonly Lazy<IResourceString> LazyWorld = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyWorld = new Lazy<IResourceString>(
() => new ResourceManagerString("World", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("World", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
@@ -181,7 +192,7 @@ the source code generator transforms it into the following `C#` class members:
#region World #region World
private static readonly Lazy<IResourceString> LazyWorld = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyWorld = new Lazy<IResourceString>(
() => new ResourceManagerString("World", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("World", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
@@ -206,17 +217,18 @@ the generator generates following code to support the formatted string:
internal static class Greetings internal static class Greetings
{ {
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("Greetings", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("Greetings", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
public static IResourceString Format => LazyFormat.Value; public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString p1, IResourceString p2) => new FormattedResourceString( public static IResourceString From(IResourceString p1, IResourceString p2) => AddToCultureCache(new FormattedResourceString(
Format, Format,
GetDefaultCulture,
p1, p1,
p2 p2
); ));
} }
#endregion // Greetings #endregion // Greetings
@@ -234,11 +246,11 @@ In cases where the element with the format string includes a comment element lik
the source generator extracts the parameter names from the comment instead of using generic names: the source generator extracts the parameter names from the comment instead of using generic names:
```cs ```cs
public static IResourceString From(IResourceString name, IResourceString otherName) => new FormattedResourceString( public static IResourceString From(IResourceString name, IResourceString otherName) => AddToCultureCache(new FormattedResourceString(
Format, Format,
name, name,
otherName otherName
); ));
``` ```
This allows for more descriptive parameter names in the generated code. This allows for more descriptive parameter names in the generated code.
@@ -278,7 +290,10 @@ This allows for more descriptive parameter names in the generated code.
- Provides access to resource strings stored in `resx` files or other resource sources. - Provides access to resource strings stored in `resx` files or other resource sources.
- Handles retrieving the localized value for the specified `CultureInfo`. - Handles retrieving the localized value for the specified `CultureInfo`.
Great! Based on the provided code for the console app `ResourceString.Net.App.Console`, let's enhance the Readme.md file to include instructions on how to use the console app and generate a C# class based on a given resource file. ### CultureBasedCachedString
- Implements the `IResourceString` interface.
- Enhances performance by avoiding redundant resource string lookups and reducing the overhead associated with repeated string generation.
## Console App: ResourceString.Net.App.Console ## Console App: ResourceString.Net.App.Console

View File

@@ -9,6 +9,11 @@
<PackAsTool>true</PackAsTool> <PackAsTool>true</PackAsTool>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<Deterministic>true</Deterministic>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="../ResourceString.Net.Logic/ResourceString.Net.Logic.csproj" /> <ProjectReference Include="../ResourceString.Net.Logic/ResourceString.Net.Logic.csproj" />
</ItemGroup> </ItemGroup>

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Globalization;
namespace ResourceString.Net.Contract
{
public sealed class CultureBasedCachedString : IResourceString
{
private readonly Func<CultureInfo> m_GetDefaultCulture;
private readonly IResourceString m_ResourceString;
private readonly Dictionary<CultureInfo, string> m_Cache = new Dictionary<CultureInfo, string>();
public CultureBasedCachedString(
IResourceString resourceString,
Func<CultureInfo>? getDefaultCulture = default)
{
m_ResourceString = resourceString;
m_GetDefaultCulture = getDefaultCulture ?? (() => CultureInfo.CurrentCulture);
}
public string Value => GetValue(m_GetDefaultCulture());
public string GetValue(CultureInfo cultureInfo)
{
if (m_Cache.TryGetValue(cultureInfo, out var value))
{
return value;
}
var newValue = m_ResourceString.GetValue(cultureInfo);
m_Cache[cultureInfo] = newValue;
return newValue;
}
}
}

View File

@@ -1,11 +1,14 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System;
namespace ResourceString.Net.Contract namespace ResourceString.Net.Contract
{ {
public class FormattedResourceString : IResourceString public sealed class FormattedResourceString : IResourceString
{ {
private readonly Func<CultureInfo> m_GetDefaultCulture;
public IResourceString Format { get; } public IResourceString Format { get; }
public IEnumerable<IResourceString> Parameters { get; } public IEnumerable<IResourceString> Parameters { get; }
@@ -14,14 +17,17 @@ namespace ResourceString.Net.Contract
public FormattedResourceString( public FormattedResourceString(
IResourceString format, IResourceString format,
Func<CultureInfo>? getDefaultCulture = default,
params IResourceString[] parameters) params IResourceString[] parameters)
{ {
Format = format ?? new JoinedResourceString( Format = format ?? new JoinedResourceString(
LiteralString.Factory(","), LiteralString.Factory(","),
getDefaultCulture,
parameters.Select(((p, idx) => LiteralString.Factory("{" + idx + "}"))).ToArray() parameters.Select(((p, idx) => LiteralString.Factory("{" + idx + "}"))).ToArray()
); );
Parameters = parameters; Parameters = parameters;
m_GetDefaultCulture = getDefaultCulture ?? (() => CultureInfo.CurrentCulture);
} }
public string GetValue(CultureInfo cultureInfo) public string GetValue(CultureInfo cultureInfo)

View File

@@ -1,12 +1,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System;
namespace ResourceString.Net.Contract namespace ResourceString.Net.Contract
{ {
public class JoinedResourceString : IResourceString public sealed class JoinedResourceString : IResourceString
{ {
private readonly Func<CultureInfo> m_GetDefaultCulture;
public IResourceString Separator { get; } public IResourceString Separator { get; }
public IEnumerable<IResourceString> Elements { get; } public IEnumerable<IResourceString> Elements { get; }
@@ -15,6 +18,7 @@ namespace ResourceString.Net.Contract
public JoinedResourceString( public JoinedResourceString(
IResourceString separator, IResourceString separator,
Func<CultureInfo>? getDefaultCulture = default,
params IResourceString[] elements) params IResourceString[] elements)
{ {
Separator = separator ?? new LiteralString( Separator = separator ?? new LiteralString(
@@ -22,6 +26,7 @@ namespace ResourceString.Net.Contract
); );
Elements = elements; Elements = elements;
m_GetDefaultCulture = getDefaultCulture ?? (() => CultureInfo.CurrentCulture);
} }
public string GetValue(CultureInfo cultureInfo) public string GetValue(CultureInfo cultureInfo)

View File

@@ -4,7 +4,7 @@ using System.Threading;
namespace ResourceString.Net.Contract namespace ResourceString.Net.Contract
{ {
public class LiteralString : IResourceString public sealed class LiteralString : IResourceString
{ {
private Lazy<string> m_Value; private Lazy<string> m_Value;

View File

@@ -6,22 +6,22 @@ namespace ResourceString.Net.Contract
{ {
public class ResourceManagerString : IResourceString public class ResourceManagerString : IResourceString
{ {
private readonly CultureInfo m_Culture; private readonly Func<CultureInfo> m_GetDefaultCulture;
public string Id { get; } public string Id { get; }
public ResourceManager Manager { get; } public ResourceManager Manager { get; }
public string Value => GetValue(m_Culture); public string Value => GetValue(m_GetDefaultCulture());
public ResourceManagerString( public ResourceManagerString(
string id, string id,
ResourceManager manager, ResourceManager manager,
CultureInfo cultureInfo) Func<CultureInfo>? getDefaultCulture = default)
{ {
Id = id ?? string.Empty; Id = id ?? string.Empty;
Manager = manager ?? throw new ArgumentNullException(nameof(manager)); Manager = manager ?? throw new ArgumentNullException(nameof(manager));
m_Culture = cultureInfo ?? CultureInfo.CurrentCulture; m_GetDefaultCulture = getDefaultCulture ?? (() => CultureInfo.CurrentCulture);
} }
public string GetValue(CultureInfo cultureInfo) public string GetValue(CultureInfo cultureInfo)

View File

@@ -6,4 +6,9 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<Deterministic>true</Deterministic>
<Optimize>true</Optimize>
</PropertyGroup>
</Project> </Project>

View File

@@ -13,6 +13,7 @@ public class LogicResourcesTests
Assert.AreEqual( Assert.AreEqual(
new FormattedResourceString( new FormattedResourceString(
Properties.Resources.ResourceStringMembers.Format, Properties.Resources.ResourceStringMembers.Format,
default,
LiteralString.Factory("ResourceStringMembers"), LiteralString.Factory("ResourceStringMembers"),
LiteralString.Factory("ResourceManager") LiteralString.Factory("ResourceManager")
).Value, ).Value,
@@ -31,6 +32,7 @@ public class LogicResourcesTests
Assert.AreEqual( Assert.AreEqual(
new FormattedResourceString( new FormattedResourceString(
Properties.Resources.ResourceFormatClassMembers.Format, Properties.Resources.ResourceFormatClassMembers.Format,
default,
LiteralString.Factory("ResourceFormatClassMembers"), LiteralString.Factory("ResourceFormatClassMembers"),
LiteralString.Factory("ResourceManager"), LiteralString.Factory("ResourceManager"),
LiteralString.Factory("Format") LiteralString.Factory("Format")
@@ -51,6 +53,7 @@ public class LogicResourcesTests
Assert.AreEqual( Assert.AreEqual(
new FormattedResourceString( new FormattedResourceString(
Properties.Resources.ResourceFormatClassFromMethod.Format, Properties.Resources.ResourceFormatClassFromMethod.Format,
default,
LiteralString.Factory("IResourceString name"), LiteralString.Factory("IResourceString name"),
LiteralString.Factory("name") LiteralString.Factory("name")
).Value, ).Value,

View File

@@ -45,13 +45,23 @@ namespace TestNameSpace
public static ResourceManager ResourceManager => _ResourceManager.Value; public static ResourceManager ResourceManager => _ResourceManager.Value;
private static CultureInfo GetDefaultCulture()
{
return CultureInfo.CurrentCulture;
}
private static IResourceString AddToCultureCache(IResourceString source)
{
return new CultureBasedCachedString(source, GetDefaultCulture);
}
#endregion // ResourceManager #endregion // ResourceManager
#region Test1 #region Test1
private static readonly Lazy<IResourceString> LazyTest1 = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyTest1 = new Lazy<IResourceString>(
() => new ResourceManagerString(""Test1"", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString(""Test1"", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
@@ -62,17 +72,18 @@ namespace TestNameSpace
internal static class Test2 internal static class Test2
{ {
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString(""Test2"", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString(""Test2"", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
public static IResourceString Format => LazyFormat.Value; public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString prefix, IResourceString p1) => new FormattedResourceString( public static IResourceString From(IResourceString prefix, IResourceString p1) => AddToCultureCache(new FormattedResourceString(
Format, Format,
GetDefaultCulture,
prefix, prefix,
p1 p1
); ));
} }

View File

@@ -35,12 +35,12 @@ internal static class CodeSnippetFactory
return Enumerable.Empty<IResourceString>(); return Enumerable.Empty<IResourceString>();
} }
var stringResourses = resources.Where(r => r.Type.Match( var stringResources = resources.Where(r => r.Type.Match(
v => typeof(string).IsAssignableFrom(Type.GetType(v.Trim(), false, true)), v => typeof(string).IsAssignableFrom(Type.GetType(v.Trim(), false, true)),
() => true () => true
)); ));
return stringResourses.Select(r => return stringResources.Select(r =>
{ {
var openBraces = r.Value var openBraces = r.Value
.Replace("{{", string.Empty) .Replace("{{", string.Empty)
@@ -77,11 +77,11 @@ internal static class CodeSnippetFactory
var from = Properties.Resources.ResourceFormatClassFromMethod.From( var from = Properties.Resources.ResourceFormatClassFromMethod.From(
new JoinedResourceString( new JoinedResourceString(
LiteralString.Factory(", "), LiteralString.Factory(", "),
parameterNames.Select(n => LiteralString.Factory($"{nameof(IResourceString)} {n}")).ToArray() elements: parameterNames.Select(n => LiteralString.Factory($"{nameof(IResourceString)} {n}")).ToArray()
), ),
new JoinedResourceString( new JoinedResourceString(
LiteralString.Factory($",{System.Environment.NewLine} "), LiteralString.Factory($",{System.Environment.NewLine} "),
parameterNames.Select(n => LiteralString.Factory($"{n}")).ToArray() elements: parameterNames.Select(n => LiteralString.Factory($"{n}")).ToArray()
) )
); );
return Properties.Resources.ResourceFormatClassMembers.From( return Properties.Resources.ResourceFormatClassMembers.From(
@@ -106,7 +106,7 @@ internal static class CodeSnippetFactory
resourceManagerSnippet, resourceManagerSnippet,
new JoinedResourceString( new JoinedResourceString(
LiteralString.Empty, LiteralString.Empty,
memberSnippets?.ToArray() ?? Array.Empty<IResourceString>() elements: memberSnippets?.ToArray() ?? Array.Empty<IResourceString>()
) )
); );
} }

View File

@@ -20,101 +20,116 @@ namespace ResourceString.Net.Logic.Properties
public static ResourceManager ResourceManager => _ResourceManager.Value; public static ResourceManager ResourceManager => _ResourceManager.Value;
private static CultureInfo GetDefaultCulture()
{
return CultureInfo.CurrentCulture;
}
private static IResourceString AddToCultureCache(IResourceString source)
{
return new CultureBasedCachedString(source, GetDefaultCulture);
}
#endregion // ResourceManager #endregion // ResourceManager
internal static class ResourceStringMembers internal static class ResourceStringMembers
{ {
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourceStringMembers", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("ResourceStringMembers", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
public static IResourceString Format => LazyFormat.Value; public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString resourceId, IResourceString resourceManagerPropertyName) => new FormattedResourceString( public static IResourceString From(IResourceString resourceId, IResourceString resourceManagerPropertyName) => AddToCultureCache(new FormattedResourceString(
Format, Format,
GetDefaultCulture,
resourceId, resourceId,
resourceManagerPropertyName resourceManagerPropertyName
); ));
} }
internal static class ResourceFormatClassMembers internal static class ResourceFormatClassMembers
{ {
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourceFormatClassMembers", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("ResourceFormatClassMembers", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
public static IResourceString Format => LazyFormat.Value; public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString resourceId, IResourceString resourceManagerPropertyName, IResourceString fromMethodDefinition) => new FormattedResourceString( public static IResourceString From(IResourceString resourceId, IResourceString resourceManagerPropertyName, IResourceString fromMethodDefinition) => AddToCultureCache(new FormattedResourceString(
Format, Format,
GetDefaultCulture,
resourceId, resourceId,
resourceManagerPropertyName, resourceManagerPropertyName,
fromMethodDefinition fromMethodDefinition
); ));
} }
internal static class ResourceFormatClassFromMethod internal static class ResourceFormatClassFromMethod
{ {
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourceFormatClassFromMethod", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("ResourceFormatClassFromMethod", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
public static IResourceString Format => LazyFormat.Value; public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString fromMethodSignature, IResourceString parameterNames) => new FormattedResourceString( public static IResourceString From(IResourceString fromMethodSignature, IResourceString parameterNames) => AddToCultureCache(new FormattedResourceString(
Format, Format,
GetDefaultCulture,
fromMethodSignature, fromMethodSignature,
parameterNames parameterNames
); ));
} }
internal static class ResourcesClassTemplate internal static class ResourcesClassTemplate
{ {
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourcesClassTemplate", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("ResourcesClassTemplate", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
public static IResourceString Format => LazyFormat.Value; public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString ns, IResourceString className, IResourceString resourceManagerRegion, IResourceString resourceRegions) => new FormattedResourceString( public static IResourceString From(IResourceString ns, IResourceString className, IResourceString resourceManagerRegion, IResourceString resourceRegions) => AddToCultureCache(new FormattedResourceString(
Format, Format,
GetDefaultCulture,
ns, ns,
className, className,
resourceManagerRegion, resourceManagerRegion,
resourceRegions resourceRegions
); ));
} }
internal static class ResourceManagerMemberTemplate internal static class ResourceManagerMemberTemplate
{ {
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourceManagerMemberTemplate", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("ResourceManagerMemberTemplate", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
public static IResourceString Format => LazyFormat.Value; public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString className, IResourceString resourceManagerTypeName) => new FormattedResourceString( public static IResourceString From(IResourceString className, IResourceString resourceManagerTypeName) => AddToCultureCache(new FormattedResourceString(
Format, Format,
GetDefaultCulture,
className, className,
resourceManagerTypeName resourceManagerTypeName
); ));
} }
#region DefaultPropertyName_ResourceManager #region DefaultPropertyName_ResourceManager
private static readonly Lazy<IResourceString> LazyDefaultPropertyName_ResourceManager = new Lazy<IResourceString>( private static readonly Lazy<IResourceString> LazyDefaultPropertyName_ResourceManager = new Lazy<IResourceString>(
() => new ResourceManagerString("DefaultPropertyName_ResourceManager", ResourceManager, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("DefaultPropertyName_ResourceManager", ResourceManager, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );

View File

@@ -5,7 +5,7 @@
#region {0} #region {0}
private static readonly Lazy&lt;IResourceString> Lazy{0} = new Lazy&lt;IResourceString>( private static readonly Lazy&lt;IResourceString> Lazy{0} = new Lazy&lt;IResourceString>(
() => new ResourceManagerString("{0}", {1}, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("{0}", {1}, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
@@ -20,7 +20,7 @@
internal static class {0} internal static class {0}
{{ {{
private static readonly Lazy&lt;IResourceString> LazyFormat = new Lazy&lt;IResourceString>( private static readonly Lazy&lt;IResourceString> LazyFormat = new Lazy&lt;IResourceString>(
() => new ResourceManagerString("{0}", {1}, CultureInfo.CurrentCulture), () => AddToCultureCache(new ResourceManagerString("{0}", {1}, GetDefaultCulture)),
LazyThreadSafetyMode.PublicationOnly LazyThreadSafetyMode.PublicationOnly
); );
@@ -32,10 +32,11 @@
</data> </data>
<data name="ResourceFormatClassFromMethod" xml:space="preserve"> <data name="ResourceFormatClassFromMethod" xml:space="preserve">
<value> <value>
public static IResourceString From({0}) => new FormattedResourceString( public static IResourceString From({0}) => AddToCultureCache(new FormattedResourceString(
Format, Format,
GetDefaultCulture,
{1} {1}
); ));
</value> </value>
<comment>0 = FromMethodSignature, 1 = ParameterNames</comment> <comment>0 = FromMethodSignature, 1 = ParameterNames</comment>
</data> </data>
@@ -71,6 +72,16 @@ namespace {0}
public static ResourceManager ResourceManager => _ResourceManager.Value; public static ResourceManager ResourceManager => _ResourceManager.Value;
private static CultureInfo GetDefaultCulture()
{{
return CultureInfo.CurrentCulture;
}}
private static IResourceString AddToCultureCache(IResourceString source)
{{
return new CultureBasedCachedString(source, GetDefaultCulture);
}}
#endregion // ResourceManager #endregion // ResourceManager
</value> </value>
<comment>0 = ResourceManagerTypeName, 1 = ClassName</comment> <comment>0 = ResourceManagerTypeName, 1 = ClassName</comment>

View File

@@ -6,6 +6,11 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<Deterministic>true</Deterministic>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="4.4.3" /> <PackageReference Include="LanguageExt.Core" Version="4.4.3" />
<PackageReference Include="System.Resources.Extensions" Version="7.0.0" /> <PackageReference Include="System.Resources.Extensions" Version="7.0.0" />

View File

@@ -13,14 +13,26 @@
<NoPackageAnalysis>true</NoPackageAnalysis> <NoPackageAnalysis>true</NoPackageAnalysis>
<IncludeBuildOutput>false</IncludeBuildOutput> <IncludeBuildOutput>false</IncludeBuildOutput>
<PackageProjectUrl>https://gitlab.com/stubbfel/ResourceString.Net</PackageProjectUrl> <PackageProjectUrl>https://gitlab.com/stubbfel/ResourceString.Net</PackageProjectUrl>
<RepositoryUrl>https://gitlab.com/stubbfel/ResourceString.Net.git</RepositoryUrl>
<RepositoryBranch>main</RepositoryBranch>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>Readme.md</PackageReadmeFile> <PackageReadmeFile>Readme.md</PackageReadmeFile>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<Deterministic>true</Deterministic>
<Optimize>true</Optimize>
</PropertyGroup> </PropertyGroup>
<!-- It puts the dll in the expected folder of the NuGet package to be recognized as a C# analyzer --> <!-- It puts the dll in the expected folder of the NuGet package to be recognized as a C# analyzer -->
<ItemGroup> <ItemGroup>
<None Include="$(OutputPath)/*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> <None Include="$(OutputPath)/*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)/$(AssemblyName).Contract.dll" Pack="true" PackagePath="lib/netstandard2.0" Visible="false" /> <None Include="$(OutputPath)/$(AssemblyName).Contract.dll" Pack="true" PackagePath="lib/netstandard2.0" Visible="false" />
<None Include="$(OutputPath)/$(AssemblyName).Contract.pdb" Pack="true" PackagePath="lib/netstandard2.0" Visible="false" />
<None Include="../Readme.md" Pack="true" PackagePath="/"/> <None Include="../Readme.md" Pack="true" PackagePath="/"/>
</ItemGroup> </ItemGroup>

View File

@@ -1,6 +1,4 @@
{ fetchNuGet }: [ { fetchNuGet }: [
(fetchNuGet { pname = "Fody"; version = "6.7.0"; sha256 = "0fv0zrffa296qjyi11yk31vfqh6gm1nxsx8g5zz380jcsrilnp3h"; })
(fetchNuGet { pname = "ILMerge.Fody"; version = "1.24.0"; sha256 = "1gibwcl8ngbvwlcqzd9clysrhsjb8g4gwry7n8ifw1mrw7sjjk6x"; })
(fetchNuGet { pname = "LanguageExt.Core"; version = "4.4.3"; sha256 = "1pd7wx4c21v56y6i75sxbs990mjrs6bp9h8c48a5w79s1zpbinw5"; }) (fetchNuGet { pname = "LanguageExt.Core"; version = "4.4.3"; sha256 = "1pd7wx4c21v56y6i75sxbs990mjrs6bp9h8c48a5w79s1zpbinw5"; })
(fetchNuGet { pname = "Microsoft.Bcl.AsyncInterfaces"; version = "7.0.0"; sha256 = "1waiggh3g1cclc81gmjrqbh128kwfjky3z79ma4bd2ms9pa3gvfm"; }) (fetchNuGet { pname = "Microsoft.Bcl.AsyncInterfaces"; version = "7.0.0"; sha256 = "1waiggh3g1cclc81gmjrqbh128kwfjky3z79ma4bd2ms9pa3gvfm"; })
(fetchNuGet { pname = "Microsoft.CodeAnalysis.Analyzers"; version = "3.3.4"; sha256 = "0wd6v57p53ahz5z9zg4iyzmy3src7rlsncyqpcag02jjj1yx6g58"; }) (fetchNuGet { pname = "Microsoft.CodeAnalysis.Analyzers"; version = "3.3.4"; sha256 = "0wd6v57p53ahz5z9zg4iyzmy3src7rlsncyqpcag02jjj1yx6g58"; })

View File

@@ -3,4 +3,5 @@
alias nixe='nix --experimental-features "nix-command flakes"' alias nixe='nix --experimental-features "nix-command flakes"'
alias nulock='nixe run .#devTasks.updateNugetLock' alias nulock='nixe run .#devTasks.updateNugetLock'
alias fllock='nixe run .#devTasks.updateFlakeLock' alias fllock='nixe run .#devTasks.updateFlakeLock'
alias ulock='nulock && fllock' alias ulock='nulock && fllock'
alias restore-on-current-source='nixe build && dotnet nuget locals all -c && dotnet restore -s result/share -s https://api.nuget.org/v3/index.json $1'

6
flake.lock generated
View File

@@ -2,11 +2,11 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1684364070, "lastModified": 1684522302,
"narHash": "sha256-+bmqPSEQBePWwmfwxUX8kvJLyg8OM9mRKnDi5qB+m1s=", "narHash": "sha256-L7nUSrOYTWvXmIQ8NtVU2/AAah/ouJpf9DDVSt0s9+I=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "2e6eb88c9ab70147e6087d37c833833fd4a907e5", "rev": "c555a28f2436be370c40df70f4cd6c25fceff7af",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@@ -4,7 +4,7 @@
outputs = { self, nixpkgs }: outputs = { self, nixpkgs }:
let let
name = "ResourceString.Net"; name = "ResourceString.Net";
version = "0.0.3"; version = "0.0.4";
supportedSystems = supportedSystems =
[ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems; forAllSystems = nixpkgs.lib.genAttrs supportedSystems;