xtd 0.2.0
Loading...
Searching...
No Matches
translator.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "../io/path.hpp"
7#include "../environment.hpp"
8#include "../optional.hpp"
9#include "../static.hpp"
10#include "../string.hpp"
11
13namespace xtd {
15 namespace globalization {
28 class translator final static_ {
29 public:
31
41 static void language(const xtd::string& language);
46 static void language(xtd::null_ptr);
47
51
56
58
65 static void add_value(const xtd::string& language, const xtd::string& key, const xtd::string& value) {
66 auto language_name = to_language_name(language);
67 if (!xtd::io::directory::exists(xtd::io::path::combine(application_locale_path(), language_name))) language_name = to_fallback_name(language_name);
68 if (load_language(language_name)) languages_[language_name] = language_name;
69 language_values_[languages_[language_name]][key] = value;
70 }
71
76 static bool parse_locale(const xtd::string& locale_path);
77
81 static bool parse_locale(const xtd::string& locale_path, const xtd::string& language);
82
87 static bool parse_file(const xtd::string& file);
88
93 static bool parse_file(const xtd::string& file, const xtd::string& language);
94
98 static xtd::string translate(const xtd::string& value) noexcept {return translate(language(), value);}
103 static xtd::string translate(const xtd::string& language, const xtd::string& value) noexcept {return translate(language, value.chars().c_str());}
105
107 static const char* translate(const char* value) noexcept {return translate(language(), value);}
108 static const char* translate(const xtd::string& language, const char* value) noexcept {
109 auto language_name = to_language_name(language);
110 if (!xtd::io::directory::exists(xtd::io::path::combine(application_locale_path(), language_name))) language_name = to_fallback_name(language_name);
111 if (!load_language(language_name)) return value;
112 if (!language_values_[languages_[language_name]].contains_key(value)) return value;
113 return language_values_[languages_[language_name]][value].c_str();
114 }
116
117 private:
119 static string xtd_locale_path() {return environment::get_folder_path(xtd::environment::special_folder::xtd_locale);}
120 static bool load_language(const xtd::string& language) {return load_language(language, xtd_locale_path(), application_locale_path());}
121 static bool load_language(const xtd::string& language, const xtd::string& xtd_locale_path, const xtd::string& application_locale_path);
122 static string to_language_name(const xtd::string& language);
123 static string to_fallback_name(const xtd::string& language);
124 static xtd::collections::generic::dictionary<xtd::string, xtd::collections::specialized::string_dictionary> language_values_;
126 static xtd::optional<xtd::string> language_;
127 };
128 }
129}
130
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:63
@ xtd_locale
The xtd locale folder.
Definition environment.hpp:249
@ application_resources
The application resources folder.
Definition environment.hpp:245
static xtd::string get_folder_path(environment::special_folder folder)
Gets the path to the system special folder that is identified by the specified enumeration.
Definition environment.hpp:613
Represents translator class. This class cannot be inherited.
Definition translator.hpp:28
static bool parse_file(const xtd::string &file, const xtd::string &language)
Parses a specified file that contains translations for a specified language.
static xtd::array< xtd::string > languages()
Gets an array of languages supported by the application.
static xtd::string translate(const xtd::string &value) noexcept
Translates a string into the lganguage specified by the xtd::translate::language property.
Definition translator.hpp:98
static xtd::string system_language()
Gets the current system language.
static void language(xtd::null_ptr)
Sets the current application language.
static xtd::string language()
Gets the current application language.
static bool parse_locale(const xtd::string &locale_path, const xtd::string &language)
Parses a specified path that contains translations.
static bool parse_file(const xtd::string &file)
Parses a specified file that contains translations for a specified language.
static void language(const xtd::string &language)
Sets the current application language.
static void add_value(const xtd::string &language, const xtd::string &key, const xtd::string &value)
Manually adds a translation for a specified key and language.
Definition translator.hpp:65
static bool parse_locale(const xtd::string &locale_path)
Parses a specified path that contains translations.
static xtd::string translate(const xtd::string &language, const xtd::string &value) noexcept
Translates a string into the specified lganguage.
Definition translator.hpp:103
static bool exists(const xtd::string &path)
Determines whether the given path refers to an existing directory on disk.
static xtd::string combine(const xtd::string &path1, const xtd::string &path2)
Combines two path strings.
Contains xtd::environment class.
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:37
xtd::collections::generic::dictionary< xtd::string, xtd::string > string_dictionary
Represents a xtd::collections::generic::dictionary with the key and the value strongly typed to be st...
Definition string_dictionary.hpp:28
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::nullptr_t null_ptr
Represents the null_opt alias on std::nullptr_t.
Definition null_ptr.hpp:19
std::optional< type_t > optional
Represents the optional alias on std::optional.
Definition optional.hpp:25
Contains xtd::globalization::translator suffixes.
Contains classes that define culture-related information, including language, country/region,...
Definition culture_info.hpp:18
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::optional type.
Contains xtd::io::path class.
Contains xtd::static_object class.
Contains xtd::string alias.
Contains xtd::collections::specialized::string_dictionary typedef.