ZNC  trunk
Translation.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2024 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ZNC_TRANSLATION_H
18 #define ZNC_TRANSLATION_H
19 
20 #include <znc/ZNCString.h>
21 #include <unordered_map>
22 #include <variant>
23 
25  static std::map<CString, CTranslationInfo> GetTranslations();
26 
28 };
29 
30 // All instances of modules share single message map using this class stored in
31 // CZNC.
32 class CTranslation {
33  public:
34  static CTranslation& Get();
35  CString Singular(const CString& sDomain, const CString& sContext,
36  const CString& sEnglish);
37  CString Plural(const CString& sDomain, const CString& sContext,
38  const CString& sEnglish, const CString& sEnglishes,
39  int iNum);
40 
41  void PushLanguage(const CString& sLanguage);
42  void PopLanguage();
43 
44  void NewReference(const CString& sDomain);
45  void DelReference(const CString& sDomain);
46 
47  private:
48  // Domain is either "znc" or "znc-foo" where foo is a module name
49  const std::locale& LoadTranslation(const CString& sDomain);
50  std::unordered_map<CString /* domain */,
51  std::unordered_map<CString /* language */, std::locale>>
52  m_Translations;
53  VCString m_sLanguageStack;
54  std::unordered_map<CString /* domain */, int> m_miReferences;
55 };
56 
58  explicit CLanguageScope(const CString& sLanguage);
60 };
61 
63  explicit CTranslationDomainRefHolder(const CString& sDomain);
65 
66  private:
67  const CString m_sDomain;
68 };
69 
70 // This is inspired by boost::locale::message, but without boost
72  public:
73  CDelayedTranslation() = default;
74  CDelayedTranslation(const CString& sDomain, const CString& sContext,
75  const CString& sEnglish)
76  : m_sDomain(sDomain), m_sContext(sContext), m_sEnglish(sEnglish) {}
77  CString Resolve() const;
78 
79  private:
80  CString m_sDomain;
81  CString m_sContext;
82  CString m_sEnglish;
83 };
84 
86  public:
87  COptionalTranslation(const CString& sText) : m_text(sText) {}
89  COptionalTranslation(const CDelayedTranslation& dTranslation) : m_text(dTranslation) {}
90  CString Resolve() const {
91  if (m_text.index() == 0) {
92  return std::get<0>(m_text);
93  }
94  return std::get<1>(m_text).Resolve();
95  }
96 
97  private:
98  std::variant<CString, CDelayedTranslation> m_text;
99 };
100 
101 // Used by everything except modules.
102 // CModule defines its own version of these functions.
104  protected:
105  static CString t_s(const CString& sEnglish, const CString& sContext = "");
106  static CInlineFormatMessage t_f(const CString& sEnglish,
107  const CString& sContext = "");
108  static CInlineFormatMessage t_p(const CString& sEnglish,
109  const CString& sEnglishes, int iNum,
110  const CString& sContext = "");
111  static CDelayedTranslation t_d(const CString& sEnglish,
112  const CString& sContext = "");
113 };
114 
115 #endif
std::vector< CString > VCString
Definition: ZNCString.h:38
Definition: Translation.h:103
static CString t_s(const CString &sEnglish, const CString &sContext="")
static CDelayedTranslation t_d(const CString &sEnglish, const CString &sContext="")
static CInlineFormatMessage t_f(const CString &sEnglish, const CString &sContext="")
static CInlineFormatMessage t_p(const CString &sEnglish, const CString &sEnglishes, int iNum, const CString &sContext="")
Definition: Translation.h:71
CDelayedTranslation()=default
CString Resolve() const
CDelayedTranslation(const CString &sDomain, const CString &sContext, const CString &sEnglish)
Definition: Translation.h:74
Definition: ZNCString.h:673
Definition: Translation.h:85
COptionalTranslation(const CDelayedTranslation &dTranslation)
Definition: Translation.h:89
CString Resolve() const
Definition: Translation.h:90
COptionalTranslation(const CString &sText)
Definition: Translation.h:87
COptionalTranslation(const char *s)
Definition: Translation.h:88
String class that is used inside ZNC.
Definition: ZNCString.h:68
Definition: Translation.h:32
void NewReference(const CString &sDomain)
void DelReference(const CString &sDomain)
void PushLanguage(const CString &sLanguage)
void PopLanguage()
static CTranslation & Get()
CString Singular(const CString &sDomain, const CString &sContext, const CString &sEnglish)
CString Plural(const CString &sDomain, const CString &sContext, const CString &sEnglish, const CString &sEnglishes, int iNum)
Definition: Translation.h:57
CLanguageScope(const CString &sLanguage)
Definition: Translation.h:62
CTranslationDomainRefHolder(const CString &sDomain)
Definition: Translation.h:24
CString sSelfName
Definition: Translation.h:27
static std::map< CString, CTranslationInfo > GetTranslations()