Adonthell  0.4
nls.cc
Go to the documentation of this file.
1 /*
2  $Id: nls.cc,v 1.10 2006/09/12 06:44:38 ksterker Exp $
3 
4  Copyright (C) 2002/2003 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file nls.cc
17  *
18  * @author Kai Sterker
19  * @brief National Language Support
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <locale.h>
27 #include "gettext.h"
28 #include "nls.h"
29 
30 // Initialize NLS
31 void nls::init (config &myconfig)
32 {
33 #if ENABLE_NLS
34  // if no language specified in the config file, determine
35  // the locale from the environment variables
36  if (myconfig.language == "")
37  setlocale (LC_MESSAGES, "");
38  // otherwise overwrite any environment variables
39  else
40  set_language (myconfig.language);
41 
42  // open the message catalogue
43  std::string location = "/usr/share/locale";
44  const char *domain = myconfig.game_name.c_str ();
45 
46  bindtextdomain (domain, location.c_str ());
47  textdomain (domain);
48  bind_textdomain_codeset(domain, "UTF-8");
49 #endif
50 }
51 
52 // Set the language to use
53 void nls::set_language (const string &language)
54 {
55 #if ENABLE_NLS
56 #ifndef __BEOS__
57  string lang = "LANGUAGE=" + language;
58  putenv ((char *) lang.c_str ());
59 #else
60  // TODO: no putenv on BEOS, but there should be setenv
61 #endif
62  {
63  // tell gettext that the language has changed
64  extern int _nl_msg_cat_cntr;
65  ++_nl_msg_cat_cntr;
66  }
67 
68  setlocale (LC_MESSAGES, language.c_str ());
69 #endif
70 }
71 
72 // Translate some text
73 const char* nls::translate (const string &text)
74 {
75 #if ENABLE_NLS
76  return gettext (text.c_str ());
77 #else
78  return text.c_str ();
79 #endif
80 }