## Language-selection widget for the footer.
##
## Requires settings.LANGUAGE_COOKIE.
<%page expression_filter="h"/>
<%!
  from babel import Locale
  from django.conf import settings
  from django.utils.translation import ugettext as _

  from openedx.core.djangoapps.lang_pref import COOKIE_DURATION
  from openedx.core.djangoapps.lang_pref.api import released_languages
  from openedx.core.djangolib.js_utils import js_escaped_string

  # Make sure LANGUAGE_COOKIE is present.
  if not settings.LANGUAGE_COOKIE:
      raise ValueError('settings.LANGUAGE_COOKIE is required to use footer-language-selector.')
%>
<%namespace name='static' file='../static_content.html'/>

<div class="footer-language-selector">
    <label for="footer-language-select">
        <span class="icon fa fa-globe" aria-hidden="true"></span>
        <span class="sr">${_("Choose Language")}</span>
    </label>
    <select id="footer-language-select" name="language" onchange="footerLanguageSelector.handleSelection(this)">
        % for language in sorted(released_languages(), key=lambda x: x.code):
            <% language_name = Locale.parse(language.code.replace('_', '-'), sep='-').language_name %>
            % if language.code == LANGUAGE_CODE:
                <option value="${language.code}" selected="selected">${language_name}</option>
            % else:
                <option value="${language.code}">${language_name}</option>
            % endif
        % endfor
    </select>
</div>

<script type="text/javascript">
    window.footerLanguageSelector = {
        ##
        ## Set the language cookie using the same settings as
        ## https://github.com/edx/edx-platform/blob/master/openedx/core/djangoapps/lang_pref/views.py#L26
        ## and
        ## https://github.com/edx/edx-platform/blob/master/openedx/core/djangoapps/lang_pref/middleware.py#63
        ## Then refresh the page so that the language negotiation middleware can read it and re-render everything
        ## in the selected language.
        ##
        ## NOTE: For logged-in users, the LMS language negotiation middleware should persist the selected language
        ## preference to the user's profile. This effect may be delayed on pages that do not use the LMS language
        ## selection middleware.
        ##
        handleSelection: function($select) {
            this.setLanguageCookie($select.value, this.refreshPage);
        },

        setLanguageCookie: function(value, callback) {
            var cookie = '${settings.LANGUAGE_COOKIE | n, js_escaped_string}=' + value + ';path=/';

            <% session_cookie_domain = static.get_value('SESSION_COOKIE_DOMAIN', settings.SESSION_COOKIE_DOMAIN) %>
            % if session_cookie_domain:
                cookie += ';domain=${session_cookie_domain | n, js_escaped_string}';
            % endif
            % if COOKIE_DURATION:
                cookie += ';max-age=${COOKIE_DURATION | n, js_escaped_string}';
            % endif

            document.cookie = cookie;

            callback();
        },

        refreshPage: function() {
            window.location.reload();
        }
    };
</script>
