1/*
2 * nls.c :  Helpers for NLS programs.
3 *
4 * ====================================================================
5 *    Licensed to the Apache Software Foundation (ASF) under one
6 *    or more contributor license agreements.  See the NOTICE file
7 *    distributed with this work for additional information
8 *    regarding copyright ownership.  The ASF licenses this file
9 *    to you under the Apache License, Version 2.0 (the
10 *    "License"); you may not use this file except in compliance
11 *    with the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 *    Unless required by applicable law or agreed to in writing,
16 *    software distributed under the License is distributed on an
17 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 *    KIND, either express or implied.  See the License for the
19 *    specific language governing permissions and limitations
20 *    under the License.
21 * ====================================================================
22 */
23
24#include <stdlib.h>
25
26#ifndef WIN32
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <unistd.h>
31#endif
32
33#include <apr_errno.h>
34
35#include "svn_nls.h"
36#include "svn_error.h"
37#include "svn_pools.h"
38#include "svn_path.h"
39
40#include "svn_private_config.h"
41
42svn_error_t *
43svn_nls_init(void)
44{
45  svn_error_t *err = SVN_NO_ERROR;
46
47#ifdef ENABLE_NLS
48  if (getenv("SVN_LOCALE_DIR"))
49    {
50      bindtextdomain(PACKAGE_NAME, getenv("SVN_LOCALE_DIR"));
51    }
52  else
53    {
54#ifdef WIN32
55      WCHAR ucs2_path[MAX_PATH];
56      char* utf8_path;
57      const char* internal_path;
58      apr_pool_t* pool;
59      apr_size_t inwords, outbytes, outlength;
60
61      apr_pool_create(&pool, 0);
62      /* get exe name - our locale info will be in '../share/locale' */
63      inwords = GetModuleFileNameW(0, ucs2_path,
64                                   sizeof(ucs2_path) / sizeof(ucs2_path[0]));
65      if (! inwords)
66        {
67          /* We must be on a Win9x machine, so attempt to get an ANSI path,
68             and convert it to Unicode. */
69          CHAR ansi_path[MAX_PATH];
70
71          if (GetModuleFileNameA(0, ansi_path, sizeof(ansi_path)))
72            {
73              inwords =
74                MultiByteToWideChar(CP_ACP, 0, ansi_path, -1, ucs2_path,
75                                    sizeof(ucs2_path) / sizeof(ucs2_path[0]));
76              if (! inwords)
77                {
78                err =
79                  svn_error_createf(APR_EINVAL, NULL,
80                                    _("Can't convert string to UCS-2: '%s'"),
81                                    ansi_path);
82                }
83            }
84          else
85            {
86              err = svn_error_create(APR_EINVAL, NULL,
87                                     _("Can't get module file name"));
88            }
89        }
90
91      if (! err)
92        {
93          outbytes = outlength = 3 * (inwords + 1);
94          utf8_path = apr_palloc(pool, outlength);
95
96          outbytes = WideCharToMultiByte(CP_UTF8, 0, ucs2_path, inwords,
97                                         utf8_path, outbytes, NULL, NULL);
98
99          if (outbytes == 0)
100            {
101              err = svn_error_wrap_apr(apr_get_os_error(),
102                                       _("Can't convert module path "
103                                         "to UTF-8 from UCS-2: '%s'"),
104                                       ucs2_path);
105            }
106          else
107            {
108              utf8_path[outlength - outbytes] = '\0';
109              internal_path = svn_dirent_internal_style(utf8_path, pool);
110              /* get base path name */
111              internal_path = svn_dirent_dirname(internal_path, pool);
112              internal_path = svn_dirent_join(internal_path,
113                                              SVN_LOCALE_RELATIVE_PATH,
114                                              pool);
115              bindtextdomain(PACKAGE_NAME, internal_path);
116            }
117        }
118      svn_pool_destroy(pool);
119    }
120#else /* ! WIN32 */
121      bindtextdomain(PACKAGE_NAME, SVN_LOCALE_DIR);
122    }
123#endif /* WIN32 */
124
125#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
126  bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
127#endif /* HAVE_BIND_TEXTDOMAIN_CODESET */
128
129#endif /* ENABLE_NLS */
130
131  return err;
132}
133