Permalink
Browse files

add i8n support (#8)

  • Loading branch information...
tomkralidis committed Oct 18, 2015
1 parent 6277104 commit c01905c4d54cc3db3730615b6ce735d4ac3384cf
@@ -33,7 +33,7 @@
from flask import (flash, Flask, g, jsonify, redirect,
render_template, request, url_for)
from flask.ext.babel import Babel, gettext
from flask.ext.login import (LoginManager, login_user, logout_user,
current_user, login_required)
@@ -45,6 +45,7 @@
import views
APP = Flask(__name__)
BABEL = Babel(APP)
APP.config.from_pyfile('config.py')
APP.config.from_pyfile('../instance/config.py')
APP.secret_key = APP.config['SECRET_KEY']
@@ -54,6 +55,17 @@
GHC_SITE_URL = APP.config['GHC_SITE_URL'].rstrip('/')
LANGUAGES = {
'en': 'English',
'fr': 'Francais'
}
@BABEL.localeselector
def get_locale():
# return request.accept_languages.best_match(LANGUAGES.keys())
return 'fr'
@LOGIN_MANAGER.user_loader
def load_user(identifier):
@@ -379,8 +391,11 @@ def get_resource_by_id(identifier):
def register():
"""register a new user"""
if not APP.config['GHC_SELF_REGISTER']:
flash('This site is not configured for self-registration. '
'Please contact %s ' % APP.config['GHC_ADMIN_EMAIL'], 'warning')
msg1 = gettext('This site is not configured for self-registration')
msg2 = gettext('Please contact')
flash('%s. ' % msg1,
'%s %s ' % (msg2, APP.config['GHC_ADMIN_EMAIL']),
'warning')
return redirect(url_for('home'))
if request.method == 'GET':
return render_template('register.html')
@@ -393,7 +408,8 @@ def register():
DB.session.rollback()
bad_column = err.message.split()[2]
bad_value = request.form[bad_column]
flash('%s %s already registered' % (bad_column, bad_value), 'danger')
msg = gettext('already registered')
flash('%s %s %s' % (bad_column, bad_value, msg), 'danger')
return redirect(url_for('register'))
return redirect(url_for('login'))
@@ -412,8 +428,8 @@ def add():
resource = Resource.query.filter_by(resource_type=resource_type,
url=url).first()
if resource is not None:
flash('service already registered (%s, %s)' % (resource_type, url),
'danger')
msg = gettext('Service already registered')
flash('%s (%s, %s)' % (msg, resource_type, url), 'danger')
if 'resource_type' in request.args:
rtype = request.args.get('resource_type')
return redirect(url_for('add',
@@ -435,7 +451,8 @@ def add():
DB.session.add(run_to_add)
try:
DB.session.commit()
flash('service registered (%s, %s)' % (resource_type, url), 'success')
msg = gettext('Service registered')
flash('%s (%s, %s)' % (msg, resource_type, url), 'success')
except Exception, err:
DB.session.rollback()
flash(str(err), 'danger')
@@ -470,16 +487,17 @@ def test(resource_identifier):
"""test a resource"""
resource = Resource.query.filter_by(identifier=resource_identifier).first()
if resource is None:
flash('resource not found', 'danger')
flash(gettext('Resource not found'), 'danger')
return redirect(request.referrer)
[title, success, response_time, message, start_time] = run_test_resource(
resource.resource_type, resource.url)
if message not in ['OK', None, 'None']:
flash('ERROR: %s' % message, 'danger')
msg = gettext('ERROR')
flash('%s: %s' % (msg, message), 'danger')
else:
flash('Resource tested successfully', 'success')
flash(gettext('Resource tested successfully'), 'success')
return redirect(url_for('get_resource_by_id',
identifier=resource_identifier))
@@ -491,11 +509,12 @@ def delete(resource_identifier):
"""delete a resource"""
resource = Resource.query.filter_by(identifier=resource_identifier).first()
if g.user.role != 'admin' and g.user.username != resource.owner.username:
flash('you do not have access to delete this resource', 'danger')
msg = gettext('You do not have access to delete this resource')
flash(msg, 'danger')
return redirect('/resource/%s' % resource_identifier)
if resource is None:
flash('resource not found', 'danger')
flash(gettext('Resource not found'), 'danger')
return redirect(url_for('home'))
runs = Run.query.filter_by(resource_identifier=resource_identifier).all()
@@ -507,7 +526,7 @@ def delete(resource_identifier):
try:
DB.session.commit()
flash('Resource deleted', 'success')
flash(gettext('Resource deleted'), 'success')
return redirect(url_for('home'))
except Exception, err:
DB.session.rollback()
@@ -525,7 +544,7 @@ def login():
registered_user = User.query.filter_by(username=username,
password=password).first()
if registered_user is None:
flash('invalid username and / or password', 'danger')
flash(gettext('Invalid username and / or password'), 'danger')
return redirect(url_for('login'))
login_user(registered_user)
@@ -538,7 +557,7 @@ def login():
def logout():
"""logout"""
logout_user()
flash('logged out', 'success')
flash(gettext('Logged out'), 'success')
if request.referrer:
return redirect(request.referrer)
else:
@@ -39,6 +39,7 @@
from owslib.csw import CatalogueServiceWeb
from owslib.sos import SensorObservationService
from flask.ext.babel import gettext
from enums import RESOURCE_TYPES
LOGGER = logging.getLogger(__name__)
@@ -48,9 +49,10 @@ def run_test_resource(resource_type, url):
"""tests a CSW service and provides run metrics"""
if resource_type not in RESOURCE_TYPES.keys():
msg = 'Invalid resource type: %s' % resource_type
LOGGER.error(msg)
raise RuntimeError(msg)
msg = gettext('Invalid resource type')
msg2 = '%s: %s' % (msg, resource_type)
LOGGER.error(msg2)
raise RuntimeError(msg2)
title = None
start_time = datetime.datetime.utcnow()
@@ -79,15 +81,15 @@ def run_test_resource(resource_type, url):
except:
title = url
elif resource_type == 'urn:geoss:waf':
title = 'WAF for %s' % urlparse(url).hostname
title = 'WAF %s %s' % (gettext('for'), urlparse(url).hostname)
elif resource_type == 'FTP':
ows = urlopen(url)
title = urlparse(url).hostname
success = True
if resource_type.startswith('OGC:'):
title = ows.identification.title
if title is None:
title = '%s for %s' % (resource_type, url)
title = '%s %s %s' % (resource_type, gettext('for'), url)
except Exception, err:
msg = str(err)
LOGGER.exception(msg)
@@ -30,6 +30,7 @@
import logging
import smtplib
from flask.ext.babel import gettext
from util import render_template2
LOGGER = logging.getLogger(__name__)
@@ -43,15 +44,15 @@ def notify(config, resource, run, last_run_success):
this_run_success = run.success
if last_run_success and not this_run_success:
result = 'Failing'
result = gettext('Failing')
elif not last_run_success and this_run_success:
result = 'Fixed'
result = gettext('Fixed')
elif not last_run_success and not this_run_success:
result = 'Still Failing'
result = gettext('Still Failing')
elif last_run_success and this_run_success:
result = 'Passing'
result = gettext('Passing')
if result != 'Passing':
if result != gettext('Passing'):
status_changed = True
if not status_changed:
@@ -2,10 +2,10 @@
{% block body %}
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-3 main">
<form role="form" action="" method="post">
<h2>Add Resource <i class="fa fa-plus fa-fw"></i></h2>
<h2>{{ _('Add Resource') }} <i class="fa fa-plus fa-fw"></i></h2>
<div class="form-group input-group col-sm-3">
<select class="form-control" name="resource_type">
<option value="none">Select Resource Type</option>
<option value="none">{{ _('Select Resource Type') }}</option>
{% for key, value in resource_types.iteritems() %}
{% if 'resource_type' in request.args and request.args['resource_type'] == key %}
{% set selected = 'selected' %}
@@ -19,10 +19,10 @@ <h2>Add Resource <i class="fa fa-plus fa-fw"></i></h2>
<div class="form-group input-group col-sm-5">
<input type="text" name="url" class="form-control" placeholder="url" required>
<p class="help-block">Enter the URL without any query parameters:<br/>good: <code>http://host/wms</code><br/>bad:<code>http://host/wms?service=WMS&version=1.1.1&request=GetCapabilities</code></p>
<p class="help-block">{{ _('Enter the URL without any query parameters') }}:<br/>{{ _('good') }}: <code>http://host/wms</code><br/>{{ _('bad') }}:<code>http://host/wms?service=WMS&version=1.1.1&request=GetCapabilities</code></p>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Submit</button>
<button type="submit" class="btn btn-success">{{ _('Submit') }}</button>
</div>
</form>
</div>
@@ -7,9 +7,9 @@
{% block body %}
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1 class="page-header">Dashboard</h1>
<h1 class="page-header">{{ _('Dashboard') }}</h1>
{% if response['first_run'].checked_datetime and response['last_run'].checked_datetime %}
<h3 class="page-header">Monitoring Period: {{ response['first_run'].checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }} - {{ response['last_run'].checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}</h3>
<h3 class="page-header">{{ _('Monitoring Period') }}: {{ response['first_run'].checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }} - {{ response['last_run'].checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}</h3>
{% endif %}
<div class="row col-md-offset-0">
<!--
@@ -22,13 +22,13 @@ <h3 class="page-header">Monitoring Period: {{ response['first_run'].checked_date
</div>
<div class="col-xs-9 text-right">
<div class="huge">{{ response['total'] }}</div>
<div>Resources</div>
<div>{{ _('Resources') }}</div>
</div>
</div>
</div>
<a href="#">
<div class="panel-footer">
<span class="pull-left"><a href="#all-resources">View Details</a></span>
<span class="pull-left"><a href="#all-resources">{{ _('View Details') }}</a></span>
<span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
<div class="clearfix"></div>
</div>
@@ -50,7 +50,7 @@ <h3 class="page-header">Monitoring Period: {{ response['first_run'].checked_date
</div>
<a href="#">
<div class="panel-footer">
<span class="pull-right">Operational ({{ response['success']['number'] }}/{{ response['total'] }})</span>
<span class="pull-right">{{ _('Operational') }} ({{ response['success']['number'] }}/{{ response['total'] }})</span>
<div class="clearfix"></div>
</div>
</a>
@@ -70,7 +70,7 @@ <h3 class="page-header">Monitoring Period: {{ response['first_run'].checked_date
</div>
<a href="#">
<div class="panel-footer">
<span class="pull-right">Reliable</span>
<span class="pull-right">{{ _('Reliable') }}</span>
<div class="clearfix"></div>
</div>
</a>
@@ -91,7 +91,7 @@ <h3 class="page-header">Monitoring Period: {{ response['first_run'].checked_date
</div>
<a href="#">
<div class="panel-footer">
<span class="pull-right">Broken ({{ response['fail']['number'] }}/{{ response['total'] }})</span>
<span class="pull-right">{{ _('Broken') }} ({{ response['fail']['number'] }}/{{ response['total'] }})</span>
<div class="clearfix"></div>
</div>
</a>
@@ -111,23 +111,23 @@ <h3 class="page-header">Monitoring Period: {{ response['first_run'].checked_date
</div>
-->
</div>
<h2 id="all-resources">Resources <a class="btn btn-default btn-xs" href="{{ url_for('json', q=request.args.get('q'), resource_type=request.args.get('resource_type')) }}"><span class="fa fa-download"></span> JSON</a> <a class="btn btn-default btn-xs" href="{{ url_for('csv', q=request.args.get('q'), resource_type=request.args.get('resource_type')) }}"><span class="fa fa-download"></span> CSV</a></h2>
<h2 id="all-resources">{{ _('Resources') }} <a class="btn btn-default btn-xs" href="{{ url_for('json', q=request.args.get('q'), resource_type=request.args.get('resource_type')) }}"><span class="fa fa-download"></span> JSON</a> <a class="btn btn-default btn-xs" href="{{ url_for('csv', q=request.args.get('q'), resource_type=request.args.get('resource_type')) }}"><span class="fa fa-download"></span> CSV</a></h2>
<div class="form-inline">
<input id="filter" type="text" class="form-control input-small placeholder="Search..."> (foo, site:.org, title:foo, type:wms, url:example.org)
<input id="filter" type="text" class="form-control input-small placeholder="{{ _('Search...') }}"> (foo, site:.org, title:foo, type:wms, url:example.org)
</div>
<span id="resources-table-num-results">{{ response['total'] }} results</span>
<span id="resources-table-num-results">{{ response['total'] }} {{ _('results') }}</span>
<div>
<table id="resources-table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Type</th>
<th>Name</th>
<th>Last Check</th>
<th>Status</th>
<th>Response Times (seconds)</th>
<th class="col-md-1">Reliability</th>
<th>{{ _('Type') }}</th>
<th>{{ _('Name') }}</th>
<th>{{ _('Last Check') }}</th>
<th>{{ _('Status') }}</th>
<th>{{ _('Response Times (seconds)') }}</th>
<th class="col-md-1">{{ _('Reliability') }}</th>
</tr>
</thead>
<tbody class="searchable">
@@ -138,7 +138,7 @@ <h2 id="all-resources">Resources <a class="btn btn-default btn-xs" href="{{ url_
<td>
<ul>
<li>{{ resource.last_run.checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}</li>
<li>{{ resource.last_run.response_time|round(2) }} seconds</li>
<li>{{ resource.last_run.response_time|round(2) }} {{ _('seconds') }}</li>
</ul>
</td>
{% if resource.last_run.success %}
@@ -38,7 +38,7 @@
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Add <i class="fa fa-plus"></i></a>
<a class="dropdown-toggle" data-toggle="dropdown" href="#">{{ _('Add') }} <i class="fa fa-plus"></i></a>
<ul class="dropdown-menu dropdown-messages">
{% for key, value in resource_types.iteritems() %}
<li><a href="{{ url_for('add', resource_type=key) }}">{{ value['label'] }}</a></li>
@@ -53,9 +53,9 @@
</ul>
</li>
{% else %}
<li><a href="{{ url_for('login', next=request.script_root+request.path) }}">Login<i class="fa fa-sign-in fa-fw"></i></a></li>
<li><a href="{{ url_for('login', next=request.script_root+request.path) }}">{{ _('Login') }}<i class="fa fa-sign-in fa-fw"></i></a></li>
{% endif %}
<li><a href="https://github.com/geopython/GeoHealthCheck">Help <i class="fa fa-question-circle fa-fw"></i></a></li>
<li><a href="https://github.com/geopython/GeoHealthCheck">{{ _('Help') }} <i class="fa fa-question-circle fa-fw"></i></a></li>
</ul>
</div>
</div>
@@ -65,7 +65,7 @@
<div class="col-sm-3 col-md-2 sidebar">
<ul class="nav nav-sidebar">
<li>
<i class="fa fa-filter fa-fw"></i><span>Resource Types ({{ resources_total }})</span>
<i class="fa fa-filter fa-fw"></i><span>{{ _('Resource Types') }} ({{ resources_total }})</span>
<ul class="nav nav-second-level">
{% for rt in resource_types_counts %}
{% if request.args.get('resource_type') == rt[0] %}
@@ -80,10 +80,10 @@
</ul>
<ul class="nav nav-sidebar">
<li>
<i class="fa fa-cog fa-fw"></i>Settings
<i class="fa fa-cog fa-fw"></i>{{ _('Settings') }}
<ul class="nav nav-second-level">
<li><a>History: {{ config['GHC_RETENTION_DAYS'] }} days</a></li>
<li><a>Test Frequency: {{ config['GHC_RUN_FREQUENCY'] }}</a></li>
<li><a>{{ _('History') }}: {{ config['GHC_RETENTION_DAYS'] }} {{ _('days') }}</a></li>
<li><a>{{ _('Test Frequency') }}: {{ config['GHC_RUN_FREQUENCY'] }}</a></li>
<li><a id="clockbox"></a></li>
</ul>
</li>
@@ -102,7 +102,7 @@
{% endblock %}
</div>
</div>
<footer><div class="footer text-center">Powered by <a href="http://geopython.github.io/GeoHealthCheck">GeoHealthCheck</a> {{ app_version }}</div></footer>
<footer><div class="footer text-center">{{ _('Powered by') }} <a href="http://geopython.github.io/GeoHealthCheck">GeoHealthCheck</a> {{ app_version }}</div></footer>
<script src="{{ url_for('static', filename='lib/bower_components/jquery/dist/jquery.min.js') }}"></script>
<script src="{{ url_for('static', filename='lib/bower_components/bootstrap/dist/js/bootstrap.min.js') }}"></script>
<script src="{{ url_for('static', filename='lib/bower_components/raphael/raphael-min.js') }}"></script>
Oops, something went wrong.

0 comments on commit c01905c

Please sign in to comment.