BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Translator.h
1 #pragma once
2 
3 #include <iostream>
4 #include <vector>
5 #include <string>
6 
7 #include <Poco/SharedPtr.h>
8 #include <Poco/Dynamic/Var.h>
9 
10 namespace BeeeOn {
11 
12 class Locale;
13 
14 class Translator {
15 public:
16  typedef Poco::SharedPtr<Translator> Ptr;
17 
18  virtual ~Translator();
19 
26  template <typename... Rest>
27  std::string formatSure(
28  const std::string &key,
29  const Rest & ... rest)
30  {
31  std::vector<Poco::Dynamic::Var> tmp;
32  formatCollect(tmp, rest...);
33  const auto &result = formatImpl(key, tmp);
34  return result.empty()? key : result;
35  }
36 
42  template <typename... Rest>
43  std::string format(
44  const std::string &key,
45  const std::string &def,
46  const Rest & ... rest)
47  {
48  std::vector<Poco::Dynamic::Var> tmp;
49  formatCollect(tmp, rest...);
50  const auto &result = formatImpl(key, tmp);
51  return result.empty() ?
52  formatFallback(def, tmp) : result;
53  }
54 
60  std::string vformat(
61  const std::string &key,
62  const std::string &def,
63  const std::vector<Poco::Dynamic::Var> &args)
64  {
65  const auto &result = formatImpl(key, args);
66  return result.empty() ?
67  formatFallback(def, args) : result;
68  }
69 
70 protected:
71  void prepareArg(std::vector<Poco::Dynamic::Var> &tmp, const char *first)
72  {
73  tmp.push_back(std::string(first));
74  }
75 
76  template <typename Type>
77  void prepareArg(std::vector<Poco::Dynamic::Var> &tmp, const Type &value)
78  {
79  tmp.push_back(value);
80  }
81 
82  void formatCollect(std::vector<Poco::Dynamic::Var> &)
83  {
84  }
85 
86  template <typename First, typename... Rest>
87  void formatCollect(std::vector<Poco::Dynamic::Var> &tmp,
88  const First &first,
89  const Rest & ... rest)
90  {
91  prepareArg(tmp, first);
92  formatCollect(tmp, rest...);
93  }
94 
98  virtual std::string formatImpl(
99  const std::string &key,
100  const std::vector<Poco::Dynamic::Var> &args) = 0;
101 
107  std::string formatFallback(
108  const std::string &def,
109  const std::vector<Poco::Dynamic::Var> &args) const;
110 };
111 
113 public:
114  typedef Poco::SharedPtr<TranslatorFactory> Ptr;
115 
116  virtual ~TranslatorFactory();
117 
122  virtual Translator *create(
123  const Locale &locale,
124  const std::string &name = "") = 0;
125 };
126 
127 }
std::string format(const std::string &key, const std::string &def, const Rest &...rest)
Definition: Translator.h:43
Definition: Translator.h:112
std::string formatSure(const std::string &key, const Rest &...rest)
Definition: Translator.h:27
std::string vformat(const std::string &key, const std::string &def, const std::vector< Poco::Dynamic::Var > &args)
Definition: Translator.h:60
std::string formatFallback(const std::string &def, const std::vector< Poco::Dynamic::Var > &args) const
Definition: Translator.cpp:16
Definition: Locale.h:12
virtual std::string formatImpl(const std::string &key, const std::vector< Poco::Dynamic::Var > &args)=0
virtual Translator * create(const Locale &locale, const std::string &name="")=0
Definition: Translator.h:14