-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8228c6
commit 9434644
Showing
84 changed files
with
575 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[run] | ||
source=django_translation_flags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include *.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
2 changes: 1 addition & 1 deletion
2
css/django-internationalization.css → ...s/static/css/django-translation-flags.css
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Oops, something went wrong.