django-hvad icon indicating copy to clipboard operation
django-hvad copied to clipboard

Custom language tabs in Admin

Open alvarovelezgalvez opened this issue 7 years ago • 4 comments

First of all thanks for the project!

We had the need in our project of showing a custom list of languages in admin translation tabs different from the list which is used by default, from settings.LANGUAGES. Our use case is that we have our website in 4 languages but we allow our users to add some texts in any language they find appropriate not only from those 4 languages.

We have solved it easily by overriding the get_language_tabs method and adding a "custom_languages" parameter to iterate through it

def get_language_tabs_custom(self, obj, request, available_languages, custom_languages):
    info = None if obj is None else (obj._meta.app_label, obj._meta.model_name)
    tabs = []
    get = request.GET.copy()
    language = self._language(request)
    for language in custom_languages:
        key =  language.language
        name = language.__unicode__
        get['language'] = key
        url = '%s?%s' % (request.path, get.urlencode())
        if language == key:
            status = 'current'
        elif key in available_languages:
            status = 'available'
        else:
            status = 'empty'
        del_url = (reverse('admin:%s_%s_delete_translation' % info, args=(obj.pk, key))
                   if obj is not None and key in available_languages else None)
        tabs.append((url, name, key, status, del_url))
    return tabs

then in your Admin model, you only have to add this parameter with the list of languages you prefer to show (in this example we add the languages chosen by the user, stored in his profile):

    def get_language_tabs(self, obj, request, available_languages):
        return get_language_tabs_custom(self, obj, request, available_languages, obj.user.profile.languages.all())

alvarovelezgalvez avatar Nov 21 '17 10:11 alvarovelezgalvez

Hello, Glad to hear you found a solution to your issue!

I was wondering as well, whether setting the LANGUAGES key in settings.HVAD dictionary would have worked as well? This setting defaults to be the same as settings.LANGUAGES, but it can be set manually to override the languages just for hvad. It has more implications than the admin though, it also means .fallbacks() will include all those languages as well. Did you have a chance to try that approach?

spectras avatar Nov 21 '17 17:11 spectras

Hi!

Maybe I didn't understood this solution well, but setting up a settings.HVAD will modify the option languages to all the models? Good thing for the solution we found is that we only need to modify the languages of some Admin models, not in the entire application... (or maybe i didn't understood it properly!)

Thanks for the reply!

alvarovelezgalvez avatar Nov 22 '17 08:11 alvarovelezgalvez

Yes it would be for all models. If you only want this for a few select models, then I believe your approach it the best way. Thanks for taking the time to report here :)

spectras avatar Nov 22 '17 12:11 spectras

Thank to you for the comments!

alvarovelezgalvez avatar Nov 23 '17 16:11 alvarovelezgalvez