Languages.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Languages.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. namespace Languages
  12. {
  13. enum class EPluralForms
  14. {
  15. NONE,
  16. VI_1, // Single plural form, (Vietnamese)
  17. EN_2, // Two forms, singular used for one only (English)
  18. FR_2, // Two forms, singular used for zero and one (French)
  19. UK_3, // Three forms, special cases for numbers ending in 1 and 2, 3, 4, except those ending in 1[1-4] (Ukrainian)
  20. CZ_3, // Three forms, special cases for 1 and 2, 3, 4 (Czech)
  21. PL_3, // Three forms, special case for one and some numbers ending in 2, 3, or 4 (Polish)
  22. RO_3, // Three forms, special case for numbers ending in 00 or [2-9][0-9] (Romanian)
  23. };
  24. enum class ELanguages
  25. {
  26. BELARUSIAN,
  27. BULGARIAN,
  28. CZECH,
  29. CHINESE,
  30. DUTCH,
  31. ENGLISH,
  32. FILIPINO,
  33. FINNISH,
  34. FRENCH,
  35. GERMAN,
  36. GREEK,
  37. HUNGARIAN,
  38. ITALIAN,
  39. JAPANESE,
  40. KOREAN,
  41. LATVIAN,
  42. NORWEGIAN,
  43. POLISH,
  44. PORTUGUESE,
  45. ROMANIAN,
  46. RUSSIAN,
  47. SERBIAN,
  48. SPANISH,
  49. SWEDISH,
  50. TURKISH,
  51. UKRAINIAN,
  52. VIETNAMESE,
  53. COUNT
  54. };
  55. struct Options
  56. {
  57. /// string identifier (ascii, lower-case), e.g. "english"
  58. std::string identifier;
  59. /// human-readable name of language in English
  60. std::string nameEnglish;
  61. /// human-readable name of language in its own language
  62. std::string nameNative;
  63. /// encoding that is used by H3 for this language
  64. std::string encoding;
  65. /// proper locale name, e.g. "en_US"
  66. std::string localeName;
  67. /// primary IETF language tag
  68. std::string tagIETF;
  69. /// ISO 639-2 (B) language code
  70. std::string tagISO2;
  71. /// DateTime format
  72. std::string dateTimeFormat;
  73. /// Ruleset for plural forms in this language
  74. EPluralForms pluralForms = EPluralForms::NONE;
  75. /// Selectable in launcher
  76. bool selectable;
  77. };
  78. inline const auto & getLanguageList()
  79. {
  80. static const std::array<Options, 27> languages
  81. { {
  82. { "belarusian", "Belarusian", "Беларускі", "CP1251", "be_BY", "be", "bel", "%d.%m.%Y %H:%M", EPluralForms::UK_3, true },
  83. { "bulgarian", "Bulgarian", "Български", "CP1251", "bg_BG", "bg", "bul", "%d.%m.%Y %H:%M", EPluralForms::EN_2, true },
  84. { "czech", "Czech", "Čeština", "CP1250", "cs_CZ", "cs", "cze", "%d.%m.%Y %H:%M", EPluralForms::CZ_3, true },
  85. { "chinese", "Chinese", "简体中文", "GBK", "zh_CN", "zh", "chi", "%Y-%m-%d %H:%M", EPluralForms::VI_1, true }, // Note: actually Simplified Chinese
  86. { "dutch", "Dutch", "Nederlands", "CP1252", "nl_NL", "nl", "nld", "%Y-%m-%d %H:%M", EPluralForms::EN_2, true },
  87. { "english", "English", "English", "CP1252", "en_US", "en", "eng", "%Y-%m-%d %H:%M", EPluralForms::EN_2, true }, // English uses international date/time format here
  88. { "filipino", "Filipino", "Pilipino", "CP1252", "fil_FIL","fil","fil","%Y-%m-%d %H:%M", EPluralForms::EN_2, true },
  89. { "finnish", "Finnish", "Suomi", "CP1252", "fi_FI", "fi", "fin", "%d.%m.%Y %H:%M", EPluralForms::EN_2, true },
  90. { "french", "French", "Français", "CP1252", "fr_FR", "fr", "fre", "%d/%m/%Y %H:%M", EPluralForms::FR_2, true },
  91. { "german", "German", "Deutsch", "CP1252", "de_DE", "de", "ger", "%d.%m.%Y %H:%M", EPluralForms::EN_2, true },
  92. { "greek", "Greek", "ελληνικά", "CP1253", "el_GR", "el", "ell", "%d/%m/%Y %H:%M", EPluralForms::EN_2, true },
  93. { "hungarian", "Hungarian", "Magyar", "CP1250", "hu_HU", "hu", "hun", "%Y. %m. %d. %H:%M", EPluralForms::EN_2, true },
  94. { "italian", "Italian", "Italiano", "CP1250", "it_IT", "it", "ita", "%d/%m/%Y %H:%M", EPluralForms::EN_2, true },
  95. { "japanese", "Japanese", "日本語", "JIS", "ja_JP", "ja", "jpn", "%Y年%m月%d日 %H:%M", EPluralForms::VI_1, true },
  96. { "korean", "Korean", "한국어", "CP949", "ko_KR", "ko", "kor", "%Y-%m-%d %H:%M", EPluralForms::VI_1, true },
  97. { "latvian", "Latvian", "Latviešu", "CP1257", "lv_LV", "lv", "lva", "%d.%m.%Y %H:%M", EPluralForms::PL_3, true },
  98. { "norwegian", "Norwegian", "Norsk Bokmål", "UTF-8", "nb_NO", "nb", "nor", "%d/%m/%Y %H:%M", EPluralForms::EN_2, false },
  99. { "polish", "Polish", "Polski", "CP1250", "pl_PL", "pl", "pol", "%d.%m.%Y %H:%M", EPluralForms::PL_3, true },
  100. { "portuguese", "Portuguese", "Português", "CP1252", "pt_BR", "pt", "por", "%d/%m/%Y %H:%M", EPluralForms::EN_2, true }, // Note: actually Brazilian Portuguese
  101. { "romanian", "Romanian", "Română", "CP1252", "ro_RO", "ro", "rum", "%Y-%m-%d %H:%M", EPluralForms::RO_3, true }, // Note: codepage is ISO-8859-16, but doesn't work with MSVC -> using CP1252 because there is also no known official/fan translation for OH3
  102. { "russian", "Russian", "Русский", "CP1251", "ru_RU", "ru", "rus", "%d.%m.%Y %H:%M", EPluralForms::UK_3, true },
  103. { "serbian", "Serbian", "Српски", "CP1252", "sr_SR", "sr", "srp", "%Y-%m-%d %H:%M", EPluralForms::UK_3, true },
  104. { "spanish", "Spanish", "Español", "CP1252", "es_ES", "es", "spa", "%d/%m/%Y %H:%M", EPluralForms::EN_2, true },
  105. { "swedish", "Swedish", "Svenska", "CP1252", "sv_SE", "sv", "swe", "%Y-%m-%d %H:%M", EPluralForms::EN_2, true },
  106. { "turkish", "Turkish", "Türkçe", "CP1254", "tr_TR", "tr", "tur", "%d.%m.%Y %H:%M", EPluralForms::EN_2, true },
  107. { "ukrainian", "Ukrainian", "Українська", "CP1251", "uk_UA", "uk", "ukr", "%d.%m.%Y %H:%M", EPluralForms::UK_3, true },
  108. { "vietnamese", "Vietnamese", "Tiếng Việt", "UTF-8", "vi_VN", "vi", "vie", "%d/%m/%Y %H:%M", EPluralForms::VI_1, true }, // Fan translation uses special encoding
  109. } };
  110. static_assert(languages.size() == static_cast<size_t>(ELanguages::COUNT), "Languages array is missing a value!");
  111. return languages;
  112. }
  113. inline const Options & getLanguageOptions(ELanguages language)
  114. {
  115. return getLanguageList().at(static_cast<size_t>(language));
  116. }
  117. inline const Options & getLanguageOptions(const std::string & language)
  118. {
  119. for(const auto & entry : getLanguageList())
  120. if(entry.identifier == language)
  121. return entry;
  122. throw std::out_of_range("Language " + language + " does not exists!");
  123. }
  124. template<typename Numeric>
  125. inline constexpr int getPluralFormIndex(EPluralForms form, Numeric value)
  126. {
  127. // Based on https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
  128. switch(form)
  129. {
  130. case EPluralForms::NONE:
  131. case EPluralForms::VI_1:
  132. return 0;
  133. case EPluralForms::EN_2:
  134. if (value == 1)
  135. return 1;
  136. return 2;
  137. case EPluralForms::FR_2:
  138. if (value == 1 || value == 0)
  139. return 1;
  140. return 2;
  141. case EPluralForms::UK_3:
  142. if (value % 10 == 1 && value % 100 != 11)
  143. return 1;
  144. if (value%10>=2 && value%10<=4 && (value%100<10 || value%100>=20))
  145. return 2;
  146. return 0;
  147. case EPluralForms::CZ_3:
  148. if (value == 1)
  149. return 1;
  150. if (value>=2 && value<=4)
  151. return 2;
  152. return 0;
  153. case EPluralForms::PL_3:
  154. if (value == 1)
  155. return 1;
  156. if (value%10>=2 && value%10<=4 && (value%100<10 || value%100>=20))
  157. return 2;
  158. return 0;
  159. case EPluralForms::RO_3:
  160. if (value == 1)
  161. return 1;
  162. if (value==0 || (value%100 > 0 && value%100 < 20))
  163. return 2;
  164. return 0;
  165. }
  166. throw std::runtime_error("Invalid plural form enumeration received!");
  167. }
  168. template<typename Numeric>
  169. inline std::string getPluralFormTextID(std::string languageName, Numeric value, std::string textID)
  170. {
  171. int formIndex = getPluralFormIndex(getLanguageOptions(languageName).pluralForms, value);
  172. return textID + '.' + std::to_string(formIndex);
  173. }
  174. }