~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to ext/native/i18n/i18n.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#pragma once
 
2
 
 
3
// I18N = I....18..dots.....N = INTERNATIONALIZATION
 
4
 
 
5
// Super simple I18N library.
 
6
// Just enough to be useful and usable.
 
7
// Spits out easy-to-edit utf-8 .INI files.
 
8
 
 
9
// As usual, everything is UTF-8. Nothing else allowed.
 
10
 
 
11
#include <map>
 
12
#include <string>
 
13
#include <vector>
 
14
 
 
15
#include "base/stringutil.h"
 
16
#include "file/ini_file.h"
 
17
 
 
18
// Reasonably thread safe.
 
19
 
 
20
class I18NRepo;
 
21
 
 
22
struct I18NEntry {
 
23
        I18NEntry(const std::string &t) : text(t), readFlag(false) {}
 
24
        I18NEntry() : readFlag(false) {}
 
25
        std::string text;
 
26
        bool readFlag;
 
27
};
 
28
 
 
29
struct I18NCandidate {
 
30
        I18NCandidate() : key(0), defVal(0) {}
 
31
        I18NCandidate(const char *k, const char *d) : key(k), defVal(d) {}
 
32
        const char *key;
 
33
        const char *defVal;
 
34
};
 
35
 
 
36
class I18NCategory {
 
37
public:
 
38
        // NOTE: Name must be a global constant string - it is not copied.
 
39
        I18NCategory(const char *name) : name_(name) {}
 
40
        const char *T(const char *key, const char *def = 0);
 
41
        const char *T(const std::string &key) {
 
42
                return T(key.c_str(), nullptr);
 
43
        }
 
44
 
 
45
        const std::map<std::string, std::string> &Missed() const {
 
46
                return missedKeyLog_;
 
47
        }
 
48
 
 
49
        void SetMap(const std::map<std::string, std::string> &m);
 
50
        const std::map<std::string, I18NEntry> &GetMap() { return map_; }
 
51
        void ClearMissed() { missedKeyLog_.clear(); }
 
52
        const char *GetName() const { return name_.c_str(); }
 
53
 
 
54
private:
 
55
        I18NCategory(I18NRepo *repo, const char *name) : name_(name) {}
 
56
 
 
57
        std::string name_;
 
58
 
 
59
        std::map<std::string, I18NEntry> map_;
 
60
        std::map<std::string, std::string> missedKeyLog_;
 
61
 
 
62
        // Noone else can create these.
 
63
        friend class I18NRepo;
 
64
 
 
65
        DISALLOW_COPY_AND_ASSIGN(I18NCategory);
 
66
};
 
67
 
 
68
class I18NRepo {
 
69
public:
 
70
        I18NRepo() {}
 
71
        ~I18NRepo();
 
72
 
 
73
        bool IniExists(const std::string &languageID) const;
 
74
        bool LoadIni(const std::string &languageID, const std::string &overridePath = ""); // NOT the filename!
 
75
        void SaveIni(const std::string &languageID);
 
76
 
 
77
        I18NCategory *GetCategory(const char *categoryName);
 
78
        const char *T(const char *category, const char *key, const char *def = 0);
 
79
 
 
80
private:
 
81
        std::string GetIniPath(const std::string &languageID) const;
 
82
        void Clear();
 
83
        I18NCategory *LoadSection(const IniFile::Section *section, const char *name);
 
84
        void SaveSection(IniFile &ini, IniFile::Section *section, I18NCategory *cat);
 
85
 
 
86
        std::map<std::string, I18NCategory *> cats_;
 
87
 
 
88
        DISALLOW_COPY_AND_ASSIGN(I18NRepo);
 
89
};
 
90
 
 
91
extern I18NRepo i18nrepo;
 
92
 
 
93
// These are simply talking to the one global instance of I18NRepo.
 
94
 
 
95
inline I18NCategory *GetI18NCategory(const char *categoryName) {
 
96
        if (!categoryName)
 
97
                return nullptr;
 
98
        return i18nrepo.GetCategory(categoryName);
 
99
}
 
100
 
 
101
inline const char *T(const char *category, const char *key, const char *def = 0) {
 
102
        return i18nrepo.T(category, key, def);
 
103
}
 
104
 
 
105
 
 
106