ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Translation.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2018 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 
24  static std::map<CString, CTranslationInfo> GetTranslations();
25 
27 };
28 
29 // All instances of modules share single message map using this class stored in
30 // CZNC.
31 class CTranslation {
32  public:
33  static CTranslation& Get();
34  CString Singular(const CString& sDomain, const CString& sContext,
35  const CString& sEnglish);
36  CString Plural(const CString& sDomain, const CString& sContext,
37  const CString& sEnglish, const CString& sEnglishes,
38  int iNum);
39 
40  void PushLanguage(const CString& sLanguage);
41  void PopLanguage();
42 
43  void NewReference(const CString& sDomain);
44  void DelReference(const CString& sDomain);
45 
46  private:
47  // Domain is either "znc" or "znc-foo" where foo is a module name
48  const std::locale& LoadTranslation(const CString& sDomain);
49  std::unordered_map<CString /* domain */,
50  std::unordered_map<CString /* language */, std::locale>>
51  m_Translations;
52  VCString m_sLanguageStack;
53  std::unordered_map<CString /* domain */, int> m_miReferences;
54 };
55 
57  explicit CLanguageScope(const CString& sLanguage);
59 };
60 
62  explicit CTranslationDomainRefHolder(const CString& sDomain);
64 
65  private:
66  const CString m_sDomain;
67 };
68 
69 // This is inspired by boost::locale::message, but without boost
71  public:
72  CDelayedTranslation() = default;
73  CDelayedTranslation(const CString& sDomain, const CString& sContext,
74  const CString& sEnglish)
75  : m_sDomain(sDomain), m_sContext(sContext), m_sEnglish(sEnglish) {}
76  CString Resolve() const;
77 
78  private:
79  CString m_sDomain;
80  CString m_sContext;
81  CString m_sEnglish;
82 };
83 
85  public:
87  : m_bTranslating(false), m_sText(sText) {}
90  : m_bTranslating(true), m_dTranslation(dTranslation) {}
91  CString Resolve() const {
92  return m_bTranslating ? m_dTranslation.Resolve() : m_sText;
93  }
94 
95  private:
96  bool m_bTranslating;
97  // TODO switch to std::variant<CString, CDelayedTranslation> after C++17
98  CString m_sText;
99  CDelayedTranslation m_dTranslation;
100 };
101 
102 // Used by everything except modules.
103 // CModule defines its own version of these functions.
105  protected:
106  static CString t_s(const CString& sEnglish, const CString& sContext = "");
107  static CInlineFormatMessage t_f(const CString& sEnglish,
108  const CString& sContext = "");
109  static CInlineFormatMessage t_p(const CString& sEnglish,
110  const CString& sEnglishes, int iNum,
111  const CString& sContext = "");
112  static CDelayedTranslation t_d(const CString& sEnglish,
113  const CString& sContext = "");
114 };
115 
116 #endif
static std::map< CString, CTranslationInfo > GetTranslations()
static CInlineFormatMessage t_f(const CString &sEnglish, const CString &sContext="")
COptionalTranslation(const CString &sText)
Definition: Translation.h:86
COptionalTranslation(const CDelayedTranslation &dTranslation)
Definition: Translation.h:89
void PushLanguage(const CString &sLanguage)
CTranslationDomainRefHolder(const CString &sDomain)
static CDelayedTranslation t_d(const CString &sEnglish, const CString &sContext="")
CString sSelfName
Definition: Translation.h:26
CString Resolve() const
Definition: Translation.h:84
CString Resolve() const
Definition: Translation.h:91
Definition: Translation.h:23
void NewReference(const CString &sDomain)
Definition: Translation.h:70
Definition: Translation.h:61
std::vector< CString > VCString
Definition: ZNCString.h:38
String class that is used inside ZNC.
Definition: ZNCString.h:68
void PopLanguage()
void DelReference(const CString &sDomain)
CString Plural(const CString &sDomain, const CString &sContext, const CString &sEnglish, const CString &sEnglishes, int iNum)
CDelayedTranslation()=default
Definition: Translation.h:56
static CTranslation & Get()
CDelayedTranslation(const CString &sDomain, const CString &sContext, const CString &sEnglish)
Definition: Translation.h:73
Definition: Translation.h:31
static CString t_s(const CString &sEnglish, const CString &sContext="")
CString Singular(const CString &sDomain, const CString &sContext, const CString &sEnglish)
CLanguageScope(const CString &sLanguage)
static CInlineFormatMessage t_p(const CString &sEnglish, const CString &sEnglishes, int iNum, const CString &sContext="")
Definition: ZNCString.h:673
COptionalTranslation(const char *s)
Definition: Translation.h:88
Definition: Translation.h:104