Repository: apluslms/a-plus

Issue:
In A+ it should be possible to set student's default language in a specific course
> **Migrated from Trello**

- [x] Add language to the userprofile - #617
- [x] When user is viewing material with different language, make it clear that it's not their default, e.g., show a warning. - #651 

    Additionally, buttons (links) should be shown to set that language as their preferred language (to enrollment object) and to switch back to their preferred language.

    This is more about the language set via url (query parameter), so that when the user is directed to a material in a different language than in their enrollment object (or profile), then that would be pointed out to them.

- [x] Store language in the Enrollment object. Think about the case where system has 3 languages sv, en, and fi, user has set the language in profile to sv and course is provided in en and fi. In that case, A+ should be able to remember the language the user likes to read that course and not force to set profile language. - #632

----
**Comments**

>**Jaakko Kantojärvi:**
At least for a profile, so when student logs in, the language is returned to original one.




Discussion:
The UserProfile already has a `lang` field: https://github.com/apluslms/a-plus/blob/d34f94b7c9a8e4d22cd28d08968b36787bb344c4/userprofile/models.py#L37-L38
Is it used anywhere? 

Like the issue says, when the student comes to a multilingual course, the course should be by default displayed in the language set in the userprofile. It would be good to check which language is now used in the old code. Is it random, or is it based on the HTTP language headers?

If the user views the course in other language than what is set in the profile, we need to careful about making too obnoxious warnings about it. If the user really wants to view the other language, then they should not be constantly bombarded with warnings about the language not being their default.

This issue can be done after https://github.com/apluslms/a-plus/issues/520 (different language URLs for multilingual courses).
> It would be good to check which language is now used in the old code. Is it random, or is it based on the HTTP language headers?

Django session (or actually a separate cookie) contains the language information. It's initialized with the HTTP language. I didn't remember we had DB reservation for the language, but I don't think it is used yet at all.

Current behavriour is that the user can have multiple sessions with different languages. We should replace this with the user linked language. The language selection in profile page would make more sense then.
> This issue can be done after #520 (different language URLs for multilingual courses).

I think this issue needs to be done before #520 because #520 affects how the language is selected, and now the language is stored in the wrong place (in a cookie). The lang field in UserProfile is not in use now, so it would make sense to fix this first.
> > This issue can be done after #520 (different language URLs for multilingual courses).
> 
> I think this issue needs to be done before #520 because #520 affects how the language is selected, and now the language is stored in the wrong place (in a cookie). The lang field in UserProfile is not in use now, so it would make sense to fix this first.

Alright, good point!

Resolving patch:
diff --git a/course/viewbase.py b/course/viewbase.py
--- a/course/viewbase.py
+++ b/course/viewbase.py
@@ -68,10 +68,14 @@ def get_resource_objects(self):
             self.is_teacher = self.course.is_teacher(user)
             self.is_course_staff = self.is_teacher or self.is_assistant
             self.get_taggings = lambda: CachedStudent(instance, user.id).data['tag_slugs']
+            self.url_without_language = remove_query_param_from_url(self.request.get_full_path(), 'hl')
+            self.query_language = None
+            self.user_language = None
 
             self.note(
                 "course", "instance", "content", "user_course_data", "is_student", "is_assistant",
-                "is_teacher", "is_course_staff", "get_taggings",
+                "is_teacher", "is_course_staff", "get_taggings", "url_without_language",
+                "query_language", "user_language"
             )
 
             # Try to find a language that is defined for this course instance
@@ -81,20 +85,24 @@ def get_resource_objects(self):
                 instance_def_language = instance_languages[0]
                 instance_languages = set(instance_languages)
 
+                languages = []
+                if self.user_course_data and self.user_course_data.language:
+                    languages.append(self.user_course_data.language)
+                if is_real_user and user.userprofile.language:
+                    languages.append(user.userprofile.language)
+                languages.append(get_language())
+
                 query_language = self.request.GET.get('hl')
                 if query_language:
                     if query_language[:2] in instance_languages:
                         language = query_language
+                        if languages:
+                            self.user_language = languages[0]
+                            if self.user_language[:2] != query_language[:2]:
+                                self.query_language = query_language
                     else:
                         raise TranslationNotFound
                 else:
-                    languages = []
-                    if self.user_course_data and self.user_course_data.language:
-                        languages.append(self.user_course_data.language)
-                    if is_real_user and user.userprofile.language:
-                        languages.append(user.userprofile.language)
-                    languages.append(get_language())
-
                     for lang in languages:
                         if lang[:2] in instance_languages:
                             language = lang


Test patch: