~ubuntu-branches/ubuntu/warty/fluxbox/warty

« back to all changes in this revision

Viewing changes to src/i18n.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matt Hope
  • Date: 2002-04-12 22:08:52 UTC
  • Revision ID: james.westby@ubuntu.com-20020412220852-0gbqxr57mgu63qdh
Tags: upstream-0.1.7
ImportĀ upstreamĀ versionĀ 0.1.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// i18n.hh for Fluxbox Window Manager
 
2
// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.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,v 1.2 2002/01/09 14:11:20 fluxgen Exp $
 
26
 
 
27
// stupid macros needed to access some functions in version 2 of the GNU C
 
28
// library
 
29
#ifndef   _GNU_SOURCE
 
30
#define   _GNU_SOURCE
 
31
#endif // _GNU_SOURCE
 
32
 
 
33
#ifdef    HAVE_CONFIG_H
 
34
#  include "../config.h"
 
35
#endif // HAVE_CONFIG_H
 
36
 
 
37
#include "i18n.hh"
 
38
 
 
39
#include <X11/Xlocale.h>
 
40
 
 
41
#ifdef    STDC_HEADERS
 
42
#  include <stdlib.h>
 
43
#  include <string.h>
 
44
#  include <stdio.h>
 
45
#endif // STDC_HEADERS
 
46
 
 
47
#ifdef    HAVE_LOCALE_H
 
48
#  include <locale.h>
 
49
#endif // HAVE_LOCALE_H
 
50
 
 
51
 
 
52
void NLSInit(const char *catalog) {
 
53
  I18n *i18n = I18n::instance();
 
54
  i18n->openCatalog(catalog);
 
55
}
 
56
 
 
57
 
 
58
I18n::I18n(void) {
 
59
#ifdef    HAVE_SETLOCALE
 
60
  locale = setlocale(LC_ALL, "");
 
61
  if (! locale) {
 
62
    fprintf(stderr, "failed to set locale, reverting to \"C\"\n");
 
63
#endif // HAVE_SETLOCALE
 
64
    locale = "C";
 
65
    mb = 0;
 
66
#ifdef    HAVE_SETLOCALE
 
67
  } else if (! strcmp(locale, "C") || ! strcmp(locale, "POSIX")) {
 
68
    mb = 0;
 
69
  } else {
 
70
    mb = 1;
 
71
    // truncate any encoding off the end of the locale
 
72
    char *l = strchr(locale, '.');
 
73
    if (l) *l = '\0';
 
74
  }
 
75
#endif // HAVE_SETLOCALE
 
76
 
 
77
  catalog_filename = (char *) 0;
 
78
  catalog_fd = (nl_catd) -1;
 
79
}
 
80
 
 
81
 
 
82
I18n::~I18n(void) {
 
83
  delete catalog_filename;
 
84
 
 
85
#if defined(NLS) && defined(HAVE_CATCLOSE)
 
86
  if (catalog_fd != (nl_catd) -1)
 
87
    catclose(catalog_fd);
 
88
#endif // HAVE_CATCLOSE
 
89
}
 
90
 
 
91
I18n *I18n::instance() {
 
92
        static I18n singleton; //singleton object
 
93
        return &singleton;
 
94
}
 
95
 
 
96
void I18n::openCatalog(const char *catalog) {
 
97
#if defined(NLS) && defined(HAVE_CATOPEN)
 
98
  int lp = strlen(LOCALEPATH), lc = strlen(locale),
 
99
      ct = strlen(catalog), len = lp + lc + ct + 3;
 
100
  catalog_filename = new char[len];
 
101
 
 
102
  strncpy(catalog_filename, LOCALEPATH, lp);
 
103
  *(catalog_filename + lp) = '/';
 
104
  strncpy(catalog_filename + lp + 1, locale, lc);
 
105
  *(catalog_filename + lp + lc + 1) = '/';
 
106
  strncpy(catalog_filename + lp + lc + 2, catalog, ct + 1);
 
107
 
 
108
#  ifdef    MCLoadBySet
 
109
  catalog_fd = catopen(catalog_filename, MCLoadBySet);
 
110
#  else // !MCLoadBySet
 
111
  catalog_fd = catopen(catalog_filename, NL_CAT_LOCALE);
 
112
#  endif // MCLoadBySet
 
113
 
 
114
  if (catalog_fd == (nl_catd) -1)
 
115
    fprintf(stderr, "failed to open catalog, using default messages\n");
 
116
        
 
117
#else // !HAVE_CATOPEN
 
118
  catalog_fd = (nl_catd) -1;
 
119
  catalog_filename = (char *) 0;
 
120
#endif // HAVE_CATOPEN
 
121
}
 
122
 
 
123
 
 
124
const char *I18n::getMessage(int set, int msg, const char *s) {
 
125
#if   defined(NLS) && defined(HAVE_CATGETS)
 
126
  if (catalog_fd != (nl_catd) -1)
 
127
    return (const char *) catgets(catalog_fd, set, msg, s);
 
128
  else
 
129
#endif
 
130
    return s;
 
131
}