如何在Django中根据选中语言切换RTL/LTR布局及内联样式
It looks like the main issue here is that your template is checking the default language (from settings) instead of the current language the user has selected. Let's fix this step by step:
Step 1: Pass the Current Language to Your Template Context
In your views.py, you're calculating currentlang but not including it in the context dictionary. Update the view to send this value to the template so it can react to the user's selected language:
def index(request): if not request.session.has_key('currency'): request.session['currency'] = settings.DEFAULT_CURRENCY setting = Setting.objects.get(pk=1) # >>>>>>>>>>>>>>>>>>>>>> M U L T I L A N G U G A E >>>>>>>> START defaultlang = settings.LANGUAGE_CODE[0:2] currentlang = request.LANGUAGE_CODE[0:2] # Add currentlang to the context for template use context={'defaultlang': defaultlang, 'currentlang': currentlang} return render(request,'index.html',context)
Step 2: Update Template Conditionals to Use Current Language
Modify your page.html to check currentlang instead of defaultlang. This ensures the layout switches when the user selects Arabic (or any other RTL language):
<div id="top-header"> <div class="container"> <div class="{% if currentlang == 'en' %}pull-left {% else %} pull-right{% endif %}"> <span>{% trans "Welcome to E-shop!" %}</span> </div> <div class="{% if currentlang == 'en' %}pull-right {% else %} pull-left{% endif %}"> <span>GoodBye</span> </div> </div> </div>
Step 3: Dynamically Set the HTML Direction Attribute
To ensure the entire page's text direction updates automatically, add this to your base template's <html> tag (e.g., base.html):
<html lang="{{ currentlang }}" dir="{% if currentlang == 'ar' %}rtl{% else %}ltr{% endif %}">
Step 4: Verify Your Language Switching Mechanism
Double-check that your language switcher is correctly updating the user's language preference so request.LANGUAGE_CODE reflects the selected language. For example, if using Django's built-in set_language view, ensure your form submits the selected language and updates the user's session/cookie properly.
Bonus: Support Multiple RTL Languages
If you plan to add more RTL languages later, define a list in settings.py:
RTL_LANGUAGES = ['ar', 'fa', 'he'] # Add any other RTL language codes here
Then update your template conditionals to check if currentlang is in this list (you'll need to pass settings to the context or create a template filter):
<!-- Using a template filter (recommended) --> {% load language_tags %} <div class="{% if currentlang|is_rtl %}pull-right {% else %} pull-left{% endif %}"> <!-- Or if passing settings directly to context --> <div class="{% if currentlang in settings.RTL_LANGUAGES %}pull-right {% else %} pull-left{% endif %}">
To create the is_rtl template filter, add this to your app's templatetags directory:
# templatetags/language_tags.py from django import template from django.conf import settings register = template.Library() @register.filter def is_rtl(lang_code): return lang_code in getattr(settings, 'RTL_LANGUAGES', [])
These changes will ensure your layout correctly switches between LTR and RTL when the user selects Arabic (or any other RTL language).
内容的提问来源于stack exchange,提问作者coderboy

