strings.py 3.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -*- coding: utf-8 -*-
"""
This file is part of GetTor, a service providing alternative methods to download
the Tor Browser.

:authors: Hiro <hiro@torproject.org>
          please also see AUTHORS file
:copyright: (c) 2008-2014, The Tor Project, Inc.
            (c) 2014, all entities within the AUTHORS file
:license: see included LICENSE for information
"""

import json
import os
import inspect
16
import re
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

strings = {}
translations = {}

_rundir = None

def setRundir(path):
    """Set the absolute path to the runtime directory.

    See :meth:`BaseOptions.postOptions`.

    :param string path: The path to set.
    """
    global _rundir
    _rundir = path

def getRundir():
    """Get the absolute path to the runtime directory.

    :rtype: string
    :returns: The path to the config file.
    """
    return _rundir

def find_run_dir(rundir=None):
    """Get the absolute path to the runtime directory.

    :rtype: string
    :returns: The path to the config file.
    """
    gRundir = getRundir()

    if gRundir is None:
        if rundir is not None:
            gRundir = os.path.abspath(os.path.expanduser(rundir))
        else:
            gRundir = os.getcwd()
    setRundir(gRundir)

    if not os.path.isdir(gRundir):  # pragma: no cover
        raise usage.UsageError(
            "Could not change to runtime directory: `%s'" % gRundir)

    return gRundir

def get_resource_path(filename, path):
    """
    Returns the absolute path of a resource
    """
    rundir = find_run_dir()
    prefix = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))), path)
    prefix = os.path.join(rundir, prefix)
    if not os.path.exists(prefix):
        prefix = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(prefix)))), path)

    return os.path.join(prefix, filename)

def get_version():
Hiro's avatar
Hiro committed
75
76
77
78
    """
    The current version
    """

79
80
81
82
83
84
    version = ""
    with open(get_resource_path('version.txt', '../share')) as f:
        version = f.read().strip()
    return version

def get_locales():
Hiro's avatar
Hiro committed
85
86
87
    """
    Get available_locales
    """
Hiro's avatar
Hiro committed
88

89
90
91
92
93
94
    filename = get_resource_path("available_locales.json", '../share/locale')
    locales = {}
    with open(filename, encoding='utf-8') as f:
        locales = json.load(f)
    return locales

Hiro's avatar
Hiro committed
95
def load_strings(current_locale):
96
97
98
99
100
101
    """
    Loads translated strings and fallback to English
    if the translation does not exist.
    """
    global strings, translations

Hiro's avatar
Hiro committed
102

103
104
105
106
    # Load all translations
    translations = {}
    available_locales = get_locales()

Hiro's avatar
Hiro committed
107
108
    if current_locale not in available_locales:
        current_locale = "en"
109

Hiro's avatar
Hiro committed
110
111
112
    filename = get_resource_path("{}.json".format(current_locale), '../share/locale')
    with open(filename, encoding='utf-8') as f:
        translations[current_locale] = json.load(f)
113
114

    strings = {}
Hiro's avatar
Hiro committed
115
116
    for s in translations[current_locale]:
        strings[s] = translations[current_locale][s]
Hiro's avatar
Hiro committed
117

118

119
120
121
122
def redact_emails(text):
    redacted_text = re.sub(r'[\w\.+-]+@[\w\.+-]+', '[REDACTED_EMAIL]', text)
    return redacted_text

123
124
125
126
127
128
129
130

def translated(k):
    """
    Returns a translated string.
    """
    return strings[k]

_ = translated