~ubuntu-branches/ubuntu/trusty/fluxbox/trusty-proposed

« back to all changes in this revision

Viewing changes to src/FbTk/I18n.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// I18n.hh for Fluxbox Window Manager
2
 
// Copyright (c) 2001 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
3
 
//
4
 
// I18n.cc for Blackbox - an X11 Window manager
5
 
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
6
 
//
7
 
// Permission is hereby granted, free of charge, to any person obtaining a
8
 
// copy of this software and associated documentation files (the "Software"),
9
 
// to deal in the Software without restriction, including without limitation
10
 
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
 
// and/or sell copies of the Software, and to permit persons to whom the
12
 
// Software is furnished to do so, subject to the following conditions:
13
 
//
14
 
// The above copyright notice and this permission notice shall be included in
15
 
// all copies or substantial portions of the Software.
16
 
//
17
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20
 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
 
// DEALINGS IN THE SOFTWARE.
24
 
 
25
 
// $Id: I18n.cc 4026 2005-05-18 08:32:17Z fluxgen $
26
 
 
27
 
/* Note:
28
 
 * A good reference for the older non-gettext style I18n
29
 
 * functions is the "Locale tutorial"
30
 
 *     Written by Patrick D'Cruze (pdcruze@orac.iinet.com.au)
31
 
 * A copy of which is available (at the time of writing) here:
32
 
 * http://www.kulichki.com/moshkow/CYRILLIC/locale-tutorial-0_8.txt
33
 
 */
34
 
 
35
 
//usr GNU extensions
36
 
#ifndef  _GNU_SOURCE
37
 
#define  _GNU_SOURCE
38
 
#endif // _GNU_SOURCE
39
 
 
40
 
#include "I18n.hh"
41
 
 
42
 
#include <X11/Xlocale.h>
43
 
 
44
 
#ifdef HAVE_CSTDLIB
45
 
  #include <cstdlib>
46
 
#else
47
 
  #include <stdlib.h>
48
 
#endif
49
 
#ifdef HAVE_CSTRING
50
 
  #include <cstring>
51
 
#else
52
 
  #include <string.h>
53
 
#endif
54
 
#ifdef HAVE_CSTDIO
55
 
  #include <cstdio>
56
 
#else
57
 
  #include <stdio.h>
58
 
#endif
59
 
 
60
 
#include <iostream>
61
 
 
62
 
using std::cerr;
63
 
using std::endl;
64
 
using std::string;
65
 
 
66
 
namespace FbTk {
67
 
 
68
 
void NLSInit(const char *catalog) {
69
 
    I18n *i18n = I18n::instance();
70
 
    i18n->openCatalog(catalog);
71
 
}
72
 
 
73
 
 
74
 
I18n::I18n():m_multibyte(false), m_catalog_fd((nl_catd)(-1)) {
75
 
#ifdef  HAVE_SETLOCALE
76
 
    //make sure we don't get 0 to m_locale string
77
 
    char *temp = setlocale(LC_MESSAGES, "");
78
 
    m_locale = ( temp ?  temp : ""); 
79
 
    if (m_locale.empty()) {
80
 
        cerr<<"Warning: Failed to set locale, reverting to \"C\""<<endl;
81
 
#endif // HAVE_SETLOCALE
82
 
 
83
 
        m_locale = "C";
84
 
 
85
 
#ifdef  HAVE_SETLOCALE
86
 
 
87
 
    } else {            
88
 
 
89
 
        setlocale(LC_TIME, "");
90
 
        // MB_CUR_MAX returns the size of a char in the current locale
91
 
        if (MB_CUR_MAX > 1)
92
 
            m_multibyte = true;
93
 
                
94
 
        // truncate any encoding off the end of the locale
95
 
                                
96
 
        // remove everything after @
97
 
        string::size_type index = m_locale.find('@');
98
 
        if (index != string::npos)
99
 
            m_locale.erase(index); //erase all characters starting at index                             
100
 
        // remove everything after .            
101
 
        index = m_locale.find('.');
102
 
        if (index != string::npos) 
103
 
            m_locale.erase(index); //erase all characters starting at index 
104
 
        // remove everything before =           
105
 
        index = m_locale.find('=');
106
 
        if (index != string::npos) 
107
 
            m_locale.erase(0,index+1); //erase all characters starting up to index 
108
 
    }
109
 
#endif // HAVE_SETLOCALE
110
 
}
111
 
 
112
 
 
113
 
I18n::~I18n() {
114
 
 
115
 
#if defined(NLS) && defined(HAVE_CATCLOSE)
116
 
    if (m_catalog_fd != (nl_catd)-1)
117
 
        catclose(m_catalog_fd);
118
 
#endif // HAVE_CATCLOSE
119
 
}
120
 
 
121
 
I18n *I18n::instance() {
122
 
    static I18n singleton; //singleton object
123
 
    return &singleton;
124
 
}
125
 
 
126
 
void I18n::openCatalog(const char *catalog) {
127
 
#if defined(NLS) && defined(HAVE_CATOPEN)
128
 
        
129
 
    string catalog_filename = LOCALEPATH;
130
 
    catalog_filename += '/';
131
 
    catalog_filename += m_locale;
132
 
    catalog_filename += '/';
133
 
    catalog_filename += catalog;
134
 
 
135
 
#ifdef MCLoadBySet
136
 
    m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet);
137
 
#else // !MCLoadBySet
138
 
    m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE);
139
 
#endif // MCLoadBySet
140
 
 
141
 
    if (m_catalog_fd == (nl_catd)-1) {
142
 
        cerr<<"Warning: Failed to open file("<<catalog_filename<<")"<<endl;
143
 
        cerr<<"for translation, using default messages."<<endl;
144
 
    }
145
 
        
146
 
#else // !HAVE_CATOPEN
147
 
        
148
 
    m_catalog_fd = (nl_catd)-1;
149
 
#endif // HAVE_CATOPEN
150
 
}
151
 
 
152
 
 
153
 
const char *I18n::getMessage(int set_number, int message_number, 
154
 
                             const char *default_message) const {
155
 
 
156
 
#if defined(NLS) && defined(HAVE_CATGETS)
157
 
    if (m_catalog_fd != (nl_catd)-1)
158
 
        return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message);
159
 
    else
160
 
#endif // NLS && HAVE_CATGETS
161
 
        return default_message;
162
 
}
163
 
 
164
 
}; // end namespace FbTk