lang_instance.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "lang_auto.h"
  9. #include "base/const_string.h"
  10. #include "base/weak_ptr.h"
  11. namespace Lang {
  12. inline constexpr auto kChoosingStickerReplacement = "oo"_cs;
  13. struct Language {
  14. QString id;
  15. QString pluralId;
  16. QString baseId;
  17. QString name;
  18. QString nativeName;
  19. };
  20. inline bool operator==(const Language &a, const Language &b) {
  21. return (a.id == b.id) && (a.name == b.name);
  22. }
  23. inline bool operator!=(const Language &a, const Language &b) {
  24. return !(a == b);
  25. }
  26. QString CloudLangPackName();
  27. QString CustomLanguageId();
  28. Language DefaultLanguage();
  29. class Instance;
  30. Instance &GetInstance();
  31. enum class Pack {
  32. None,
  33. Current,
  34. Base,
  35. };
  36. class Instance {
  37. struct PrivateTag;
  38. public:
  39. Instance();
  40. Instance(not_null<Instance*> derived, const PrivateTag &);
  41. void switchToId(const Language &language);
  42. void switchToCustomFile(const QString &filePath);
  43. Instance(const Instance &other) = delete;
  44. Instance &operator=(const Instance &other) = delete;
  45. Instance(Instance &&other) = default;
  46. Instance &operator=(Instance &&other) = default;
  47. QString systemLangCode() const;
  48. QString langPackName() const;
  49. QString cloudLangCode(Pack pack) const;
  50. QString id() const;
  51. rpl::producer<QString> idChanges() const;
  52. QString baseId() const;
  53. QString name() const;
  54. QString nativeName() const;
  55. QString id(Pack pack) const;
  56. bool isCustom() const;
  57. int version(Pack pack) const;
  58. QByteArray serialize() const;
  59. void fillFromSerialized(const QByteArray &data, int dataAppVersion);
  60. bool supportChoosingStickerReplacement() const;
  61. int rightIndexChoosingStickerReplacement(bool named) const;
  62. void applyDifference(
  63. Pack pack,
  64. const MTPDlangPackDifference &difference);
  65. static std::map<ushort, QString> ParseStrings(
  66. const MTPVector<MTPLangPackString> &strings);
  67. [[nodiscard]] rpl::producer<> updated() const {
  68. return _updated.events();
  69. }
  70. QString getValue(ushort key) const {
  71. Expects(key < _values.size());
  72. return _values[key];
  73. }
  74. QString getNonDefaultValue(const QByteArray &key) const;
  75. bool isNonDefaultPlural(ushort key) const {
  76. Expects(key + 5 < _nonDefaultSet.size());
  77. return _nonDefaultSet[key]
  78. || _nonDefaultSet[key + 1]
  79. || _nonDefaultSet[key + 2]
  80. || _nonDefaultSet[key + 3]
  81. || _nonDefaultSet[key + 4]
  82. || _nonDefaultSet[key + 5]
  83. || (_base && _base->isNonDefaultPlural(key));
  84. }
  85. private:
  86. void setBaseId(const QString &baseId, const QString &pluralId);
  87. void applyDifferenceToMe(const MTPDlangPackDifference &difference);
  88. void applyValue(const QByteArray &key, const QByteArray &value);
  89. void resetValue(const QByteArray &key);
  90. void reset(const Language &language);
  91. void fillFromCustomContent(
  92. const QString &absolutePath,
  93. const QString &relativePath,
  94. const QByteArray &content);
  95. bool loadFromCustomFile(const QString &filePath);
  96. void loadFromContent(const QByteArray &content);
  97. void loadFromCustomContent(
  98. const QString &absolutePath,
  99. const QString &relativePath,
  100. const QByteArray &content);
  101. void updatePluralRules();
  102. void updateChoosingStickerReplacement();
  103. Instance *_derived = nullptr;
  104. QString _id, _pluralId;
  105. rpl::event_stream<QString> _idChanges;
  106. QString _name, _nativeName;
  107. QString _customFilePathAbsolute;
  108. QString _customFilePathRelative;
  109. QByteArray _customFileContent;
  110. int _version = 0;
  111. rpl::event_stream<> _updated;
  112. struct {
  113. bool support = false;
  114. int rightIndex = 0;
  115. int rightIndexNamed = 0;
  116. } _choosingStickerReplacement;
  117. mutable QString _systemLanguage;
  118. std::vector<QString> _values;
  119. std::vector<uchar> _nonDefaultSet;
  120. std::map<QByteArray, QByteArray> _nonDefaultValues;
  121. std::unique_ptr<Instance> _base;
  122. };
  123. namespace details {
  124. QString Current(ushort key);
  125. rpl::producer<QString> Value(ushort key);
  126. } // namespace details
  127. } // namespace Lang