Search code examples
djangointernationalizationdjango-modeltranslation

Django modeltranslation: don't show items which don't have translations


I want to show only the items which have translations into a language, when this language is active. At the moment untranslated items are shown too. Is there such setting in django-modeltranslation or do I have to write something myself?


Solution

  • I've solved it myself:

    In translation.py:

    class MenuItemTranslationOptions(TranslationOptions):
    fields = ('title',)
    fallback_values = {
        'title': False,
    }
    

    In a template:

      {% if request.LANGUAGE_CODE == 'en' and item.title_en %}  #this checks if the item is translated into english          
    
            <li class="navig"><a href="{{ item.link }}">{{ item.title }} </a>
            </li>
    
            {% elif request.LANGUAGE_CODE == 'ru' %} #default language
    
               <li class="navig"><a href="{{ item.link }}">{{ item.title }} </a>
            </li>
    
            {% endif %}