Languages.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. };
  23. enum class ELanguages
  24. {
  25. CZECH,
  26. CHINESE,
  27. ENGLISH,
  28. FINNISH,
  29. FRENCH,
  30. GERMAN,
  31. GREEK,
  32. HUNGARIAN,
  33. ITALIAN,
  34. JAPANESE,
  35. KOREAN,
  36. NORWEGIAN,
  37. POLISH,
  38. PORTUGUESE,
  39. RUSSIAN,
  40. SPANISH,
  41. SWEDISH,
  42. TURKISH,
  43. UKRAINIAN,
  44. VIETNAMESE,
  45. COUNT
  46. };
  47. struct Options
  48. {
  49. /// string identifier (ascii, lower-case), e.g. "english"
  50. std::string identifier;
  51. /// human-readable name of language in English
  52. std::string nameEnglish;
  53. /// human-readable name of language in its own language
  54. std::string nameNative;
  55. /// encoding that is used by H3 for this language
  56. std::string encoding;
  57. /// primary IETF language tag
  58. std::string tagIETF;
  59. /// ISO 639-2 (B) language code
  60. std::string tagISO2;
  61. /// DateTime format
  62. std::string dateTimeFormat;
  63. /// Ruleset for plural forms in this language
  64. EPluralForms pluralForms = EPluralForms::NONE;
  65. /// Selectable in launcher
  66. bool selectable;
  67. };
  68. inline const auto & getLanguageList()
  69. {
  70. static const std::array<Options, 20> languages
  71. { {
  72. { "czech", "Czech", "Čeština", "CP1250", "cs", "cze", "%d.%m.%Y %H:%M", EPluralForms::CZ_3, true },
  73. { "chinese", "Chinese", "简体中文", "GBK", "zh", "chi", "%Y-%m-%d %H:%M", EPluralForms::VI_1, true }, // Note: actually Simplified Chinese
  74. { "english", "English", "English", "CP1252", "en", "eng", "%Y-%m-%d %H:%M", EPluralForms::EN_2, true }, // English uses international date/time format here
  75. { "finnish", "Finnish", "Suomi", "CP1252", "fi", "fin", "%d.%m.%Y %H:%M", EPluralForms::EN_2, true },
  76. { "french", "French", "Français", "CP1252", "fr", "fre", "%d/%m/%Y %H:%M", EPluralForms::FR_2, true },
  77. { "german", "German", "Deutsch", "CP1252", "de", "ger", "%d.%m.%Y %H:%M", EPluralForms::EN_2, true },
  78. { "greek", "Greek", "ελληνικά", "CP1253", "el", "ell", "%d/%m/%Y %H:%M", EPluralForms::EN_2, false },
  79. { "hungarian", "Hungarian", "Magyar", "CP1250", "hu", "hun", "%Y. %m. %d. %H:%M", EPluralForms::EN_2, true },
  80. { "italian", "Italian", "Italiano", "CP1250", "it", "ita", "%d/%m/%Y %H:%M", EPluralForms::EN_2, true },
  81. { "japanese", "Japanese", "日本語", "JIS", "ja", "jpn", "%Y年%m月%d日 %H:%M", EPluralForms::NONE, false },
  82. { "korean", "Korean", "한국어", "CP949", "ko", "kor", "%Y-%m-%d %H:%M", EPluralForms::VI_1, true },
  83. { "polish", "Polish", "Polski", "CP1250", "pl", "pol", "%d.%m.%Y %H:%M", EPluralForms::PL_3, true },
  84. { "portuguese", "Portuguese", "Português", "CP1252", "pt", "por", "%d/%m/%Y %H:%M", EPluralForms::EN_2, true }, // Note: actually Brazilian Portuguese
  85. { "russian", "Russian", "Русский", "CP1251", "ru", "rus", "%d.%m.%Y %H:%M", EPluralForms::UK_3, true },
  86. { "spanish", "Spanish", "Español", "CP1252", "es", "spa", "%d/%m/%Y %H:%M", EPluralForms::EN_2, true },
  87. { "swedish", "Swedish", "Svenska", "CP1252", "sv", "swe", "%Y-%m-%d %H:%M", EPluralForms::EN_2, true },
  88. { "norwegian", "Norwegian", "Norsk", "CP1252", "no", "nor", "%d/%m/%Y %H:%M", EPluralForms::EN_2, false },
  89. { "turkish", "Turkish", "Türkçe", "CP1254", "tr", "tur", "%d.%m.%Y %H:%M", EPluralForms::EN_2, true },
  90. { "ukrainian", "Ukrainian", "Українська", "CP1251", "uk", "ukr", "%d.%m.%Y %H:%M", EPluralForms::UK_3, true },
  91. { "vietnamese", "Vietnamese", "Tiếng Việt", "UTF-8", "vi", "vie", "%d/%m/%Y %H:%M", EPluralForms::VI_1, true }, // Fan translation uses special encoding
  92. } };
  93. static_assert(languages.size() == static_cast<size_t>(ELanguages::COUNT), "Languages array is missing a value!");
  94. return languages;
  95. }
  96. inline const Options & getLanguageOptions(ELanguages language)
  97. {
  98. return getLanguageList().at(static_cast<size_t>(language));
  99. }
  100. inline const Options & getLanguageOptions(const std::string & language)
  101. {
  102. for(const auto & entry : getLanguageList())
  103. if(entry.identifier == language)
  104. return entry;
  105. throw std::out_of_range("Language " + language + " does not exists!");
  106. }
  107. template<typename Numeric>
  108. inline constexpr int getPluralFormIndex(EPluralForms form, Numeric value)
  109. {
  110. // Based on https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
  111. switch(form)
  112. {
  113. case EPluralForms::NONE:
  114. case EPluralForms::VI_1:
  115. return 0;
  116. case EPluralForms::EN_2:
  117. if (value == 1)
  118. return 1;
  119. return 2;
  120. case EPluralForms::FR_2:
  121. if (value == 1 || value == 0)
  122. return 1;
  123. return 2;
  124. case EPluralForms::UK_3:
  125. if (value % 10 == 1 && value % 100 != 11)
  126. return 1;
  127. if (value%10>=2 && value%10<=4 && (value%100<10 || value%100>=20))
  128. return 2;
  129. return 0;
  130. case EPluralForms::CZ_3:
  131. if (value == 1)
  132. return 1;
  133. if (value>=2 && value<=4)
  134. return 2;
  135. return 0;
  136. case EPluralForms::PL_3:
  137. if (value == 1)
  138. return 1;
  139. if (value%10>=2 && value%10<=4 && (value%100<10 || value%100>=20))
  140. return 2;
  141. return 0;
  142. }
  143. throw std::runtime_error("Invalid plural form enumeration received!");
  144. }
  145. template<typename Numeric>
  146. inline std::string getPluralFormTextID(std::string languageName, Numeric value, std::string textID)
  147. {
  148. int formIndex = getPluralFormIndex(getLanguageOptions(languageName).pluralForms, value);
  149. return textID + '.' + std::to_string(formIndex);
  150. }
  151. }