Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate extension-methods for classic ResXFileCodeGenerator types #23

Open
earloc opened this issue Nov 16, 2022 · 3 comments
Open

generate extension-methods for classic ResXFileCodeGenerator types #23

earloc opened this issue Nov 16, 2022 · 3 comments

Comments

@earloc
Copy link
Owner

earloc commented Nov 16, 2022

As a developer, I want the source-generator to generate typed extension-methods for already existing types that are generated by the classic ResXFileGenerator``, in order to leverage typed access to interpolated ressources.

@earloc earloc added the enhancement New feature or request label Nov 16, 2022
@earloc earloc added the work:in progress Work on this item has been started label Dec 19, 2022
@earloc earloc added this to the v0.7 milestone Dec 19, 2022
@earloc
Copy link
Owner Author

earloc commented Dec 27, 2022

Closing, as the generated types only contain static-properties, which cannot be extended in a way that actually make sense. Also, using these types should be avoided, as they tend to be problematic in multi-threaded(-multi-cultured) apps, due to their nature of heavily relying on those statics.

People should find a way to introduce the usage of IStringLocalizer in their apps.

Maybe a FAQ could help people in guiding theit way to adopt TypealizR?

@earloc earloc closed this as completed Dec 27, 2022
@earloc earloc added wontfix This will not be worked on and removed work:in progress Work on this item has been started labels Dec 27, 2022
@earloc
Copy link
Owner Author

earloc commented Jan 19, 2023

Recently revisited possibilities to support this based on this discussions over at #78.

SamplesWithCode.resx

<root>
  <data name="Greeting" xml:space="preserve">
    <value>Cheers!</value>
  </data>
  <data name="Hello {world:s}" xml:space="preserve">
    <value>Hello '{0}'</value>
  </data>
</root>

SamplesWithCode.Designer.cs

    internal class SamplesWithCode {
        //other stuff ommited for simplicity

        /// <summary>
        ///   Looks up a localized string similar to Cheers!.
        /// </summary>
        internal static string Greeting {
            get {
                return ResourceManager.GetString("Greeting", resourceCulture);
            }
        }
        
        /// <summary>
        ///   Looks up a localized string similar to Hello &apos;{0}&apos;.
        /// </summary>
        internal static string Hello__world_s_ {
            get {
                return ResourceManager.GetString("Hello {world:s}", resourceCulture);
            }
        }
    }

A feasable approach could be to generate the following code for a *ResXFileCodeGenerator-enabled file:

SamplesWithCodeExtensions.cs

internal static class SamplesWithCodeExtensions
{
    static LocalizedString GetLocalizable(string key, params object[] args)
    {
        var value = key;
        var raw = SamplesWithCode.ResourceManager.GetString(key, SamplesWithCode.Culture);
        if (raw is not null)
        {
            value = raw;
        }

        var formattedValue = string.Format(value, args);

        return new LocalizedString(key, formattedValue, raw is null);
    }

    public static LocalizedString Hello__world(this SamplesWithCode _, string world)
        => GetLocalizable("Hello {world:s}", world);

    public static LocalizedString Greeting(this SamplesWithCode _)
        => GetLocalizable("Greeting");
}

Which then could be used as:

var x = new SamplesWithCode(); //generated class by *ResXFileCodeGenerator

Console.WriteLine(x.Greeting());  // generated by TypealizR
Console.WriteLine(x.Hello__world("earth"));  // generated by TypealizR

The key here is to use the type generated by *ResXFileCodeGenerator just as a marker-type for the extension class.
Sadly, these types are not generated as partial, which would open a slight different approach. But extension-methods seem OK to achieve the desired result.

@earloc earloc reopened this Jan 19, 2023
@earloc
Copy link
Owner Author

earloc commented Jan 19, 2023

Maybe the generated methods need an additional IFormatProvider-parameter or any other mechnism to get rid of the dependency on the internal static global::System.Globalization.CultureInfo Culture-property from those *.Designer.cs files.

@earloc earloc added work:design storage:resx and removed wontfix This will not be worked on labels Jan 19, 2023
@earloc earloc modified the milestones: v0.7, v0.9 Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant