Skip to content
Snippets Groups Projects
Commit 832db291 authored by Fabrice Salvaire's avatar Fabrice Salvaire
Browse files

improved translation

parent 34c2f5d2
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,7 @@
<li><a href="{{ url('test.slider') }}">{{ _('Slider') }}</a></li>
<li><a href="{{ url('test.route-angularjs') }}">{{ _('Route AngularJS') }}</a></li>
<li><a href="{{ url('test.route-reactjs') }}">{{ _('Route ReactJS') }}</a></li>
<li><a href="{{ url('test.translation') }}">{{ _('Translation') }}</a></li>
</ul>
{% endblock %}
......
{# -*- mode: jinja2 -*- #}
{% extends "include/base.html" %}
{% block breadcrumb %}
{% endblock %}
{% block content %}
<h2>Language Information</h2>
{# <p>get_current_language : {% get_current_language as lang %} {{ lang }}</p> #}
{# <p>get_available_languages : {% get_available_languages as languages %} {{ languages }}</p> #}
<h3>Current Language</h3>
{% with language = get_language_info(get_current_language()) %}
<ul>
<li>Language code: {{ language.code }}</li>
<li>Name of language: {{ language.name_local }}</li>
<li>Name in English: {{ language.name }}</li>
<li>Bi-directional: {{ language.bidi }}</li>
<li>Name in the active language: {{ language.name_translated }}</li>
</ul>
{% endwith %}
<h2>Translation Test</h2>
<p>A translated message : {{ _('medical certificate year') }}</p>
<h3>Available Languages</h3>
<ul>
{% for language in get_available_languages() %}
<li>{{ language }}</li>
{% endfor %}
</ul>
{% endblock %}
####################################################################################################
#
# Climbing Asso Portal - A Portal for Climbing Club (Association)
# Copyright (C) 2018 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################
####################################################################################################
from django.conf import settings
from django.utils import translation
from jinja2 import lexer, nodes
from jinja2.ext import Extension
####################################################################################################
class GetCurrentLanguage(Extension):
tags = set(['get_current_language'])
##############################################
def parse(self, parser):
parser.stream.expect('name:get_current_language')
parser.stream.expect('name:as')
name = parser.stream.expect('name')
lineno = name.lineno
target = nodes.Name(name, 'store', lineno=lineno)
value = nodes.Const(self._get_current_language())
return nodes.Assign(target, value, lineno=lineno)
##############################################
def _get_current_language(self, *args):
return translation.get_language()
####################################################################################################
class GetAvailableLanguages(Extension):
tags = set(['get_available_languages'])
##############################################
def parse(self, parser):
parser.stream.expect('name:get_available_languages')
parser.stream.expect('name:as')
name = parser.stream.expect('name')
lineno = name.lineno
target = nodes.Name(name, 'store', lineno=lineno)
value = nodes.Const(self._get_available_languages())
return nodes.Assign(target, value, lineno=lineno)
##############################################
def _get_available_languages(self, *args):
return [(key, translation.gettext(value)) for key, value in settings.LANGUAGES]
####################################################################################################
class DjangoI18n(GetCurrentLanguage, GetAvailableLanguages):
"""Combines all extensions to one, so you don't have to put all of them in the django settings.
"""
_tag_class = {
'get_current_language': GetCurrentLanguage,
'get_available_languages': GetAvailableLanguages,
}
tags = set(_tag_class.keys())
##############################################
def parse(self, parser):
name = parser.stream.current.value
cls = self._tag_class.get(name)
if cls is None:
parser.fail("got unexpected tag '{}'".format(name)) # pragma: no cover
return cls.parse(self, parser)
####################################################################################################
#
# Climbing Asso Portal - A Portal for Climbing Club (Association)
# Copyright (C) 2018 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################
####################################################################################################
#
# https://github.com/MoritzS/jinja2-django-tags
#
####################################################################################################
####################################################################################################
from django.conf import settings
from django.utils import translation
from django_jinja import library
####################################################################################################
# from .myextensions import MyExtension
# library.extension(MyExtension)
####################################################################################################
@library.global_function
def get_current_language():
"""
Usage: {{ get_current_language() }}
"""
return translation.get_language()
####################################################################################################
@library.global_function
def get_available_languages():
"""
Usage: {{ get_available_languages() }}
"""
return [(key, translation.gettext(value)) for key, value in settings.LANGUAGES]
####################################################################################################
@library.global_function
def get_language_info(lang_code):
"""
Usage: {{ get_language_info(lang_code) }}
"""
return translation.get_language_info(lang_code)
####################################################################################################
# @library.global_function
# def get_language_info_list():
# def get_language_info(self, language):
# # ``language`` is either a language code string or a sequence
# # with the language code as its first item
# if len(language[0]) > 1:
# return translation.get_language_info(language[0])
# else:
# return translation.get_language_info(str(language))
# langs = self.languages.resolve(context)
# [self.get_language_info(lang) for lang in langs]
......@@ -278,4 +278,9 @@ urlpatterns += [
TemplateView.as_view(template_name='test/index-reactjs.html'),
name='test.route-reactjs',
),
path('test/translation',
TemplateView.as_view(template_name='test/translation.html'),
name='test.translation',
),
]
......@@ -30,6 +30,9 @@
# pot (Portable Object Template) : all the translation strings left empty
# mo (Message Object)
#
# https://github.com/python-babel/django-babel
#
#
####################################################################################################
__all__ = [
......@@ -53,12 +56,13 @@ class MakeMessage:
##############################################
def __init__(self, domain):
def __init__(self, application, domain='django'):
self._application = application
self._domain = domain
self._source_path = Path(__file__).parents[2].resolve()
self._locale_dir = self._source_path.joinpath(self._domain, 'locale')
self._locale_dir = self._source_path.joinpath(self._application, 'locale')
self._babel_cfg_path = self._locale_dir.joinpath('babel.cfg')
self._message_pot_path = self._locale_dir.joinpath(self._domain + '.pot')
......@@ -191,6 +195,6 @@ class MakeMessage:
messages = self._extract_js_for_language(po_file)
json_data[language] = messages
json_path = self._locale_dir.joinpath('messages.json')
json_path = self._locale_dir.joinpath(self._domain + '.json')
with open(json_path, 'w') as fh:
json.dump(json_data, fh)
......@@ -111,6 +111,7 @@ INSTALLED_APPS = [
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
......@@ -162,6 +163,8 @@ JINJA_TEMPLATES = {
'django_jinja.builtins.extensions.UrlsExtension',
'django_jinja.builtins.extensions.StaticFilesExtension',
'django_jinja.builtins.extensions.DjangoFiltersExtension',
'ClimbingAssoPortal.jinja_extensions.i18n.DjangoI18n',
],
'bytecode_cache': {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment