1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | ### apps_verbose/templatetags/verbose_tags.py ###
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from django import template
from django.template.defaultfilters import *
from my_project.apps_verbose.models import * ### define import for your project (my_project) ###
register = template.Library()
@register.simple_tag
def verbose_name(app_name, language_code):
try:
app = Verbose_Name.objects.get(app = app_name.lower())
app_name_translated = app.verbose_name_tranlations_set.get(language = language_code)
return app_name_translated.name
except:
return app_name
@register.simple_tag
def verbose_name_title(title, language_code):
title = title.split(' ')
title_translated = []
for word in title:
title_translated.append(verbose_name(word, language_code))
return ' '.join(title_translated)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | ### apps_verbose/models.py ###
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from django.db import models
from django.conf import settings
from django.db.models import get_app, get_apps
def get_app_name (path):
path = path.split('.')
return path[1]
app_list = ((get_app_name(app),get_app_name(app))for app in settings.INSTALLED_APPS if get_app_name(app))
class Verbose_Name(models.Model):
"""
Apps
"""
class Meta:
verbose_name = u'Verbose_Name'
verbose_name_plural = u'Verbose_Name'
app = models.CharField(max_length=200,unique=True,choices=app_list)
def __unicode__(self):
return u'%s' % (self.app)
class Verbose_Name_Tranlations(models.Model):
"""
Translations
"""
class Meta:
verbose_name = u'Verbose_Name Tranlations'
verbose_name_plural = u'Verbose_Name Tranlations'
unique_together = ('name','language')
verbose_name = models.ForeignKey(Verbose_Name)
language = models.CharField(max_length=20,unique=True,choices = settings.LANGUAGES)
name = models.CharField(verbose_name = u'Name', max_length = 200, null = False, blank = False,)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | ### apps_verbose/admin.py ###
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from django.contrib import admin
from django.contrib.admin.options import ModelAdmin
from my_project.apps_verbose.models import * ### define import for your project (my_project) ###
class Child_Verbose_Name_Tranlations(admin.TabularInline):
model = Verbose_Name_Tranlations
extra = 3
class Admin_Verbose_Name(ModelAdmin):
fieldsets = (
(u'Verbose Name',
{'fields':('app',)}),
)
inlines = [Child_Verbose_Name_Tranlations,]
list_display = ('__unicode__',)
list_display_links = ('__unicode__',)
admin.site.register(Verbose_Name,Admin_Verbose_Name)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!-- templates/admin/object_history.html -->
{% extends "admin/base_site.html" %}
{% load i18n %}
{% load verbose_tags %} <!-- Add Tag Library here -->
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="../../../../">{% trans 'Home' %}</a> ›
<!-- Changes made here -->
<a href="../../../">{% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %}</a> ›
<a href="../../">{{ module_name }}</a> ›
<a href="../">{{ object|truncatewords:"18" }}</a> ›
{% trans 'History' %}
</div>
{% endblock %}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!-- templates/admin/delete_selected_confirmation.html -->
{% extends "admin/base_site.html" %}
{% load i18n %}
{% load verbose_tags %} <!-- Add Tag Library here -->
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="../../">{% trans "Home" %}</a> ›
<!-- Changes made here -->
<a href="../">{% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %}</a> ›
<a href="./">{{ opts.verbose_name_plural|capfirst }}</a> ›
{% trans 'Delete multiple objects' %}
</div>
{% endblock %}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!-- templates/admin/delete_confirmation.html -->
{% extends "admin/base_site.html" %}
{% load i18n %}
{% load verbose_tags %} <!-- Add Tag Library here -->
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="../../../../">{% trans "Home" %}</a> ›
<!-- Changes made here -->
<a href="../">{% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %}</a> ›
<a href="../../">{{ opts.verbose_name_plural|capfirst }}</a> ›
<a href="../">{{ object|truncatewords:"18" }}</a> ›
{% trans 'Delete' %}
</div>
{% endblock %}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!-- templates/admin/app_index.html -->
{% extends "admin/index.html" %}
{% load i18n %}
{% load verbose_tags %} <!-- Add Tag Library here -->
{% if not is_popup %}
{% block breadcrumbs %}
<div class="breadcrumbs"><a href="../">
{% trans "Home" %}</a> ›
{% for app in app_list %}
<!-- Changes made here -->
{% get_current_language as LANGUAGE_CODE %}{% verbose_name app.name LANGUAGE_CODE %}
{% endfor %}</div>{% endblock %}
{% endif %}
{% block sidebar %}{% endblock %}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!-- templates/admin/change_form.html -->
{% extends "admin/base_site.html" %}
{% load i18n admin_modify adminmedia %}
{% load verbose_tags %} <!-- Add Tag Library here -->
{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="../../../jsi18n/"></script>
{{ media }}
{% endblock %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% admin_media_prefix %}css/forms.css" />{% endblock %}
{% block coltype %}{% if ordered_objects %}colMS{% else %}colM{% endif %}{% endblock %}
{% block bodyclass %}{{ opts.app_label }}-{{ opts.object_name.lower }} change-form{% endblock %}
{% block breadcrumbs %}{% if not is_popup %}
<div class="breadcrumbs">
<a href="../../../">{% trans "Home" %}</a> ›
<!-- Changes made here -->
<a href="../../">{% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %}</a> ›
{% if has_change_permission %}<a href="../">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %} ›
{% if add %}{% trans "Add" %} {{ opts.verbose_name }}{% else %}{{ original|truncatewords:"18" }}{% endif %}
</div>
{% endif %}{% endblock %}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <!-- templates/admin/index.html -->
{% extends "admin/base_site.html" %}
{% load i18n %}
{% load verbose_tags %} <!-- Add Tag Library here -->
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% load adminmedia %}{% admin_media_prefix %}css/dashboard.css" />{% endblock %}
{% block coltype %}colMS{% endblock %}
{% block bodyclass %}dashboard{% endblock %}
{% block breadcrumbs %}{% endblock %}
{% block content %}
<div id="content-main">
{% if app_list %}
{% for app in app_list %}
<div class="module">
<table summary="{% blocktrans with app.name as name %}Models available in the {{ name }} application.{% endblocktrans %}">
<!-- Changes made here -->
<caption><a href="{{ app.app_url }}" class="section">{% get_current_language as LANGUAGE_CODE %}{% verbose_name app.name LANGUAGE_CODE %}</a></caption>
{% for model in app.models %}
<tr>
{% if model.perms.change %}
<th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
{% else %}
<th scope="row">{{ model.name }}</th>
{% endif %}
{% if model.perms.add %}
<td><a href="{{ model.admin_url }}add/" class="addlink">{% trans 'Add' %}</a></td>
{% else %}
<td> </td>
{% endif %}
(...)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <!-- templates/admin/change_list.html -->
{% extends "admin/base_site.html" %}
{% load adminmedia admin_list i18n %}
{% load verbose_tags %} <!-- Add Tag Library here -->
{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% admin_media_prefix %}css/changelists.css" />
{% if cl.formset %}
<link rel="stylesheet" type="text/css" href="{% admin_media_prefix %}css/forms.css" />
<script type="text/javascript" src="../../jsi18n/"></script>
{% endif %}
{{ media }}
{% if not actions_on_top and not actions_on_bottom %}
<style>
#changelist table thead th:first-child {width: inherit}
</style>
{% endif %}
{% endblock %}
{% block bodyclass %}change-list{% endblock %}
{% if not is_popup %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="../../">
{% trans "Home" %}
</a>
›
<a href="../">
<!-- Changes made here -->
{% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %}
</a>
›
{{ cl.opts.verbose_name_plural|capfirst }}
</div>
{% endblock %}
{% endif %}
(...)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!-- templates/admin/base.html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="{{ LANGUAGE_CODE }}" xml:lang="{{ LANGUAGE_CODE }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/base.css{% endblock %}" />
{% block extrastyle %}{% endblock %}
<!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="{% block stylesheet_ie %}{% load adminmedia %}{% admin_media_prefix %}css/ie.css{% endblock %}" /><![endif]-->
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% admin_media_prefix %}css/rtl.css{% endblock %}" />{% endif %}
{% block extrahead %}{% endblock %}
{% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE" />{% endblock %}
</head>
{% load i18n %}
{% load verbose_tags %} <!-- Add Tag Library here -->
<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}">
<!-- Container -->
(...)
<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
{% block pretitle %}{% endblock %}
<!-- Changes made here -->
{% block content_title %}{% if title %}<h1>{% get_current_language as LANGUAGE_CODE %}{% verbose_name_title title LANGUAGE_CODE %}</h1>{% endif %}{% endblock %}
{% block content %}
{% block object-tools %}{% endblock %}
{{ content }}
{% endblock %}
{% block sidebar %}{% endblock %}
<br class="clear" />
</div>
<!-- END Content -->
(...)
|
Este snippet permite que os nomes das aplicaçãos sejam modificaveis na forma como aparecem no Admin, incluindo traduções
As listas de aplicações e idiomas são retiradas do ficheiro settings.py.
Como Usar:
1ª parte:
Criar uma aplicação chamada 'apps_verbose' no projeto com o models.py e admin.py mostrados aqui.
Criar uma pasta dentro da aplicação com o nome 'templatetags' com o fihceiro verbose_tags.py dentro dela.
Adicionar esta aplicação ao settings.py
Se mudar o nome da aplicação, não esquecer de corrigir os imports
2ª parte:
Criar uma pasta denominada 'admin' na pasta de templates e copiar os seguintes ficheiros da pasta /django/contrib/admin/templates/admin/ folder.
/app_index.html
/base.html
/change_form.html
/change_list.html
/delete_confirmation.html
/delete_selected_confirmation.html
/index.html
/object_history.html
Fazer as alterações necessárias em cada ficheiro como mostrado aqui.
3ª parte:
Criar as Traduções no Admin e desfrutar.
