Announcement

Collapse
No announcement yet.

String.Lower problem with uppercase letter with accented characters

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • String.Lower problem with uppercase letter with accented characters

    Hi,

    I'm doing a function that turns each word into a sentence with the first letter in upper case and the others in lower case. The problem is that using string.lower command the accented letters do not become lowercase, for example:

    result = String.Lower("RENÊ");
    result = Dialog.Message("Notice", result, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);

    The dialog message shows "renÊ"

    Thanks if anyone can help me.
    Stay safe!

    Renê


  • #2
    Both the String.Lower() and the native Lua string.lower() function will not convert accented letters properly, you will need to write a custom function for this. Here is a sample how you could solve this:

    Code:
    tLookupTable = { ["Ç"]="ç", ["Ñ"]="ñ",
       ["Á"]="á", ["À"]="à", ["Â"]="â", ["Ã"]="ã", ["Ä"]="ä",
       ["É"]="é", ["È"]="è", ["Ê"]="ê", ["Ë"]="ë",
       ["Í"]="í", ["Ì"]="ì", ["Î"]="î", ["Ï"]="ï",
       ["Ó"]="ó", ["Ò"]="ó", ["Ô"]="ô", ["Õ"]="õ", ["Ö"]="ö",
       ["Ú"]="ú", ["Ù"]="ù", ["Û"]="û", ["Ü"]="ü" };
    
    function String.Lowercase(sString)
       local sLowercase = "";
       for i = 1, #sString do
          local sChar = String.Mid(sString, i, 1);
          if String.Asc(sChar) < 128 then
             sNewChar = String.Lower(sChar);
          else
             sNewChar = tLookupTable[sChar];
             if (sNewChar == nil) then
                sNewChar = "?";
             end
          end
          sLowercase = sLowercase .. sNewChar;
       end
       return (sLowercase);
    end
    
    local sSource = "ABCDEFGHIJLKMNOPQRSTUVWXYZÇÄËÏÖÜÁÀÂÃÉÈÊÍÌÏÎÑÓÒÕÔÚÙÛ";
    
    Dialog.Message("Case conversion", "Source string\r\n" .. sSource .. "\r\n\r\n"
       .. "string.lower() (native Lua)\r\n" .. string.lower(sSource) .. "\r\n\r\n"
       .. "String.Lower()\r\n" .. String.Lower(sSource) .. "\r\n\r\n"
       .. "String.Lowercase() (custom function)\r\n" .. String.Lowercase(sSource));
    The result of running the code above is this:

    Click image for larger version  Name:	SCRN-2020-06-28-01.png Views:	0 Size:	4.6 KB ID:	305291

    Ulrich

    Comment


    • #3
      Hi.

      Try to use os.setlocale
      Code:
      os.setlocale("");
      result = String.Lower("RENÊ");
      result = Dialog.Message("Notice", result, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);

      Comment


      • #4
        Thanks Ulrich!

        Comment


        • #5
          Icredible solution, thanks Lapex!

          Comment


          • #6
            Fascinating. How did you figure out that calling os.setlocale() with an empty string would work? I previously tried the approaches shown in "Programming in Lua", such as os.setlocale("ISO-8859-1"), os.setlocale("ISO-8859-1", "ctype"), os.setlocale("ISO-8859-1", "all") and os.setlocale("pt_BR"), which all should set the Latin-1 charset, but none of the attempts worked, yet an empty argument does precisely what is expected. Oh well.

            Ulrich

            Comment


            • #7
              Originally posted by Ulrich View Post
              How did you figure out that calling os.setlocale() with an empty string would work?
              https://www.lua.org/manual/5.1/manua...f-os.setlocale

              os.setlocale (locale [, category])


              Sets the current locale of the program. locale is a string specifying a locale; category is an optional string describing which category to change: "all", "collate", "ctype", "monetary", "numeric", or "time"; the default category is "all". The function returns the name of the new locale, or nil if the request cannot be honored.

              If locale is the empty string, the current locale is set to an implementation-defined native locale. If locale is the string "C", the current locale is set to the standard C locale.

              When called with nil as the first argument, this function only returns the name of the current locale for the given category.

              Comment

              Working...
              X