NDEVR
API Documentation
Translator.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Translator
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/String.h>
35#include <NDEVR/INIFactory.h>
36#include <NDEVR/File.h>
37#include <NDEVR/Dictionary.h>
38#include <NDEVR/INIInterface.h>
39#include <NDEVR/UUID.h>
40namespace NDEVR
41{
51
60 class NDEVR_BASE_API Translator : public INIInterface
61 {
62 public:
71 Translator(const StringView& translation_id);
76 Translator(const File& settings_file);
81 Translator(const Translator& translator);
91 virtual String convertNumber(const String& number_string, bool add_comma, uint04 min_decimals, uint04 max_decimals, uint04 min_digits) const;
97 virtual void append(String& append, const String& text_string) const;
104 virtual void append(String& append, const UUID& id, const String& default_text) const;
110 void copyFrom(const Translator& trans, bool use_existing_ini_file);
121 File translatorFile(bool user_file) const;
126 void getINI(INIFactory& factory) override;
142 const String& id() const { return m_translation_id; }
147 const String& name() const { return m_language_name; }
152 void setID(const StringView& id);
157 bool isLeftToRight() const;
162 void setName(const StringView& name);
168 Translator& operator=(const Translator& translation);
178 void saveTranslation(File location = File());
189 public:
200 static Translator* MainTranslator(const StringView& language);
205 static void SetMainTranslator(Translator* translator);
211 static String DefaultReadableLabel(const StringView& text_string);
217 static String DefaultReadableLabel(const UUID& text_string);
223 static UUID DefaultID(const StringView& text_string);
229 static UUID DefaultFastID(const StringView& text_string);
230 protected:
238 void init();
249 public:
252 };
253 //Translate string
254 #define _t(english_string) (TranslatedString(Translator::DefaultID(english_string), english_string))
255 //Translate number
256 #define _tn(number_string, add_comma, min_decimals, max_decimals, min_digits) (Translator::MainTranslator()->convertNumber(number_string, add_comma, min_decimals, max_decimals, min_digits))
257 //Translate string by description
258 #define _td(def, english_string) (TranslatedString(Translator::DefaultFastID(def), english_string))
259}
260
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
Contains methods for easily reading and writing to an INI file including efficient casting,...
Definition INIReader.h:107
INIInterface()
Constructs an INIInterface with no default INI file.
A core part of the engine, stores variables that can be listened to with ResourceListener which will ...
Definition Resource.h:42
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
virtual void append(String &append, const String &text_string) const
Appends a translated version of the given text string.
void init()
Initializes internal state for the Translator.
static Dictionary< String, Translator * > translators
Registry of all loaded translators keyed by language ID.
Definition Translator.h:251
Translator & operator=(const Translator &translation)
Assignment operator.
Translator()
Constructs a default Translator with no language loaded.
Dictionary< UUID, String > m_translated_strings
Cached dictionary of translated strings keyed by UUID.
Definition Translator.h:244
String m_common_characters
A string of commonly used characters in this language.
Definition Translator.h:242
void copyFrom(const Translator &trans, bool use_existing_ini_file)
Copies translation settings from another Translator.
char m_number_sep
The thousands separator character (typically ',').
Definition Translator.h:246
static Resource< Translator * > main_translator
The global main translator resource.
Definition Translator.h:250
virtual String convertNumber(const String &number_string, bool add_comma, uint04 min_decimals, uint04 max_decimals, uint04 min_digits) const
Converts a numeric string into a locale-appropriate formatted string.
virtual void append(String &append, const UUID &id, const String &default_text) const
Appends a translated string looked up by UUID, falling back to a default.
Translator(const Translator &translator)
Copy constructor.
const String & id() const
Returns the identifier of this translation.
Definition Translator.h:142
bool isLeftToRight() const
Returns whether this language reads left-to-right.
void saveTranslation(File location=File())
Saves the current translation data to a file.
File translatorFile(bool user_file) const
Returns the file path for the translator data file.
INIFactory & translationINI() const
Returns a reference to the INI factory managing translation entries.
void copyTranslationsFrom(const Translator &trans)
Copies only the translated string entries from another Translator.
void ensureLanguageLoaded() const
Lazily loads the language data from disk if not already loaded.
void getINI(INIFactory &factory) override
Populates or reads translation data from the given INI factory.
const Dictionary< UUID, String > & translatedStrings() const
Returns the dictionary of all translated strings keyed by UUID.
void setName(const StringView &name)
Sets the human-readable language name.
static String DefaultReadableLabel(const StringView &text_string)
Generates a default human-readable label from a text string.
String m_translation_id
The unique identifier for this translation.
Definition Translator.h:243
static void SetMainTranslator(Translator *translator)
Sets the application's main Translator instance.
static UUID DefaultID(const StringView &text_string)
Computes a default UUID for the given text string.
Translator(const StringView &translation_id)
Constructs a Translator for the specified translation identifier.
static UUID DefaultFastID(const StringView &text_string)
Computes a UUID for the given text string using a fast hashing method.
static Translator * MainTranslator(const StringView &language)
Returns the main Translator for a specific language.
static Translator * MainTranslator()
Returns the application's main Translator instance.
char m_decimal_sep
The decimal separator character (typically '.').
Definition Translator.h:247
const String & name() const
Returns the human-readable name of this language.
Definition Translator.h:147
static Dictionary< String, Translator > GetAvailableTranslations(File folder=File())
Discovers all available translation files in the given folder.
bool m_left_to_right
Whether the language reads left-to-right (typically true).
Definition Translator.h:248
String commonCharacters() const
Returns a string containing the most common characters for this language.
File m_translation_file
The file path of the translation data.
Definition Translator.h:240
void setID(const StringView &id)
Sets the translation identifier.
String m_language_name
The human-readable name of the language.
Definition Translator.h:241
static String DefaultReadableLabel(const UUID &text_string)
Generates a default human-readable label from a UUID.
Translator(const File &settings_file)
Constructs a Translator from a settings file on disk.
bool m_has_loaded_language
Whether the language data has been loaded from disk.
Definition Translator.h:245
File activeTranslatorFile() const
Returns the file path of the currently active translator file.
INIFactory m_language_ini
The INI factory managing the language translation file.
Definition Translator.h:239
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer s...
Definition UUID.h:61
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Contains logic for translating a string into a defined language.
Definition Translator.h:46
String readable_id
Human-readable identifier for the translation entry.
Definition Translator.h:47
String translation
The translated text in the target language.
Definition Translator.h:49
String english_string
The original English text to be translated.
Definition Translator.h:48