1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# ***********************IMPORTANT NMAP LICENSE TERMS************************
5# *                                                                         *
6# * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com LLC ("The Nmap  *
7# * Project"). Nmap is also a registered trademark of the Nmap Project.     *
8# *                                                                         *
9# * This program is distributed under the terms of the Nmap Public Source   *
10# * License (NPSL). The exact license text applying to a particular Nmap    *
11# * release or source code control revision is contained in the LICENSE     *
12# * file distributed with that version of Nmap or source code control       *
13# * revision. More Nmap copyright/legal information is available from       *
14# * https://nmap.org/book/man-legal.html, and further information on the    *
15# * NPSL license itself can be found at https://nmap.org/npsl. This header  *
16# * summarizes some key points from the Nmap license, but is no substitute  *
17# * for the actual license text.                                            *
18# *                                                                         *
19# * Nmap is generally free for end users to download and use themselves,    *
20# * including commercial use. It is available from https://nmap.org.        *
21# *                                                                         *
22# * The Nmap license generally prohibits companies from using and           *
23# * redistributing Nmap in commercial products, but we sell a special Nmap  *
24# * OEM Edition with a more permissive license and special features for     *
25# * this purpose. See https://nmap.org/oem                                  *
26# *                                                                         *
27# * If you have received a written Nmap license agreement or contract       *
28# * stating terms other than these (such as an Nmap OEM license), you may   *
29# * choose to use and redistribute Nmap under those terms instead.          *
30# *                                                                         *
31# * The official Nmap Windows builds include the Npcap software             *
32# * (https://npcap.org) for packet capture and transmission. It is under    *
33# * separate license terms which forbid redistribution without special      *
34# * permission. So the official Nmap Windows builds may not be              *
35# * redistributed without special permission (such as an Nmap OEM           *
36# * license).                                                               *
37# *                                                                         *
38# * Source is provided to this software because we believe users have a     *
39# * right to know exactly what a program is going to do before they run it. *
40# * This also allows you to audit the software for security holes.          *
41# *                                                                         *
42# * Source code also allows you to port Nmap to new platforms, fix bugs,    *
43# * and add new features.  You are highly encouraged to submit your         *
44# * changes as a Github PR or by email to the dev@nmap.org mailing list     *
45# * for possible incorporation into the main distribution. Unless you       *
46# * specify otherwise, it is understood that you are offering us very       *
47# * broad rights to use your submissions as described in the Nmap Public    *
48# * Source License Contributor Agreement. This is important because we      *
49# * fund the project by selling licenses with various terms, and also       *
50# * because the inability to relicense code has caused devastating          *
51# * problems for other Free Software projects (such as KDE and NASM).       *
52# *                                                                         *
53# * The free version of Nmap is distributed in the hope that it will be     *
54# * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of  *
55# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,        *
56# * indemnification and commercial support are all available through the    *
57# * Npcap OEM program--see https://nmap.org/oem.                            *
58# *                                                                         *
59# ***************************************************************************/
60
61import locale
62import os
63
64from zenmapCore.Name import APP_NAME
65
66
67def get_locales():
68    """Get a list of locales to use based on system configuration."""
69    locales = []
70    # locale.getdefaultlocales already looks at LANG et al. on Unix but not on
71    # Windows. We look at the environment variables first to allow overriding
72    # the system-wide setting on Windows.
73    for envar in ("LANGUAGE", "LC_ALL", "LC_MESSAGES", "LANG"):
74        val = os.environ.get(envar)
75        if val:
76            locales = val.split(":")
77            break
78    try:
79        loc, enc = locale.getdefaultlocale()
80        if loc is not None:
81            locales.append(loc)
82    except ValueError:
83        # locale.getdefaultlocale can fail with ValueError on certain locale
84        # names; it has been seen with at least en_NG.
85        # http://bugs.python.org/issue6895
86        pass
87    return locales
88
89
90def install_gettext(locale_dir):
91    try:
92        locale.setlocale(locale.LC_ALL, '')
93    except locale.Error:
94        # This can happen if the LANG environment variable is set to something
95        # invalid, like LANG=nothing or LANG=en_US/utf8 or LANG=us-ascii.
96        # Continue without internationalization.
97        pass
98
99    try:
100        import gettext
101    except ImportError:
102        pass
103    else:
104        t = gettext.translation(
105                APP_NAME, locale_dir, languages=get_locales(), fallback=True)
106        t.install(unicode=True)
107
108# Install a dummy _ function so modules can safely use it after importing this
109# module, even if they don't install the gettext version.
110
111import __builtin__
112__builtin__.__dict__["_"] = lambda s: s
113