Languages.h 5.4 KB

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