Login

Admin Apps Names Translation

Author:
Nad
Posted:
January 22, 2010
Language:
Python
Version:
1.1
Score:
3 (after 3 ratings)

This Snippet allows for your project's apps names to be displayed as you want in the Admin, including translations.

The lists of apps and languages are created from your settings.py file.

How to use

1st part:

  • Create a application called 'apps_verbose' in you project with the models.py and admin.py showed here

  • Create a folder inside it with named 'templatetags' with the verbose_tags.py file inside it.

  • Add this app to installed apps in your settings.py

  • If you change this app name, dont forget to correct the imports.

2nd part:

  • Create a folder named 'admin' in your templates folder and copy the following files form your /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

  • Make the necessary changes in each file, like shown here.

3rd part:

  • Create translations in the Admin and enjoy.
  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
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
### 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)



### 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('.')
    i=len(path)-1
    return path[i]

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=False,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,)



### 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)


<!-- 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> &rsaquo; 
<!-- Changes made here -->
    <a href="../../../">{% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %}</a> &rsaquo; 
    <a href="../../">{{ module_name }}</a> &rsaquo; 
    <a href="../">{{ object|truncatewords:"18" }}</a> &rsaquo; 
    {% trans 'History' %}
</div>
{% endblock %}



<!-- 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> &rsaquo;
<!-- Changes made here -->
     <a href="../">{% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %}</a> &rsaquo; 
     <a href="./">{{ opts.verbose_name_plural|capfirst }}</a> &rsaquo;
     {% trans 'Delete multiple objects' %}
</div>
{% endblock %}



<!-- 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> &rsaquo;
<!-- Changes made here -->
     <a href="../">{% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %}</a> &rsaquo; 
     <a href="../../">{{ opts.verbose_name_plural|capfirst }}</a> &rsaquo;
     <a href="../">{{ object|truncatewords:"18" }}</a> &rsaquo;
     {% trans 'Delete' %}
</div>
{% endblock %}



<!-- 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> &rsaquo; 
{% 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 %}



<!-- 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> &rsaquo;
<!-- Changes made here -->
     <a href="../../">{% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %}</a> &rsaquo; 
     {% if has_change_permission %}<a href="../">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %} &rsaquo; 
     {% if add %}{% trans "Add" %} {{ opts.verbose_name }}{% else %}{{ original|truncatewords:"18" }}{% endif %}
</div>
{% endif %}{% endblock %}



<!-- 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>&nbsp;</td>
            {% endif %}

(...)


<!-- 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>
       &rsaquo; 
       <a href="../">
<!-- Changes made here -->
         {% get_current_language as LANGUAGE_CODE %}{% verbose_name app_label LANGUAGE_CODE %} 
      </a>
      &rsaquo; 
      {{ cl.opts.verbose_name_plural|capfirst }}
    </div>
  {% endblock %}
{% endif %}

(...)



<!-- 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 -->

(...)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

ha22109 (on April 1, 2010):

this is not working for me.I am not able to change the app name "apps_verbose" .It is still showing in english

#

Nad (on August 2, 2010):

There is an erros in models.py. Language shoud have unique=False.

Corrected in code.

There have been some changes to the get_app_name function for better results.

#

valerio.maggio (on February 15, 2014):

@ha22109: You have to change the LANGUAGE_CODE in your settings to make it work. Moreover, if you want to enable on-demand language switching, you have to enable i18n URLS too.

@Nad/: There's a Typo in the names of the models (Tranlation instead of TranSlation) Btw, thanks for sharing this snippet!-)

#

Please login first before commenting.