# clocale

***

**1. Setting Locale**

```cpp
#include <locale>

setlocale(LC_ALL, "en_US.UTF-8");
```

**2. Getting Current Locale**

```cpp
#include <locale>

char* locale = setlocale(LC_ALL, NULL);
```

**3. Using Numeric Formatting**

```cpp
#include <locale>
#include <iomanip>

double value = 123.456;
std::cout << std::fixed << std::setprecision(2) << value << std::endl; // 123.46
```

**4. Using Monetary Formatting**

```cpp
#include <locale>
#include <iomanip>

double amount = 1000.0;
std::cout << std::fixed << std::setprecision(2) << std::showpoint << std::setmoneyfmt(std::moneypunct<char>().curr_symbol()) << amount << std::endl; // $1,000.00
```

**5. Using Datetime Formatting**

```cpp
#include <locale>
#include <ctime>
#include <iostream>

time_t now = time(nullptr);
std::cout << std::put_time(std::localtime(&now), "%c") << std::endl; // Tue Sep 13 10:43:39 2022
```

**6. Using Collation**

```cpp
#include <locale>
#include <algorithm>

std::locale loc("en_US.UTF-8");
std::sort(vec.begin(), vec.end(), [](const std::string& a, const std::string& b) { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), loc); });
```

**7. Using Facets**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout.imbue(loc);
std::wcout.imbue(loc);
```

**8. Using Monetary Facets**

```cpp
#include <locale>
#include <iomanip>

std::locale loc("en_US.UTF-8");
std::cout.imbue(loc);
std::cout << std::fixed << std::setprecision(2) << std::showpoint << std::setmoneyfmt(std::moneypunct<char>().curr_symbol()) << 1000.0 << std::endl; // $1,000.00
```

**9. Using Datetime Facets**

```cpp
#include <locale>
#include <ctime>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout.imbue(loc);
time_t now = time(nullptr);
std::cout << std::put_time(std::localtime(&now), "%c") << std::endl; // Tue Sep 13 10:43:39 2022
```

**10. Using Character Classification Facets**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout.imbue(loc);
std::cout << std::boolalpha << std::isupper('A', loc) << std::endl; // true
```

**11. Using String Conversion Facets**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout.imbue(loc);
std::wstring wide_str = L"你好";
std::string narrow_str(wide_str.begin(), wide_str.end(), loc);
std::cout << narrow_str << std::endl; // 你好
```

**12. Using Collation Facets**

```cpp
#include <locale>
#include <algorithm>

std::locale loc("en_US.UTF-8");
std::collator<char> collator(loc);
std::cout << std::boolalpha << collator.compare("a", "b") < 0 << std::endl; // true
```

**13. Using Numpunct Facets**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout.imbue(loc);
std::cout << std::fixed << std::setprecision(2) << 1000.0 << std::endl; // 1,000.00
```

**14. Using Timepunct Facets**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout.imbue(loc);
time_t now = time(nullptr);
std::cout << std::put_time(std::localtime(&now), "%c") << std::endl; // Tue Sep 13 10:43:39 2022
```

**15. Using Message Facets**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout.imbue(loc);
std::cout << std::messages::get("hello") << std::endl; // hello
```

**16. Using Classification**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << (std::isupper('A', loc) ? L"Upper" : L"Lower") << std::endl; // Upper
```

**17. Using String Conversion**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wstring wide_str = L"你好";
std::string narrow_str(wide_str.begin(), wide_str.end(), loc);
std::wcout << narrow_str << std::endl; // 你好
```

**18. Using Comparison**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << (std::wlexicographical_compare(L"a", L"b", loc) ? L"Less" : L"Greater") << std::endl; // Less
```

**19. Using Number Formatting**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << std::fixed << std::setprecision(2) << 1000.0 << std::endl; // 1,000.00
```

**20. Using Date and Time Formatting**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
time_t now = time(nullptr);
std::wcout << std::put_time(std::localtime(&now), L"%c") << std::endl; // Tue Sep 13 10:43:39 2022
```

**21. Using Message Formatting**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << std::messages::get(L"hello") << std::endl; // hello
```

**22. Using Codecvt Facet**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wstring wide_str = L"你好";
std::string narrow_str;
std::codecvt<wchar_t, char> converter;
converter.out(std::wstring_convert<wchar_t, char>::convert_type::narrow, wide_str.begin(), wide_str.end(), std::back_inserter(narrow_str));
std::cout << narrow_str << std::endl; // 你好
```

**23. Using Character Classification**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << (std::isupper(L'A', loc) ? L"Upper" : L"Lower") << std::endl; // Upper
```

**24. Using Wide String Conversion**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wstring wide_str = L"你好";
std::string narrow_str(wide_str.begin(), wide_str.end(), loc);
std::wcout << narrow_str << std::endl; // 你好
```

**25. Using Wide String Comparison**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << (std::wlexicographical_compare(L"a", L"b", loc) ? L"Less" : L"Greater") << std::endl; // Less
```

**26. Using Wide Number Formatting**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << std::fixed << std::setprecision(2) << 1000.0 << std::endl; // 1,000.00
```

**27. Using Wide Date and Time Formatting**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
time_t now = time(nullptr);
std::wcout << std::put_time(std::localtime(&now), L"%c") << std::endl; // Tue Sep 13 10:43:39 2022
```

**28. Using Wide Message Formatting**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << std::messages::get(L"hello") << std::endl; // hello
```

**29. Using Generic Character Classification**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout.imbue(loc);
std::cout << (std::isupper('A', std::locale::classic()) ? "Upper" : "Lower") << std::endl; // Upper
```

**30. Using Generic String Conversion**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wstring wide_str = L"你好";
std::string narrow_str(wide_str.begin(), wide_str.end(), std::locale::classic());
std::wcout << narrow_str << std::endl; // 你好
```

**31. Using Generic Comparison**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << (std::wlexicographical_compare(L"a", L"b", std::locale::classic()) ? L"Less" : L"Greater") << std::endl; // Less
```

**32. Using Generic Number Formatting**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << std::fixed << std::setprecision(2) << 1000.0 << std::locale::classic() << std::endl; // 1,000.00
```

**33. Using Generic Date and Time Formatting**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
time_t now = time(nullptr);
std::wcout << std::put_time(std::localtime(&now), L"%c", std::locale::classic()) << std::endl; // Tue Sep 13 10:43:39 2022
```

**34. Using Generic Message Formatting**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::wcout << std::messages::get(L"hello", std::locale::classic()) << std::endl; // hello
```

**35. Using Global Locale**

```cpp
#include <locale>
#include <iostream>

std::locale::global(std::locale("en_US.UTF-8"));
std::cout << std::fixed << std::setprecision(2) << 1000.0 << std::endl; // 1,000.00
```

**36. Using Default Locale**

```cpp
#include <locale>
#include <iostream>

std::cout.imbue(std::locale());
std::cout << std::fixed << std::setprecision(2) << 1000.0 << std::endl; // 1,000.00
```

**37. Using Copy Constructor**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2(loc1);
```

**38. Using Move Constructor**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2(std::move(loc1));
```

**39. Using Assignment Operator**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2;
loc2 = loc1;
```

**40. Using Equality Operator**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2("en_US.UTF-8");
std::cout << std::boolalpha << (loc1 == loc2) << std::endl; // true
```

**41. Using Inequality Operator**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2("fr_FR.UTF-8");
std::cout << std::boolalpha << (loc1 != loc2) << std::endl; // true
```

**42. Using Less Than Operator**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2("fr_FR.UTF-8");
std::cout << std::boolalpha << (loc1 < loc2) << std::endl; // true
```

**43. Using Greater Than Operator**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2("fr_FR.UTF-8");
std::cout << std::boolalpha << (loc1 > loc2) << std::endl; // false
```

**44. Using Less Than or Equal To Operator**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2("en_US.UTF-8");
std::cout << std::boolalpha << (loc1 <= loc2) << std::endl; // true
```

**45. Using Greater Than or Equal To Operator**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2("en_US.UTF-8");
std::cout << std::boolalpha << (loc1 >= loc2) << std::endl; // true
```

**46. Using Subscript Operator**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout << loc[std::locale::numeric_facet]() << std::endl; // decimal point separator
```

**47. Using at Function**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout << loc.at(std::locale::numeric_facet)() << std::endl; // decimal point separator
```

**48. Using has\_facet Function**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout << std::boolalpha << loc.has_facet<std::moneypunct<char>>() << std::endl; // true
```

**49. Using facet Method**

```cpp
#include <locale>
#include <iostream>

std::locale loc("en_US.UTF-8");
std::cout << loc.facet<std::moneypunct<char>>().curr_symbol() << std::endl; // $
```

**50. Using swap Method**

```cpp
#include <locale>
#include <iostream>

std::locale loc1("en_US.UTF-8");
std::locale loc2("fr_FR.UTF-8");
loc1.swap(loc2);
```
