dseinternational / open-dotnet

Libraries and tools supporting DSE Open systems and technologies.
MIT License
0 stars 0 forks source link

Add source generator to generate ResourceProvider from *.restext file #188

Open frankbuckley opened 4 hours ago

frankbuckley commented 4 hours ago

Given a project file:

  <ItemGroup>
    <EmbeddedResource Include="Resources\*.restext" />
    <AdditionalFiles Include="Resources\strings.restext" />
  </ItemGroup>

and in Resources/strings.restext:

# Saying
Say = Say
Saying = Saying
SaysWord = Says {0}

and in Resources/Strings.cs:

namespace MyNamespace.Resources;

// Marker for naming resources - see ResourceProvider
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class Strings
{
}

and in Resources/ResourceProvider.cs:

namespace MyNamespace.Resources;

[ResourceProvider(typeof(Strings))]
public partial class ResourceProvider
{
}

generate:

public partial class ResourceProvider : PackagedLocalizedResourceProvider
{
    public static readonly ResourceProvider Default = new();

    private static readonly ResourceManager s_stringsResourceManager = new(
        typeof(Strings).FullName!,
        typeof(Strings).Assembly);

    /// <summary>
    /// Sets the culture for lookups. (For testing.)
    /// </summary>
    /// <param name="culture"></param>
    internal void SetLookupCulture(CultureInfo? culture)
    {
        LookupCulture = culture;
    }

    protected override ResourceManager GetResourceManager(string resourceName)
    {
        return s_stringsResourceManager;
    }

    public string Say(CultureInfo? culture = null)
    {
        return GetString("Say", culture);
    }

    public string Saying(CultureInfo? culture = null)
    {
        return GetString("Saying", culture);
    }

    public string SayWord(object? arg0, CultureInfo? culture = null)
    {
        return string.Format(GetString("Saying", culture), arg0);
    }
}

Notes:

frankbuckley commented 3 hours ago

Suggest DSE.Open.Localization assembly/project

Or perhaps DSE.Open.Localization.Resources

frankbuckley commented 3 hours ago

return string.Format(GetString("Saying", culture), arg0);

Should pass FormatCulture:

    public string SayWord(object? arg0, CultureInfo? culture = null)
    {
        return string.Format(FormatCulture, GetString("Saying", culture), arg0);
    }