Toupper / tolower + locale (German)

How to convert a string (wstring) from lowercase and uppercase characters and vice versa? I searched the network and found that there is an STL function std :: transform.
But so far I have not figured out how to give the correct locale object, for example, the "Germany_german" function. Who can help? my code looks like this:

wstring strin = L"ABCÄÖÜabcäöü"; wstring str = strin; locale loc( "Germany_german" ); // ??? how to apply this ??? std::transform( str.begin(), str.end(), str.begin(), (int(*)(int)tolower ); //result: "abcäöüabcäöü" 

The symbols ÄÖÜ and äöü (this is like Ae, Oe, Ue) will not be correctly converted.

PS: I do not prefer a lot of sweat, and I know that BOOST is capable of anything, I would prefer an STL solution.

thanks in advance Sorry

+4
source share
4 answers

If you are running Unix, see /usr/share/locale/ for available locales. It looks like you want "de_DE.UTF-8" .

But setlocale(LC_ALL, ""); must establish that the program runs on the system locale, regardless of what it is.

Your program works for me (I installed close-paren) if I just set this standard by default without specifying Germany.

+1
source

You need to apply it in different ways:

 for(unsigned i=0;i<str.size();i++) str[i]=std::toupper(str[i],loc); 

Or set the global locale

 std::locale::global(std::locale("Germany_german")); 

and then

 std::transform( str.begin(), str.end(), str.begin(), std::toupper ); 

See: http://www.cplusplus.com/reference/std/locale/tolower/

Note 1 C toupper is different from std :: toupper, which receives std :: locale as a parameter, which uses char only as a parameter, and std::toupper works on both char and wchar_t .

Note 2 std::locale completely broken for the German language, since it works on the basis of characters and cannot convert "ß" to "SS", but it will work for most other characters ...

Note 3: If you need the correct code conversion, including handling characters like "ß", you need a good localization library such as ICU, or Boost.Locale

+9
source

The problem is that tolower() / toupper() fundamentally broken for many locales. How do you suggest toupper() to work with words containing 'ß' ? What will be in the upper case of "Maße" ? [1] There are similar examples in French and other languages.

So, the first thing you need to ask yourself is why you need it and what you want to achieve.

If you are ready to ignore all these problems, here is the first google search found me. (If you are going to google more about this, James Kanze has posted a lot of useful information about this issue in quite a few C ++ newsgroups over the years.)

[1] Hint for a few non-Germans who hang out here :) : in German, the letter "ß", literally the ligature "sz", is not available as a capital letter. It can be replaced with "ss", but not always. For example, "Masset" is a completely different word than "Maße".

+3
source

Have you tried "de_DE.UTF-8" to build the locale?

+1
source

Source: https://habr.com/ru/post/1306337/


All Articles