i18n.py 5.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
"""
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(
17
    "pavelib.prereqs.install_prereqs",
18 19 20 21 22 23 24 25 26 27 28
    "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)
29
    cmd = "i18n_tool extract"
30 31 32 33 34 35 36 37

    if verbose:
        cmd += " -vv"

    sh(cmd)


@task
louyihua committed
38 39 40 41 42 43 44 45 46
def i18n_fastgenerate():
    """
    Compile localizable strings from sources without re-extracting strings first.
    """
    cmd = "i18n_tool generate"
    sh(cmd)


@task
47 48 49 50 51
@needs("pavelib.i18n.i18n_extract")
def i18n_generate():
    """
    Compile localizable strings from sources, extracting strings first.
    """
52
    cmd = "i18n_tool generate"
53 54 55 56 57 58 59 60 61 62
    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.
    """
63
    cmd = "i18n_tool generate"
64 65 66 67 68 69 70 71 72 73
    sh(cmd + " --strict")


@task
@needs("pavelib.i18n.i18n_extract")
def i18n_dummy():
    """
    Simulate international translation by generating dummy strings
    corresponding to source strings.
    """
74
    cmd = "i18n_tool dummy"
75
    sh(cmd)
76 77 78
    # Need to then compile the new dummy strings
    cmd = "i18n_tool generate"
    sh(cmd)
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 118 119 120 121 122 123 124 125 126 127 128 129 130


@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
    """
131
    cmd = "i18n_tool transifex"
132 133 134 135 136 137 138 139 140
    sh("{cmd} push".format(cmd=cmd))


@task
@needs("pavelib.i18n.i18n_validate_transifex_config")
def i18n_transifex_pull():
    """
    Pull translated strings from Transifex
    """
141
    cmd = "i18n_tool transifex"
142 143 144 145
    sh("{cmd} pull".format(cmd=cmd))


@task
Sarina Canelake committed
146
def i18n_rtl():
Sarina Canelake committed
147 148 149
    """
    Pull all RTL translations (reviewed AND unreviewed) from Transifex
    """
Sarina Canelake committed
150 151 152 153 154 155
    cmd = "i18n_tool transifex"
    sh(cmd + " rtl")

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

    cmd = "i18n_tool generate"
Sarina Canelake committed
156

Sarina Canelake committed
157 158 159 160 161 162
    sh(cmd + " --rtl")

    print("Committing translations...")
    sh('git clean -fdX conf/locale')
    sh('git add conf/locale')
    sh('git commit --amend')
Sarina Canelake committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182


@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
183

184

Sarina Canelake committed
185
@task
186
@needs(
187
    "pavelib.i18n.i18n_clean",
188 189 190 191 192 193 194 195 196
    "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
    """
197
    # sh('paver test_i18n')  # TODO tests were removed from repo, but there should still be tests that cover the translations...
198 199
    # Validate the recently pulled translations, and give a bail option
    cmd = "i18n_tool validate"
200
    print("\n\nValidating translations with `i18n_tool validate`...")
201 202 203 204 205 206 207 208
    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(
209 210
            'git commit --message='
            '"Update translations (autogenerated message)" --edit'
211
        )
212 213 214


@task
215 216 217 218 219 220 221 222
def i18n_clean():
    """
    Clean the i18n directory of artifacts
    """
    sh('git clean -fdX conf/locale')


@task
223 224 225 226 227 228 229 230 231
@needs(
    "pavelib.i18n.i18n_extract",
    "pavelib.i18n.i18n_transifex_push",
)
def i18n_robot_push():
    """
    Extract new strings, and push to transifex
    """
    pass