📘 Learning Objectives

After completing this chapter, you will: - Master locale concepts and usage - Understand internationalization (i18n) techniques - Learn about character encoding and Unicode - Master locale-specific formatting - Understand cultural conventions and localization

🎯 Key Concepts

1. Locales

  • Locale concept: Cultural and linguistic conventions
  • Locale categories: Different aspects of localization
  • Locale objects: std::locale usage
  • Locale operations: Locale manipulation
  • Default locales: System default locales

2. Internationalization

  • i18n principles: Internationalization design
  • Character encoding: UTF-8, UTF-16, UTF-32
  • Unicode support: Unicode character handling
  • String localization: Localized string handling
  • Cultural conventions: Cultural-specific formatting

3. Formatting and Conversion

  • Number formatting: Locale-specific number formatting
  • Date/time formatting: Locale-specific date/time
  • Currency formatting: Currency display
  • Text formatting: Locale-specific text formatting
  • Conversion operations: Type conversion with locales

4. Character Handling

  • Character classification: Character type checking
  • Character conversion: Case conversion
  • Character encoding: Encoding conversion
  • Wide characters: Wide character support
  • Unicode operations: Unicode character operations

5. Localization Best Practices

  • Locale selection: Choosing appropriate locales
  • Performance considerations: Locale performance
  • Error handling: Locale error handling
  • Testing: Locale testing strategies
  • Maintenance: Locale maintenance practices

🧩 Practice Exercises

Exercise 40.1: Locale Usage

Use locales for formatting and conversion.

Exercise 40.2: Internationalization

Implement i18n features in applications.

Exercise 40.3: Character Handling

Handle different character encodings.

Exercise 40.4: Cultural Formatting

Format data according to cultural conventions.

💻 Code Examples

Locale Usage

#include <iostream>
#include <locale>
#include <iomanip>

int main() {
    // Set locale for formatting
    std::locale::global(std::locale("en_US.UTF-8"));

    // Number formatting
    double number = 1234567.89;
    std::cout << "Number: " << std::put_money(number) << std::endl;

    // Date formatting
    std::time_t now = std::time(nullptr);
    std::cout << "Date: " << std::put_time(std::localtime(&now), "%c") << std::endl;

    return 0;
}

Character Handling

#include <iostream>
#include <locale>
#include <string>

int main() {
    std::locale loc("en_US.UTF-8");

    std::string text = "Hello, World!";

    // Character classification
    for (char c : text) {
        if (std::isalpha(c, loc)) {
            std::cout << c << " is alphabetic" << std::endl;
        }
    }

    // Case conversion
    std::string upper_text;
    for (char c : text) {
        upper_text += std::toupper(c, loc);
    }
    std::cout << "Uppercase: " << upper_text << std::endl;

    return 0;
}

🎓 Key Takeaways

  1. Use locales for cultural-specific formatting
  2. Implement i18n for international applications
  3. Handle character encoding properly
  4. Format data according to cultural conventions
  5. Test localization thoroughly

🔗 Next Steps

After mastering locales and internationalization, proceed to Chapter 41 to learn about numerics.

📚 Additional Resources

  • C++ Reference: Locales
  • C++ Core Guidelines: Internationalization
  • Practice with locale-specific formatting