Skip to content

Commit

Permalink
Fix translator language and add locale methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Dec 1, 2023
1 parent 9c7daee commit 4763d7c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/xtd.core/include/xtd/translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,20 @@ namespace xtd {
/// @remarks Normally you should use the std::locale() method. This function only exists if you have a good reason to bypass std::locale().
/// @warning If you set the language with this method, and you use the std::locale method afterwards, translator will use the language initialized with this method and not with std::locale().
static void language(const xtd::ustring& language);


/// @brief Gets the global locale.
/// @return The global locale.
/// @remarks is same a call `std::locale {};`
static std::locale locale();
/// @brief Sets the global locale.
/// @param value The global locale.
/// @remarks is same a call `std::locale::global(std::locale {value});`
static void locale(const xtd::ustring& value);
/// @brief Sets the global locale.
/// @param value The global locale.
/// @remarks is same a call `std::locale::global(value);`
static void locale(const std::locale& value);

/// @brief Gets an array of languages supported by the application.
/// @return An array of languages supported by the application.
static std::vector<xtd::ustring> languages();
Expand Down
21 changes: 19 additions & 2 deletions src/xtd.core/src/xtd/translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void translator::language(const xtd::ustring& language) {
initialize(); // Must be first
} catch (...) {
}
language_ = language.to_lower();
language_ = language;
}

std::vector<xtd::ustring> translator::languages() {
Expand All @@ -47,6 +47,23 @@ std::vector<xtd::ustring> translator::languages() {
return languages;
}

std::locale translator::locale() {
return std::locale {};
}

void translator::locale(const xtd::ustring& value) {
auto parts = (value.find_last_of(".") == value.npos ? value : value.remove(value.find_last_of("."))).split({'_'});
auto extension = value.find_last_of(".") == value.npos ? ".utf-8" : value.substring(value.find_last_of("."));
if (parts.size() != 0 && parts.size() != 2) throw argument_exception(csf_);
auto language = parts.size() == 0 ? "en" : parts[0].to_lower();
auto country = parts.size() == 0 ? "US" : parts[1].to_upper();
locale(std::locale {language + "_" + country + extension});
}

void translator::locale(const std::locale& locale) {
std::locale::global(locale);
}

xtd::ustring translator::system_language() {
return locale_to_language(xtd::native::translator::get_system_locale());
}
Expand All @@ -58,7 +75,7 @@ void translator::add_value(const xtd::ustring& language, const xtd::ustring& key
void translator::parse_locale(const xtd::ustring& locale_path) {
if (!directory::exists(locale_path)) return;
for (auto locale_item : directory::get_directories(locale_path)) {
if (language_ != path::get_file_name(locale_item).to_lower()) continue;
if (language_ != path::get_file_name(locale_item)) continue;
for (auto language_item : directory::get_files(locale_item))
if (path::get_extension(language_item) == ".strings") parse_file(language_item, language_);
}
Expand Down

0 comments on commit 4763d7c

Please sign in to comment.