# locale

***

**1. Getting the Current Locale:**

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

int main() {
  std::cout << std::locale().name() << '\n';
}
```

**2. Changing the Locale:**

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

int main() {
  std::locale my_locale("en_US.UTF-8");
  std::cout.imbue(my_locale);
  std::cout << "Hello, world!" << '\n';
}
```

**3. Formatting Dates and Times:**

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

int main() {
  std::time_t t = std::time(nullptr);
  std::cout.imbue(std::locale("en_US.UTF-8"));
  std::cout << std::ctime(&t);
}
```

**4. Formatting Numbers:**

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

int main() {
  std::cout.imbue(std::locale("en_US.UTF-8"));
  std::cout << 1234.56 << '\n';  // Output: 1,234.56
}
```

**5. Formatting Currency:**

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

int main() {
  std::cout.imbue(std::locale("en_US.UTF-8"));
  std::cout << std::showbase << std::put_money(1234.56) << '\n';  // Output: $1,234.56
}
```

**6. Sorting Strings According to Locale:**

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

int main() {
  std::locale my_locale("en_US.UTF-8");
  std::vector<std::string> names = {"John", "Mary", "Alice", "Bob"};
  std::sort(names.begin(), names.end(), std::collator<std::string>(my_locale));
  for (const auto& name : names) {
    std::cout << name << '\n';
  }
}
```

**7. Comparing Strings Case-Insensitively:**

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

int main() {
  std::locale my_locale("en_US.UTF-8");
  std::string str1 = "Hello";
  std::string str2 = "hello";
  if (std::strcmp(str1.c_str(), str2.c_str(), my_locale) == 0) {
    std::cout << "Strings are equal." << '\n';
  } else {
    std::cout << "Strings are not equal." << '\n';
  }
}
```

**8. Translating Text:**

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

int main() {
  std::locale my_locale("fr_FR.UTF-8");
  std::istringstream stream("Hello, world!");
  stream.imbue(my_locale);
  std::string translated_text;
  stream >> translated_text;
  std::cout << translated_text << '\n';  // Output: Bonjour, monde !
}
```

**9. Writing Locale-Aware Applications:**

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

int main() {
  std::locale::global(std::locale("en_US.UTF-8"));
  // All input and output operations will now use the specified locale.
}
```

**10. Creating and Using Custom Locales:**

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

int main() {
  std::map<std::string, std::locale::facet*> facets;
  facets["collate"] = new std::collate<char>("en_US.UTF-8");
  facets["ctype"] = new std::ctype<char>("en_US.UTF-8");
  std::locale my_locale(std::locale(), facets);
  std::cout.imbue(my_locale);
  // Custom locale is now in use.
}
```

**11. Using the Decimal Separator:**

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

int main() {
  std::cout.imbue(std::locale("en_US.UTF-8"));
  std::cout << 1234.56 << '\n';  // Output: 1234.56
  std::cout.imbue(std::locale("fr_FR.UTF-8"));
  std::cout << 1234.56 << '\n';  // Output: 1234,56
}
```

**12. Using the Time Zone Name:**

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

int main() {
  std::cout.imbue(std::locale("en_US.UTF-8"));
  std::cout << std::ctime(nullptr);  // Output: Sun Jun 19 03:10:48 2022 Pacific Daylight Time
}
```

**13. Formatting Strings for Output:**

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

int main() {
  std::cout.imbue(std::locale("en_US.UTF-8"));
  std::cout << std::setw(10) << std::left << "Hello";  // Output: Hello     
}
```

**14. Checking if a Character is a Digit:**

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

int main() {
  std::cout.imbue(std::locale("en_US.UTF-8"));
  if (std::isdigit('5')) {
    std::cout << "5 is a digit." << '\n';
  }
}
```

**15. Converting Strings to Uppercase/Lowercase:**

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

int main() {
  std::cout.imbue(std::locale("en_US.UTF-8"));
  std::string str = "Hello, world!";
  std::cout << std::toupper(str) << '\n';  // Output: HELLO, WORLD!
  std::cout << std::tolower(str) << '\n';  // Output: hello, world!
}
```

**16. Collating Strings:**

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

int main() {
  std::locale my_locale("en_US.UTF-8");
  std::string str1 = "Hello";
  std::string str2 = "World";
  std::cout.imbue(my_locale);
  std::cout << (std::coll

```
