Skip to content

Commit

Permalink
Better options for the translation commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sh4nks committed Oct 20, 2016
1 parent c90f281 commit 42ca536
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ python:
install:
- "pip install -r requirements-dev.txt"
- "pip install coveralls"
- "pip install -e ."
# command to run tests
script:
- python manage.py compile_translations
- flaskbb translations compile
- py.test --cov=flaskbb --cov-report=term-missing tests
after_success:
- coveralls
5 changes: 4 additions & 1 deletion babel.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[ignore: .tox/**]
[ignore: .venv/**]
[ignore: **/plugins/**]
[python: **.py]
[ignore: **/node_modules/**]
[python: **/flaskbb/**.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_
68 changes: 49 additions & 19 deletions flaskbb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@

from flaskbb import create_app
from flaskbb._compat import iteritems
from flaskbb.extensions import db, whooshee
from flaskbb.extensions import db, whooshee, plugin_manager
from flaskbb.utils.populate import (create_test_data, create_welcome_forum,
create_user, create_default_groups,
create_default_settings, insert_bulk_data,
update_settings_from_fixture)
from flaskbb.utils.translations import (add_translations, compile_translations,
update_translations,
add_plugin_translations)
add_plugin_translations,
compile_plugin_translations,
update_plugin_translations)

_email_regex = r"[^@]+@[^@]+\.[^@]+"

Expand All @@ -56,17 +58,17 @@ class EmailType(click.ParamType):
supported values. All of these values have to be strings.
See :ref:`choice-opts` for an example.
"""
name = 'email'
name = "email"

def convert(self, value, param, ctx):
# Exact match
if re.match(_email_regex, value):
return value
else:
self.fail(('invalid email: %s' % value), param, ctx)
self.fail(("invalid email: %s" % value), param, ctx)

def __repr__(self):
return 'email'
return "email"


@click.group(cls=FlaskGroup, create_app=create_app)
Expand Down Expand Up @@ -175,29 +177,57 @@ def translations():
help="The plugin for which a language should be added.")
@click.argument("lang")
def new_translation(lang, plugin):
"""Adds a new language to the translations. 'lang' is the language code
of the language, like, 'de_AT'."""
"""Adds a new language to the translations. "lang" is the language code
of the language, like, "de_AT"."""
if plugin:
if plugin not in plugin_manager.all_plugins:
raise FlaskBBCLIError("Plugin {} not found.".format(plugin),
fg="red")
click.secho("[+] Adding new language {} for plugin {}..."
.format(lang, plugin), fg='cyan')
.format(lang, plugin), fg="cyan")
add_plugin_translations(plugin, lang)
else:
click.secho("[+] Adding new language {}...".format(lang), fg='cyan')
click.secho("[+] Adding new language {}...".format(lang), fg="cyan")
add_translations(lang)


@translations.command("update")
def update_translation():
@click.option("is_all", "--all", "-a", default=True, is_flag=True,
help="Updates the plugin translations as well.")
@click.option("--plugin", "-p", type=click.STRING,
help="The plugin for which the translations should be updated.")
def update_translation(is_all, plugin):
"""Updates all translations."""
click.secho("[+] Updating language files...", fg='cyan')
update_translations()
if plugin is not None:
if plugin not in plugin_manager.all_plugins:
raise FlaskBBCLIError("Plugin {} not found.".format(plugin),
fg="red")

click.secho("[+] Updating language files for plugin {}..."
.format(plugin), fg="cyan")
update_plugin_translations(plugin)
else:
click.secho("[+] Updating language files...", fg="cyan")
update_translations(include_plugins=is_all)


@translations.command("compile")
def compile_translation(lang):
@click.option("is_all", "--all", "-a", default=True, is_flag=True,
help="Compiles the plugin translations as well.")
@click.option("--plugin", "-p", type=click.STRING,
help="The plugin for which the translations should be compiled.")
def compile_translation(is_all, plugin):
"""Compiles all translations."""
click.secho("[+] Compiling language files...", fg='cyan')
compile_translations()
if plugin is not None:
if plugin not in plugin_manager.all_plugins:
raise FlaskBBCLIError("Plugin {} not found.".format(plugin),
fg="red")
click.secho("[+] Compiling language files for plugin {}..."
.format(plugin), fg="cyan")
compile_plugin_translations(plugin)
else:
click.secho("[+] Compiling language files...", fg="cyan")
compile_translations(include_plugins=is_all)


@cli.group()
Expand All @@ -210,21 +240,21 @@ def plugins():
@click.argument("plugin")
def new_plugin(plugin):
"""Creates a new plugin based on the plugin template."""
click.secho("[+] Creating new Plugin {}...".format(plugin), fg='cyan')
click.secho("[+] Creating new Plugin {}...".format(plugin), fg="cyan")


@plugins.command("install")
@click.argument("plugin")
def install_plugin(plugin):
"""Installs a new plugin from FlaskBB's Plugin Repository."""
click.secho("[+] Installing plugin {}...".format(plugin), fg='cyan')
"""Installs a new plugin from FlaskBB"s Plugin Repository."""
click.secho("[+] Installing plugin {}...".format(plugin), fg="cyan")


@plugins.command("uninstall")
@click.argument("plugin")
def uninstall_plugin(plugin):
"""Uninstalls a plugin from FlaskBB."""
click.secho("[+] Uninstalling plugin {}...".format(plugin), fg='cyan')
click.secho("[+] Uninstalling plugin {}...".format(plugin), fg="cyan")


@plugins.command("list")
Expand Down
30 changes: 20 additions & 10 deletions flaskbb/utils/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ def get_translations(self):
return translations


def update_translations():
"""Updates all translations."""
def update_translations(include_plugins=False):
"""Updates all translations.
:param include_plugins: If set to `True` it will also update the
translations for all plugins.
"""

# update flaskbb translations
translations_folder = os.path.join(current_app.root_path, "translations")
Expand All @@ -90,9 +94,10 @@ def update_translations():
subprocess.call(["pybabel", "update", "-i", source_file,
"-d", translations_folder])

# updates all plugin translations too
for plugin in plugin_manager.all_plugins:
update_plugin_translations(plugin)
if include_plugins:
# updates all plugin translations too
for plugin in plugin_manager.all_plugins:
update_plugin_translations(plugin)


def add_translations(translation):
Expand All @@ -107,16 +112,21 @@ def add_translations(translation):
"-d", translations_folder, "-l", translation])


def compile_translations():
"""Compiles all translations."""
def compile_translations(include_plugins=False):
"""Compiles all translations.
:param include_plugins: If set to `True` it will also compile the
translations for all plugins.
"""

# compile flaskbb translations
translations_folder = os.path.join(current_app.root_path, "translations")
subprocess.call(["pybabel", "compile", "-d", translations_folder])

# compile all plugin translations
for plugin in plugin_manager.all_plugins:
compile_plugin_translations(plugin)
if include_plugins:
# compile all plugin translations
for plugin in plugin_manager.all_plugins:
compile_plugin_translations(plugin)


def add_plugin_translations(plugin, translation):
Expand Down

0 comments on commit 42ca536

Please sign in to comment.