Skip to content

Commit

Permalink
Django Translation Flags done
Browse files Browse the repository at this point in the history
  • Loading branch information
silviolleite committed Nov 29, 2018
1 parent d8228c6 commit 9434644
Show file tree
Hide file tree
Showing 84 changed files with 575 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
source=django_translation_flags
25 changes: 24 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
.idea/
.venv/

# npm
node_modules/
package-lock.json
package-lock.json


# Compiled python modules.
*.pyc

# Setuptools distribution folder.
/dist/
/build/

# Python egg metadata, regenerated from source files by setuptools.
/*.egg-info
/*.egg


*.py[cod]
*.orig
build/
dist/
*.egg-info/
.tox/
.coverage
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: python
python:
- "3.6"
env:
- DJANGO_VERSION=2.0
install:
- pip install Django==$DJANGO_VERSION
- pip install tox-travis
- pip install -q -r requirements.txt
- pip install coverage
- pip install codecov
script:
- coverage run runtests.py
after_success:
- codecov
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License (MIT)

Copyright (c) 2018 Silvio Luis Leite

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include *.md
153 changes: 153 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Django Translation Flags

This Django app provides integration for translation options in templates with some most common standard world languages. This is useful fow when you need to display language options in yours Django Apps.


#### 1. Requirements
Django Translation Flags require Django Internationalization and localization properly configured. You can see more about theses settings in [https://docs.djangoproject.com/en/2.1/topics/i18n/](https://docs.djangoproject.com/en/2.1/topics/i18n/)

Basically you need to:

1. Define a custom `LANGUAGES` list on `settings.py` with tuples, i.e:

```python
from django.utils.translation import gettext as _

LANGUAGES = [
('de', _('German')),
('en', _('English')),
('pt-br', _('Brazilian Portuguese'))
]
```
Only languages listed in the `LANGUAGES` setting can be selected.
This example restricts languages that are available for automatic selection to German, English and Brazilian Portuguese

2. Markup the text to translation:

The format of `.po` files is straightforward. Each `.po` file contains a small bit of metadata, such as the translation maintainer’s contact information, but the bulk of the file is a list of messages – simple mappings between translation strings and the actual translated text for the particular language.

For instance, if your Django app contained a translation string for the text "Welcome to my site.", like so:

```python
from django.utils.translation import gettext_lazy as _
_("Welcome to my site.")

```
...then `django-admin makemessages` will have created a `.po` file containing the following snippet – a message:

```text
#: path/to/python/module.py:23
msgid "Welcome to my site."
msgstr ""
```

3. Generate and compile it using the commands bellow:

- The first step is to create a message file for a new language:
```bash
django-admin makemessages -l de -l en -l pt_BR
```
- Compiling message files after creating your message file:

```bash
django-admin compilemessages
```

For more detailed information on how to create language files it is suggested to read the documentation: [https://docs.djangoproject.com/en/2.1/topics/i18n/translation/#how-to-create-language-files](https://docs.djangoproject.com/en/2.1/topics/i18n/translation/#how-to-create-language-files)

#### 1. Install
Install from PyPI:

```
pip install django-translation-flags
```

Configuration
=====
Add `django-translation-flags` to your list of `INSTALLED_APPS` in settings.py:

```python
INSTALLED_APPS = [
...
'django-translation-flags',
...
]
```
Add the Django Translation Flags URLs to `urls.py`:
```python
from django.conf.urls import url, include

urlpatterns = [
...
path('i18n/', include('django-translation-flags.urls')),
...
]
```

Inject the required meta tags in your `base.html` (or wherever your HTML <head> is defined):
```html
{% load flags %}

<head>
<ul>
{% languages %}
</ul>
</head>
```
By default it will show the rectangular icons, but you can change it to `square`:
```html
{% load flags %}

<head>
<ul>
{% languages 'square' %}
</ul>
</head>
```

Optionally you can set your custom class for HTML tags:
```html
{% load flags %}

<head>
<ul>
{% languages 'square' li_class='your-li-class' a_class='your-a-class' %}
</ul>
</head>
```

The `languages` template tags accept `**kwargs` to configure the HTML elements.

The HTML structure is:

```html
<li>
<a>
<span></span>
</a>
</li>
```

So you can set the classes to these HTML tags:

**li_class**: Class to `li` tag (Default: empty)

**a_class**: Class to `a` tag (Default: empty)


How does it work?
=====
The Django Translation Flags has a `CSS` file where all the most important languages flags are configured. The avaliable flags are:

`af`: Afrikaans, `ar`: Arabic, `az`: Azerbaijani, `de`: German, `en`: English, `en-au`: Australian English, `es`: Spanish, `es-ar`: Argentinian Spanish, `es-mx`: Mexican Spanish, `fr`: French, `hi`: Hindi, `hu`: Hungarian, `id`: Indonesian, `it`: Italian, `ja`: Japanese, `ko`: Korean, `nl`: Dutch (Nederlands), `pl`: Polish, `pt`: Portuguese, `pt-br`: Brazilian Portuguese, `ru`: Russian, `sv`: Swedish, `tr`: Turkish, `uk`: Ukrainian, `zh-cn`: Simplified Chinese, `zh-hans`: Simplified Chinese and `zh-hant`: Traditional Chinese.

The App get the language code from `LANGUAGES` on `settings.py` and then it makes the magic happen.

Feedback
=====
Feedback and pull requests are strongly encouraged and kindly appreciated (-:

Licensing
=====
All files in this repository are distributed under the MIT license.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
5 changes: 5 additions & 0 deletions django_translation_flags/app_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
""" Settings required by django-translation-flags. """

MIDDLEWARE = [
'django.middleware.locale.LocaleMiddleware',
]
5 changes: 5 additions & 0 deletions django_translation_flags/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class DjangoTranslationFlagsConfig(AppConfig):
name = 'django_translation_flags'

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
32 changes: 32 additions & 0 deletions django_translation_flags/templates/languages.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% load static %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info for LANGUAGE_CODE as lang %}
{% get_language_info_list for LANGUAGES as languages %}

<link rel="stylesheet" type="text/css" href="{% static 'css/django-translation-flags.min.css' %}">

{% for language in languages %}
<li {% if classes.li_class != '' %} class="{{ classes.li_class }}" {% endif %}>
<a tabindex="-1" href="#{{ language.code }}"
onclick="set_language('{{ language.code }}')"
{% if classes.a_class != '' %} class="{{ classes.a_class }}" {% endif %}
title="{{ language.name_local }}">
<span class="flag-icon flag-icon-{{ language.code }} {{ icon_class }}"></span>
</a>
</li>
{% endfor %}

<form action="{% url 'set_language' %}" method="post" id="setlang">
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}">
<input type="hidden" name="language">
</form>

<script type="text/javascript">
function set_language(language) {
$('input[name="language"]').val(language);
$('form#setlang').submit();
}
</script>
Empty file.
22 changes: 22 additions & 0 deletions django_translation_flags/templatetags/flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django import template

register = template.Library()


@register.inclusion_tag('languages.html')
def languages(flag_type='', **kwargs):
"""
Templatetag languages
:param flag_type: Default empty, It acepts the string 'square'
:param kwargs: Classes to HTML tags
:return: A dict with classes
"""
if flag_type == 'square':
flag_type = 'flag-icon-square'
default = dict(li_class='', a_class='')
classes = dict(default, **kwargs)
return {
'icon_class': flag_type,
'classes': classes,
}
5 changes: 5 additions & 0 deletions django_translation_flags/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.urls import path, include

urlpatterns = [
path('', include('django.conf.urls.i18n')),
]
12 changes: 6 additions & 6 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ var runSequence = require('run-sequence');


gulp.task('less', function () {
return gulp.src('./less/django-internationalization.less')
return gulp.src('./assets/less/django-translation-flags.less')
.pipe(sourcemaps.init())
.pipe(less())
.pipe(sourcemaps.write())
.pipe(gulp.dest('./css'));
.pipe(gulp.dest('./django-translation-flags/static/css'));
});


gulp.task('minify-css', function() {
return gulp.src(['./css/django-internationalization.css'])
return gulp.src(['./django-translation-flags/static/css/django-translation-flags.css'])
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./css'))
.pipe(gulp.dest('./django-translation-flags/static/css'))
});

gulp.task('watch', function () {
gulp.watch('./less/*.less', ['less']);
gulp.watch('./css/django-internationalization.css', ['minify-css']);
gulp.watch('./assets/less/*.less', ['less']);
gulp.watch('./django-translation-flags/static/css/django-translation-flags.css', ['minify-css']);
});

gulp.task('dev', function(){
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "django-internationalization",
"name": "django-translation-flags",
"version": "1.0.0",
"description": "Generate the css and min.css to Django languages Icons",
"description": "Generator the css and min.css to Django Translation Flags",
"main": "index.js",
"scripts": {
"build": "gulp dev"
},
"repository": {
"type": "git",
"url": "https://github.com/silviolleite/django-internationalization"
"url": "https://github.com/silviolleite/django-translation-flags"
},
"author": "silviolleite",
"license": "MIT",
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Django==2.1.3
tox==3.2.1
pypandoc==1.3.3
14 changes: 14 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import sys

import django
from django.conf import settings
from django.test.utils import get_runner

if __name__ == "__main__":
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(["tests"])
sys.exit(bool(failures))
Loading

0 comments on commit 9434644

Please sign in to comment.