from django import template
from django.urls import translate_url

register = template.Library()


@register.simple_tag(takes_context=True)
def switch_lang_url(context, lang_code):
    request = context.get('request')
    if not request:
        return '/' if lang_code == 'fr' else '/en/'
    current_path = request.get_full_path()
    translated = translate_url(current_path, lang_code)
    if translated == current_path:
        return '/' if lang_code == 'fr' else '/en/'
    return translated
