/src/wt/src/Wt/WMessageResources.h
Line | Count | Source |
1 | | // This may look like C code, but it's really -*- C++ -*- |
2 | | /* |
3 | | * Copyright (C) 2008 Emweb bv, Herent, Belgium. |
4 | | * |
5 | | * See the LICENSE file for terms of use. |
6 | | */ |
7 | | #ifndef WMESSAGE_RESOURCES_ |
8 | | #define WMESSAGE_RESOURCES_ |
9 | | |
10 | | #include <string> |
11 | | #include <vector> |
12 | | #include <map> |
13 | | #include <set> |
14 | | #include <Wt/WFlags.h> |
15 | | #include <Wt/WMessageResourceBundle.h> |
16 | | #include <Wt/WDllDefs.h> |
17 | | |
18 | | #ifdef WT_THREADED |
19 | | #include <mutex> |
20 | | #endif |
21 | | |
22 | | namespace Wt { |
23 | | |
24 | | class WString; |
25 | | |
26 | | class WT_API WMessageResources |
27 | | { |
28 | | public: |
29 | | WMessageResources(const std::string& path, bool loadInMemory = true); |
30 | | WMessageResources(const char *builtin); |
31 | | |
32 | | void hibernate(); |
33 | | |
34 | 0 | bool isBuiltin(const char *data) const { return builtin_ == data; } |
35 | 0 | const std::string& path() const { return path_; } |
36 | | |
37 | | LocalizedString resolveKey(const WLocale& locale, const std::string& key) const; |
38 | | LocalizedString resolvePluralKey(const WLocale& locale, |
39 | | const std::string& key, |
40 | | ::uint64_t amount) const; |
41 | | |
42 | | static int evalPluralCase(const std::string &expression, ::uint64_t n); |
43 | | |
44 | | std::set<std::string> keys(const WLocale& locale) const; |
45 | | |
46 | | private: |
47 | | typedef std::map<std::string, std::vector<std::string> > KeyValuesMap; |
48 | | |
49 | | struct Resource { |
50 | | KeyValuesMap map_; |
51 | | std::string pluralExpression_; |
52 | | unsigned pluralCount_; |
53 | | }; |
54 | | |
55 | | typedef std::map<std::string, Resource> ResourceMap; |
56 | | |
57 | | bool loadInMemory_; |
58 | | std::string path_; |
59 | | const char *builtin_; |
60 | | #ifdef WT_THREADED |
61 | | std::mutex resourceMutex_; |
62 | | #endif |
63 | | mutable ResourceMap resources_; |
64 | | |
65 | | void load(const WLocale& locale) const; |
66 | | LocalizedString resolve(const std::string& locale, const std::string& key) const; |
67 | | LocalizedString resolvePlural(const std::string& locale, const std::string& key, ::uint64_t amount) const; |
68 | | bool readResourceFile(const std::string& locale, Resource& resource) const; |
69 | | bool readResourceStream(std::istream &s, Resource& resource, |
70 | | const std::string &fileName) const; |
71 | | |
72 | | std::string findCase(const std::vector<std::string> &cases, |
73 | | std::string pluralExpression, |
74 | | ::uint64_t amount) const; |
75 | | }; |
76 | | |
77 | | } |
78 | | |
79 | | #endif // WMESSAGE_RESOURCES_ |