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

Define the CultureInfo of the format #31

Open
ricardona opened this issue Oct 6, 2021 · 2 comments
Open

Define the CultureInfo of the format #31

ricardona opened this issue Oct 6, 2021 · 2 comments
Milestone

Comments

@ricardona
Copy link

We are using crozone/FormatWith to fill mail templates. These email templates must be sent in Spanish. However when trying to format the dates they appear in English.

Is there a mechanism to define the CultureInfo of the format?

            var template = "{d:dddd, MMMM dd, yyyy}";

            var stringDictionary = new Dictionary<string, object>
            {
                { "d", new DateTime(2025, 12, 31, 5, 10, 20) },
            };
            var result = template.FormatWith(template, stringDictionary);

            Assert.AreEqual("Wednesday, December 31, 2025", result);

Thanks in advance.

@crozone
Copy link
Owner

crozone commented Oct 7, 2021

Hi @ricardona, unfortunately there is currently no way to explicitly set the CultureInfo (or more generally, the IFormatProvider) while calling FormatWith. However, this is something I will look at adding in version 4.0.

In the meantime, I can see two possible workarounds:

  1. Format the date before passing it to format with

         // Get Spanish IFormatProvider
         IFormatProvider formatProvider = CultureInfo.CreateSpecificCulture("es");
    
         var template = "{d}";
    
         var stringDictionary = new Dictionary<string, object>
         {
             { "d", new DateTime(2025, 12, 31, 5, 10, 20).ToString("dddd, MMMM dd, yyyy", formatProvider) },
         };
         var result = template.FormatWith(stringDictionary);
    
  2. Set the current culture before calling FormatWith

         CultureInfo previousCulture = CultureInfo.CurrentCulture;
    
         // Set current culture to Spanish
         CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("es");
    
         var template = "{d:dddd, MMMM dd, yyyy}";
    
         var stringDictionary = new Dictionary<string, object>
         {
             { "d", new DateTime(2025, 12, 31, 5, 10, 20) },
         };
         var result = template.FormatWith(stringDictionary);
    
         // Reset current culture back
         CultureInfo.CurrentCulture = previousCulture;
    

I hope one of these is suitable enough for now.

@crozone crozone added this to the 4.0 milestone Oct 7, 2021
@crozone
Copy link
Owner

crozone commented Oct 8, 2021

@ricardona I've just remembered the third (and probably best) way to do this using FormatWith v3: Using FormattableWith()

// Get Spanish IFormatProvider
IFormatProvider formatProvider = CultureInfo.CreateSpecificCulture("es");

var template = "{d:dddd, MMMM dd, yyyy}";

var stringDictionary = new Dictionary<string, object>
{
    { "d", new DateTime(2025, 12, 31, 5, 10, 20) },
};
var result = template.FormattableWith(stringDictionary).ToString(formatProvider);

Output: "miércoles, diciembre 31, 2025"

This uses FormatWith to create an intermediate FormattableString, which can then be formatted using ToString(IFormatProvider). This is clean and I recommend this over the above two workarounds.

@crozone crozone added bug and removed bug labels Oct 8, 2021
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

2 participants