Pyrogenesis  trunk
language.hpp
Go to the documentation of this file.
1 // tinygettext - A gettext replacement that works directly on .po files
2 // Copyright (c) 2006 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgement in the product documentation would be
15 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
18 // 3. This notice may not be removed or altered from any source distribution.
19 
20 #ifndef HEADER_TINYGETTEXT_LANGUAGE_HPP
21 #define HEADER_TINYGETTEXT_LANGUAGE_HPP
22 
23 #include <string>
24 #include <unordered_map>
25 
26 namespace tinygettext {
27 
28 struct LanguageSpec;
29 
30 /** Lightweight wrapper around LanguageSpec */
31 class Language
32 {
33 private:
35 
36  Language(const LanguageSpec* language_spec);
37 
38 public:
39  /** Create a language from language and country code:
40  Example: Languge("de", "DE"); */
41  static Language from_spec(const std::string& language,
42  const std::string& country = std::string(),
43  const std::string& modifier = std::string());
44 
45  /** Create a language from language and country code:
46  Example: Languge("deutsch");
47  Example: Languge("de_DE"); */
48  static Language from_name(const std::string& str);
49 
50  /** Create a language from an environment variable style string (e.g de_DE.UTF-8@modifier) */
51  static Language from_env(const std::string& env);
52 
53  /** Compares two Languages, returns 0 on missmatch and a score
54  between 1 and 9 on match, the higher the score the better the
55  match */
56  static int match(const Language& lhs, const Language& rhs);
57 
58  /** Create an undefined Language object */
59  Language();
60 
61  explicit operator bool() const { return language_spec != NULL; }
62 
63  /** Returns the language code (i.e. de, en, fr) */
64  std::string get_language() const;
65 
66  /** Returns the country code (i.e. DE, AT, US) */
67  std::string get_country() const;
68 
69  /** Returns the modifier of the language (i.e. latn or Latn for
70  Serbian with non-cyrilic characters) */
71  std::string get_modifier() const;
72 
73  /** Returns the human readable name of the Language */
74  std::string get_name() const;
75 
76  /** Returns the Language as string in the form of an environment
77  variable: {language}_{country}@{modifier} */
78  std::string str() const;
79 
80  bool operator==(const Language& rhs) const;
81  bool operator!=(const Language& rhs) const;
82 
83  friend bool operator<(const Language& lhs, const Language& rhs);
84  friend struct Language_hash;
85 };
86 
87 inline bool operator<(const Language& lhs, const Language& rhs) {
88  return lhs.language_spec < rhs.language_spec;
89 }
90 
92 {
93  size_t operator()(const Language& v) const
94  {
95  return reinterpret_cast<size_t>(v.language_spec);
96  }
97 };
98 
99 } // namespace tinygettext
100 
101 #endif
102 
103 /* EOF */
std::string get_name() const
Returns the human readable name of the Language.
Definition: language.cpp:528
bool operator==(const Language &rhs) const
Definition: language.cpp:563
std::string get_country() const
Returns the country code (i.e.
Definition: language.cpp:510
size_t operator()(const Language &v) const
Definition: language.hpp:93
std::string get_modifier() const
Returns the modifier of the language (i.e.
Definition: language.cpp:519
Lightweight wrapper around LanguageSpec.
Definition: language.hpp:31
const LanguageSpec * language_spec
Definition: language.hpp:34
static Language from_spec(const std::string &language, const std::string &country=std::string(), const std::string &modifier=std::string())
Create a language from language and country code: Example: Languge("de", "DE");.
Definition: language.cpp:370
Definition: language.hpp:91
bool operator!=(const Language &rhs) const
Definition: language.cpp:569
Language()
Create an undefined Language object.
Definition: language.cpp:459
std::string get_language() const
Returns the language code (i.e.
Definition: language.cpp:501
std::string str() const
Returns the Language as string in the form of an environment variable: {language}_{country}modifier}...
Definition: language.cpp:537
Definition: language.cpp:31
Definition: dictionary.hpp:29
static int match(const Language &lhs, const Language &rhs)
Compares two Languages, returns 0 on missmatch and a score between 1 and 9 on match, the higher the score the better the match.
Definition: language.cpp:465
static Language from_env(const std::string &env)
Create a language from an environment variable style string (e.g de_DE.UTF-8)
Definition: language.cpp:420
friend bool operator<(const Language &lhs, const Language &rhs)
Definition: language.hpp:87
static Language from_name(const std::string &str)
Create a language from language and country code: Example: Languge("deutsch"); Example: Languge("de_D...
Definition: language.cpp:414