0
  1. I have local machine with locale: en-US
  2. I have build machine with locale: en-US-POSIX
  3. I have prod (AWS dotnet lambda) machine with locale: en_US.UTF-8

I have this piece of code to get proper casing for a database field:

public string FixFieldCaseName(string fieldName)
{
    var mappings = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
    mappings.Add("test", "Test");

    if (mappings.TryGetValue(fieldName, out var fixedFieldName) && fixedFieldName != fieldName)
    {
        _logger.Debug("stuff");
        return fixedFieldName;
    }
    return fieldName;
}

This works locally and on the lambda, but not on the build machines. It returns me the original string.

Local and Lambda: input ("TeSt") -> output ("Test")

Build machine: input ("TeSt") -> output ("TeSt")

It doesn't work on Ubuntu due to the locale (en-US-POSIX): https://github.com/dotnet/corefx/issues/17052

Field names are used to perform queries in the database.

What is the proper way to fix this?

  1. I'm not sure whether to use OrdinalIgnoreCase or InvariantCultureIgnoreCase, even AFTER I reread best practices on MSFT Page.

OR

  1. Should I set the culture everywhere in my code?

System.Globalization.CultureInfo.CurrentCulture = new System.Globalization.CultureInfo("en-US");

Thanks for any help.

jn1kk
  • 5,012
  • 2
  • 45
  • 72
  • The culture to use is determined based on the purpose for which the strings are to be used. Based on the method name `FixFieldCaseName()`, is there a chance this method is used during serialization and/or reflection, to access data by field name? Take a look at [Difference between CurrentCulture, InvariantCulture, CurrentUICulture and InstalledUICulture](https://stackoverflow.com/q/5060446) for guidance when to use each. – dbc Jan 30 '20 at 02:35
  • @dbc strings are use to perform queries in postgres DB. – jn1kk Jan 30 '20 at 02:38
  • 1
    If they are field names, then presumably you want to generate the same query on all machines, cultures and platforms. If so, use `OrdinalIgnoreCase`. – dbc Jan 30 '20 at 02:39
  • But if the incoming `fieldName` is something typed in by a user, you may want to use `CurrentCultureIgnoreCase`. E.g. in Turkish the uppercase of `i` is `İ` (capital dotted I), see https://en.wikipedia.org/wiki/Dotted_and_dotless_I. Do you want `i` to be equivalent to `I` or `İ` for Turkish users? – dbc Jan 30 '20 at 02:54
  • @dbc they are typed by users. we will not have field names in non english. but even if we did have turkish fields, CurrentCultureIgnoreCase doesnt work on build machines due to the wacky POSIX locale. – jn1kk Jan 30 '20 at 03:02
  • I don't think this is fixable: Here's a closed issue that discusses it - https://github.com/dotnet/runtime/issues/20109 – Rodney Richardson Sep 28 '20 at 09:33

0 Answers0