OpenTTD Source: src/language.h Source File

OpenTTD Source 15.3

language.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef LANGUAGE_H
11#define LANGUAGE_H
12
13#ifdef WITH_ICU_I18N
14#include <unicode/coll.h>
15#endif /* WITH_ICU_I18N */
16#include "strings_type.h"
17#include <filesystem>
18
19 static const uint8_t CASE_GENDER_LEN = 16;
20 static const uint8_t MAX_NUM_GENDERS = 8;
21 static const uint8_t MAX_NUM_CASES = 16;
22
24 struct LanguagePackHeader {
25 static const uint32_t IDENT = 0x474E414C;
26
27 uint32_t ident = 0;
28 uint32_t version = 0;
29 char name[32] = "";
30 char own_name[32] = "";
31 char isocode[16] = "";
32 uint16_t offsets[TEXT_TAB_END] = {};
33
35 char digit_group_separator[8] = ",";
37 char digit_group_separator_currency[8] = ",";
39 char digit_decimal_separator[8] = ".";
40 uint16_t missing = 0;
41 uint8_t plural_form = 0;
42 uint8_t text_dir = 0;
51 uint16_t winlangid = 0;
52 uint8_t newgrflangid = 0;
53 uint8_t num_genders = 0;
54 uint8_t num_cases = 0;
55 uint8_t pad[3] = {};
56
57 char genders[MAX_NUM_GENDERS][CASE_GENDER_LEN] = {};
58 char cases[MAX_NUM_CASES][CASE_GENDER_LEN] = {};
59
60 bool IsValid() const;
61 bool IsReasonablyFinished() const;
62
68 uint8_t GetGenderIndex(std::string_view gender_str) const
69 {
70 auto it = std::ranges::find(this->genders, gender_str);
71 return static_cast<uint8_t>(std::distance(std::begin(this->genders), it));
72 }
73
79 uint8_t GetCaseIndex(std::string_view case_str) const
80 {
81 auto it = std::ranges::find(this->cases, case_str);
82 return static_cast<uint8_t>(std::distance(std::begin(this->cases), it));
83 }
84};
86static_assert(sizeof(LanguagePackHeader) % 4 == 0);
87
89 struct LanguageMetadata : public LanguagePackHeader {
90 std::filesystem::path file;
91};
92
94 typedef std::vector<LanguageMetadata> LanguageList;
95
97extern LanguageList _languages;
98
100extern const LanguageMetadata *_current_language;
101
102#ifdef WITH_ICU_I18N
103extern std::unique_ptr<icu::Collator> _current_collator;
104#endif /* WITH_ICU_I18N */
105
106bool ReadLanguagePack(const LanguageMetadata *lang);
107const LanguageMetadata *GetLanguage(uint8_t newgrflangid);
108
109#endif /* LANGUAGE_H */
_languages
LanguageList _languages
The actual list of language meta data.
Definition strings.cpp:53
_current_language
const LanguageMetadata * _current_language
The currently loaded language.
Definition strings.cpp:54
MAX_NUM_GENDERS
static const uint8_t MAX_NUM_GENDERS
Maximum number of supported genders.
Definition language.h:20
_current_collator
std::unique_ptr< icu::Collator > _current_collator
Collator for the language currently in use.
Definition strings.cpp:59
LanguageList
std::vector< LanguageMetadata > LanguageList
Type for the list of language meta data.
Definition language.h:94
CASE_GENDER_LEN
static const uint8_t CASE_GENDER_LEN
The (maximum) length of a case/gender string.
Definition language.h:19
GetLanguage
const LanguageMetadata * GetLanguage(uint8_t newgrflangid)
Get the language with the given NewGRF language ID.
Definition strings.cpp:2184
ReadLanguagePack
bool ReadLanguagePack(const LanguageMetadata *lang)
Read a particular language.
Definition strings.cpp:2048
MAX_NUM_CASES
static const uint8_t MAX_NUM_CASES
Maximum number of supported cases.
Definition language.h:21
strings_type.h
Types related to strings.
TEXT_TAB_END
@ TEXT_TAB_END
End of language files.
Definition strings_type.h:41
LanguageMetadata
Make sure the size is right.
Definition language.h:89
LanguageMetadata::file
std::filesystem::path file
Name of the file we read this data from.
Definition language.h:90
LanguagePackHeader
Header of a language file.
Definition language.h:24
LanguagePackHeader::GetCaseIndex
uint8_t GetCaseIndex(std::string_view case_str) const
Get the index for the given case.
Definition language.h:79
LanguagePackHeader::plural_form
uint8_t plural_form
plural form index
Definition language.h:41
LanguagePackHeader::IsValid
bool IsValid() const
Check whether the header is a valid header for OpenTTD.
Definition strings.cpp:2017
LanguagePackHeader::IsReasonablyFinished
bool IsReasonablyFinished() const
Check whether a translation is sufficiently finished to offer it to the public.
Definition strings.cpp:2037
LanguagePackHeader::version
uint32_t version
32-bits of auto generated version info which is basically a hash of strings.h
Definition language.h:28
LanguagePackHeader::isocode
char isocode[16]
the ISO code for the language (not country code)
Definition language.h:31
LanguagePackHeader::own_name
char own_name[32]
the localized name of this language
Definition language.h:30
LanguagePackHeader::pad
uint8_t pad[3]
pad header to be a multiple of 4
Definition language.h:55
LanguagePackHeader::name
char name[32]
the international name of this language
Definition language.h:29
LanguagePackHeader::offsets
uint16_t offsets[TEXT_TAB_END]
the offsets
Definition language.h:32
LanguagePackHeader::winlangid
uint16_t winlangid
Windows language ID: Windows cannot and will not convert isocodes to something it can use to determin...
Definition language.h:51
LanguagePackHeader::num_genders
uint8_t num_genders
the number of genders of this language
Definition language.h:53
LanguagePackHeader::cases
char cases[MAX_NUM_CASES][CASE_GENDER_LEN]
the cases used by this translation
Definition language.h:58
LanguagePackHeader::missing
uint16_t missing
number of missing strings.
Definition language.h:40
LanguagePackHeader::digit_group_separator
char digit_group_separator[8]
Thousand separator used for anything not currencies.
Definition language.h:35
LanguagePackHeader::newgrflangid
uint8_t newgrflangid
newgrf language id
Definition language.h:52
LanguagePackHeader::text_dir
uint8_t text_dir
default direction of the text
Definition language.h:42
LanguagePackHeader::genders
char genders[MAX_NUM_GENDERS][CASE_GENDER_LEN]
the genders used by this translation
Definition language.h:57
LanguagePackHeader::num_cases
uint8_t num_cases
the number of cases of this language
Definition language.h:54
LanguagePackHeader::ident
uint32_t ident
32-bits identifier
Definition language.h:27
LanguagePackHeader::digit_decimal_separator
char digit_decimal_separator[8]
Decimal separator.
Definition language.h:39
LanguagePackHeader::digit_group_separator_currency
char digit_group_separator_currency[8]
Thousand separator used for currencies.
Definition language.h:37
LanguagePackHeader::IDENT
static const uint32_t IDENT
Identifier for OpenTTD language files, big endian for "LANG".
Definition language.h:25
LanguagePackHeader::GetGenderIndex
uint8_t GetGenderIndex(std::string_view gender_str) const
Get the index for the given gender.
Definition language.h:68