i18n.py 4.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
"""
Internationalization tasks
"""
import sys
import subprocess
from path import path
from paver.easy import task, cmdopts, needs, sh

try:
    from pygments.console import colorize
except ImportError:
    colorize = lambda color, text: text  # pylint: disable-msg=invalid-name


@task
@needs(
    "pavelib.i18n.i18n_validate_gettext",
    "pavelib.assets.compile_coffeescript",
)
@cmdopts([
    ("verbose", "v", "Sets 'verbose' to True"),
])
def i18n_extract(options):
    """
    Extract localizable strings from sources
    """
    verbose = getattr(options, "verbose", None)
28
    cmd = "i18n_tool extract"
29 30 31 32 33 34 35 36 37 38 39 40 41

    if verbose:
        cmd += " -vv"

    sh(cmd)


@task
@needs("pavelib.i18n.i18n_extract")
def i18n_generate():
    """
    Compile localizable strings from sources, extracting strings first.
    """
42
    cmd = "i18n_tool generate"
43 44 45 46 47 48 49 50 51 52
    sh(cmd)


@task
@needs("pavelib.i18n.i18n_extract")
def i18n_generate_strict():
    """
    Compile localizable strings from sources, extracting strings first.
    Complains if files are missing.
    """
53
    cmd = "i18n_tool generate"
54 55 56 57 58 59 60 61 62 63
    sh(cmd + " --strict")


@task
@needs("pavelib.i18n.i18n_extract")
def i18n_dummy():
    """
    Simulate international translation by generating dummy strings
    corresponding to source strings.
    """
64
    cmd = "i18n_tool dummy"
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    sh(cmd)


@task
def i18n_validate_gettext():
    """
    Make sure GNU gettext utilities are available
    """

    returncode = subprocess.call(['which', 'xgettext'])

    if returncode != 0:
        msg = colorize(
            'red',
            "Cannot locate GNU gettext utilities, which are "
            "required by django for internationalization.\n (see "
            "https://docs.djangoproject.com/en/dev/topics/i18n/"
            "translation/#message-files)\nTry downloading them from "
            "http://www.gnu.org/software/gettext/ \n"
        )

        sys.stderr.write(msg)
        sys.exit(1)


@task
def i18n_validate_transifex_config():
    """
    Make sure config file with username/password exists
    """
    home = path('~').expanduser()
    config = home / '.transifexrc'

    if not config.isfile or config.getsize == 0:
        msg = colorize(
            'red',
            "Cannot connect to Transifex, config file is missing"
            " or empty: {config} \nSee "
            "http://help.transifex.com/features/client/#transifexrc \n".format(
                config=config,
            )
        )

        sys.stderr.write(msg)
        sys.exit(1)


@task
@needs("pavelib.i18n.i18n_validate_transifex_config")
def i18n_transifex_push():
    """
    Push source strings to Transifex for translation
    """
118
    cmd = "i18n_tool transifex"
119 120 121 122 123 124 125 126 127
    sh("{cmd} push".format(cmd=cmd))


@task
@needs("pavelib.i18n.i18n_validate_transifex_config")
def i18n_transifex_pull():
    """
    Pull translated strings from Transifex
    """
128
    cmd = "i18n_tool transifex"
129 130 131 132
    sh("{cmd} pull".format(cmd=cmd))


@task
Sarina Canelake committed
133
def i18n_rtl():
Sarina Canelake committed
134 135 136
    """
    Pull all RTL translations (reviewed AND unreviewed) from Transifex
    """
Sarina Canelake committed
137 138 139 140 141 142
    cmd = "i18n_tool transifex"
    sh(cmd + " rtl")

    print("Now generating langugage files...")

    cmd = "i18n_tool generate"
Sarina Canelake committed
143

Sarina Canelake committed
144 145 146 147 148 149
    sh(cmd + " --rtl")

    print("Committing translations...")
    sh('git clean -fdX conf/locale')
    sh('git add conf/locale')
    sh('git commit --amend')
Sarina Canelake committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169


@task
def i18n_ltr():
    """
    Pull all LTR translations (reviewed AND unreviewed) from Transifex
    """
    cmd = "i18n_tool transifex"
    sh(cmd + " ltr")

    print("Now generating langugage files...")

    cmd = "i18n_tool generate"

    sh(cmd + " --ltr")

    print("Committing translations...")
    sh('git clean -fdX conf/locale')
    sh('git add conf/locale')
    sh('git commit --amend')
Sarina Canelake committed
170

171

Sarina Canelake committed
172
@task
173 174 175 176 177 178 179 180 181 182 183
@needs(
    "pavelib.i18n.i18n_transifex_pull",
    "pavelib.i18n.i18n_extract",
    "pavelib.i18n.i18n_dummy",
    "pavelib.i18n.i18n_generate_strict",
)
def i18n_robot_pull():
    """
    Pull source strings, generate po and mo files, and validate
    """
    sh('git clean -fdX conf/locale')
184
    # sh('paver test_i18n')  # tests were removed from repo, but there should still be tests that cover the translations...
185

186 187 188 189 190 191 192 193 194 195 196 197 198
    # Validate the recently pulled translations, and give a bail option
    cmd = "i18n_tool validate"
    sh("{cmd}".format(cmd=cmd))

    con = raw_input("Continue with committing these translations (y/n)? ")

    if con.lower() == 'y':
        sh('git add conf/locale')

        sh(
            'git commit --message="Update translations '
            '(autogenerated message)" --edit'
        )
199 200 201 202 203 204 205 206 207 208 209 210


@task
@needs(
    "pavelib.i18n.i18n_extract",
    "pavelib.i18n.i18n_transifex_push",
)
def i18n_robot_push():
    """
    Extract new strings, and push to transifex
    """
    pass