Skip to content

Commit

Permalink
Getting started on translations (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed May 2, 2016
1 parent ec7dbed commit 88c9516
Show file tree
Hide file tree
Showing 18 changed files with 566 additions and 32 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
*.pyc
changelog.sh
babel
.DS_Store
.coverage
_build
Expand Down
44 changes: 40 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ Generate the documentation with:
cd docs && ./build.sh

## CSS Themes
As part of the npm build process, CSS for Caravel is compiled from ```Less```, a dynamic stylesheet language.
As part of the npm build process, CSS for Caravel is compiled from `Less`, a dynamic stylesheet language.

It's possible to customize or add your own theme to Caravel, either by overriding CSS rules or preferably
by modifying the Less variables or files in ```assets/stylesheets/less/```.
by modifying the Less variables or files in `assets/stylesheets/less/`.

The ```variables.less``` and ```bootswatch.less``` files that ship with Caravel are derived from
The `variables.less` and `bootswatch.less` files that ship with Caravel are derived from
[Bootswatch](https://bootswatch.com) and thus extend Bootstrap. Modify variables in these files directly, or
swap them out entirely with the equivalent files from other Bootswatch (themes)[https://github.com/thomaspark/bootswatch.git]

Expand All @@ -179,7 +179,43 @@ meets these guidelines:
as part of the same PR. Doc string are often sufficient, make
sure to follow the sphinx compatible standards.
3. The pull request should work for Python 2.6, 2.7, and ideally python 3.3.
`from __future__ import ` will be required in every `.py` file soon.
``from __future__ import`` will be required in every `.py` file soon.
4. Code will be reviewed by re running the unittests, flake8 and syntax
should be as rigorous as the core Python project.
5. Please rebase and resolve all conflicts before submitting.


## Tranlations

We use [Babel](http://babel.pocoo.org/en/latest/) to translate Caravel. The
key is to instrument the strings that need translation using
`from flask.ext.babelpkg import lazy_gettext as _`. Once this is imported in
a module, all you have to do is to `_("Wrap your strings")` using the
underscore `_` "function".

To enable changing language in your environment, you can simply add the
`LANGUAGES` parameter to your `caravel_config.py`. Having more than one
options here will add a language selection dropdown on the right side of the
navigation bar.

LANGUAGES = {
'en': {'flag': 'us', 'name': 'English'},
'fr': {'flag': 'fr', 'name': 'French'},
'zh': {'flag': 'cn', 'name': 'Chinese'},
}

As per the [Flask AppBuilder documentation] about translation, to create a
new language dictionary, run the following command:

pybabel init -i ./babel/messages.pot -d caravel/translations -l es

Then it's a matter of running the statement bellow to gather all stings that
need translation

fabmanager babel-extract --target caravel/translations/

You can then translate the strings gathered in files located under
`caravel/translation`, where there's one per language. For the translations
to take effect, they need to be compiled using this command:

fabmanager babel-compile --target caravel/translations/
4 changes: 4 additions & 0 deletions babel/babel.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[ignore: caravel/assets/node_modules/**]
[python: caravel/**.py]
[jinja2: caravel/**/templates/**.html]
encoding = utf-8
117 changes: 117 additions & 0 deletions babel/messages.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Translations template for Caravel.
# Copyright (C) 2016 ORGANIZATION
# This file is distributed under the same license as the Caravel project.
# Maxime Beauchemin <maximebeauchemin @ gmail.com>, 2016.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-05-02 00:21-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.3.4\n"

#: caravel/models.py:564
msgid ""
"Datetime column not provided as part table configuration and is required "
"by this type of chart"
msgstr ""

#: caravel/models.py:1153
msgid "No data was returned."
msgstr ""

#: caravel/views.py:116
msgid ""
"Whether to make this column available as a [Time Granularity] option, "
"column has to be DATETIME or DATETIME-like"
msgstr ""

#: caravel/views.py:215
msgid "Databases"
msgstr ""

#: caravel/views.py:217 caravel/views.py:261 caravel/views.py:284
msgid "Sources"
msgstr ""

#: caravel/views.py:260
msgid "Tables"
msgstr ""

#: caravel/views.py:282
msgid "Druid Clusters"
msgstr ""

#: caravel/views.py:313
msgid "Slices"
msgstr ""

#: caravel/views.py:341
msgid ""
"This json object describes the positioning of the widgets in the "
"dashboard. It is dynamically generated when adjusting the widgets size "
"and positions by using drag & drop in the dashboard view"
msgstr ""

#: caravel/views.py:346
msgid ""
"The css for individual dashboards can be altered here, or in the "
"dashboard view where changes are immediately visible"
msgstr ""

#: caravel/views.py:367
msgid "Dashboards"
msgstr ""

#: caravel/views.py:392
msgid "Action Log"
msgstr ""

#: caravel/views.py:393
msgid "Security"
msgstr ""

#: caravel/views.py:430
msgid "Druid Datasources"
msgstr ""

#: caravel/views.py:514
msgid "The datasource seems to have been deleted"
msgstr ""

#: caravel/views.py:522
msgid "You don't seem to have access to this datasource"
msgstr ""

#: caravel/views.py:843
msgid "This view requires the `all_datasource_access` permission"
msgstr ""

#: caravel/views.py:954
msgid "CSS Templates"
msgstr ""

#: caravel/templates/appbuilder/navbar_right.html:34
msgid "Profile"
msgstr ""

#: caravel/templates/appbuilder/navbar_right.html:35
msgid "Logout"
msgstr ""

#: caravel/templates/appbuilder/navbar_right.html:40
msgid "Login"
msgstr ""

#: caravel/templates/caravel/welcome.html:8
#: caravel/templates/caravel/welcome.html:13
msgid "Welcome!"
msgstr ""

4 changes: 4 additions & 0 deletions caravel/assets/stylesheets/caravel.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ body {
margin: 0px !important;
}

.caret {
border-top: 4px solid;
}

.emph {
font-weight: bold;
}
Expand Down
4 changes: 3 additions & 1 deletion caravel/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@
# Setup default language
BABEL_DEFAULT_LOCALE = 'en'
# Your application default translation path
BABEL_DEFAULT_FOLDER = 'translations'
BABEL_DEFAULT_FOLDER = 'babel/translations'
# The allowed translation for you app
LANGUAGES = {
'en': {'flag': 'us', 'name': 'English'},
# 'fr': {'flag': 'fr', 'name': 'French'},
# 'zh': {'flag': 'cn', 'name': 'Chinese'},
}
# ---------------------------------------------------
# Image and file configuration
Expand Down
12 changes: 7 additions & 5 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import sqlalchemy as sqla
import sqlparse
from dateutil.parser import parse

from flask import request, g
from flask.ext.appbuilder import Model
from flask.ext.appbuilder.models.mixins import AuditMixin
from pydruid.client import PyDruid
from flask.ext.appbuilder.models.decorators import renders
from flask.ext.babelpkg import lazy_gettext as _

from pydruid.client import PyDruid
from pydruid.utils.filters import Dimension, Filter
from pydruid.utils.postaggregator import Postaggregator
from six import string_types
Expand All @@ -33,7 +36,6 @@
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import relationship
from sqlalchemy.sql import table, literal_column, text, column
from sqlalchemy.sql.elements import ColumnClause
from sqlalchemy_utils import EncryptedType

from caravel import app, db, get_session, utils
Expand Down Expand Up @@ -571,9 +573,9 @@ def query( # sqla
qry_start_dttm = datetime.now()

if not granularity and is_timeseries:
raise Exception(
raise Exception(_(
"Datetime column not provided as part table configuration "
"and is required by this type of chart")
"and is required by this type of chart"))

metrics_exprs = [
m.sqla_col
Expand Down Expand Up @@ -1191,7 +1193,7 @@ def query( # druid
query_str += json.dumps(client.query_dict, indent=2)
df = client.export_pandas()
if df is None or df.size == 0:
raise Exception("No data was returned.")
raise Exception(_("No data was returned."))

if (
not is_timeseries and
Expand Down
6 changes: 2 additions & 4 deletions caravel/templates/appbuilder/navbar_right.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@

{% macro locale_menu(languages) %}
{% set locale = session['locale'] %}
{% if not locale %}
{% set locale = 'en' %}
{% endif %}
{% if languages.keys()|length > 1 %}
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<div class="f16"><i class="flag {{languages[locale].get('flag')}}"></i><b class="caret"></b>
</div>
</a>
{% if languages.keys()|length > 1 %}
<ul class="dropdown-menu">
<li class="dropdown">
{% for lang in languages %}
Expand All @@ -21,9 +20,8 @@
{% endfor %}
</li>
</ul>
{% endif %}
</li>
{% endmacro %}
{% endif %}



Expand Down
1 change: 1 addition & 0 deletions caravel/templates/caravel/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{% block head_css %}
<link rel="stylesheet" type="text/css" href="/static/assets/node_modules/font-awesome/css/font-awesome.min.css" />
<link rel="stylesheet" type="text/css" href="/static/assets/stylesheets/caravel.css" />
<link rel="stylesheet" type="text/css" href="/static/appbuilder/css/flags/flags16.css" />
<link rel="icon" type="image/png" href="/static/assets/images/favicon.png">
{% endblock %}
{% block head_js %}
Expand Down
4 changes: 2 additions & 2 deletions caravel/templates/caravel/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<script src="/static/assets/javascripts/dist/welcome.entry.js"></script>
{% endblock %}

{% block title %}Welcome!{% endblock %}
{% block title %}{{ _("Welcome!") }}{% endblock %}

{% block body %}
<div class="container welcome">
<div class="header">
<h3>Welcome!</h3>
<h3>{{ _("Welcome!") }}</h3>
</div>
<hr/>
<div id="cal-heatmap"></div>
Expand Down
Loading

0 comments on commit 88c9516

Please sign in to comment.