Adonthell  0.4
nls.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002/2003/2016 Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /**
20  * @file nls.cc
21  *
22  * @author Kai Sterker
23  * @brief National Language Support
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #include <locale.h>
31 #include "gettext.h"
32 #include "nls.h"
33 
34 std::string nls::current_lang;
35 
36 // Initialize NLS
37 void nls::init (config &myconfig)
38 {
39 #if ENABLE_NLS
40  // if no language specified in the config file, determine
41  // the locale from the environment variables
42  if (myconfig.language == "")
43  setlocale (LC_MESSAGES, "");
44  // otherwise overwrite any environment variables
45  else
46  set_language (myconfig.language);
47 
48  // open the message catalogue
49  std::string location = "/usr/share/locale";
50  const char *domain = myconfig.game_name.c_str ();
51 
52  bindtextdomain (domain, location.c_str ());
53  textdomain (domain);
54  bind_textdomain_codeset(domain, "UTF-8");
55 #endif
56 }
57 
58 // Set the language to use
59 void nls::set_language (const string &language)
60 {
61 #if ENABLE_NLS
62  current_lang = language.empty() ? "LANGUAGE" : "LANGUAGE=" + language;
63  putenv ((char *) current_lang.c_str ());
64  {
65  // tell gettext that the language has changed
66  extern int _nl_msg_cat_cntr;
67  ++_nl_msg_cat_cntr;
68  }
69 
70  setlocale (LC_ALL,"");
71  setlocale (LC_MESSAGES, language.c_str ());
72 #endif
73 }
74 
75 // Translate some text
76 const string nls::translate (const string &text)
77 {
78 #if ENABLE_NLS
79  return gettext (text.c_str ());
80 #else
81  return text;
82 #endif
83 }
National Language Support.
static void init(config &myconfig)
Initialize national language support.
Definition: nls.cc:37
string language
Language to use if NLS was compiled in.
Definition: prefs.h:130
static const string translate(const string &text)
Translate the given string if it&#39;s found in the message catalogue.
Definition: nls.cc:76
static void set_language(const string &language)
Set or change the language to use.
Definition: nls.cc:59
string game_name
Name of the game that is running at present.
Definition: prefs.h:135
This class contains the engine&#39;s configuration read either from the config file or from the command l...
Definition: prefs.h:74