Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3551: Manually set cookie value during language change #3622

Merged
merged 5 commits into from
Aug 3, 2017

Conversation

muxout
Copy link
Contributor

@muxout muxout commented Jul 5, 2017

When a new language is selected the language code is stored in a cookie. But when the same piece of code is invoked from the library page the value in the cookie doesn't change.
This seem to fix the issue but I haven't quite figured out why. Maybe someone familiar with Angular should have a look.

@seanlip
Copy link
Member

seanlip commented Jul 5, 2017

@kevinlee12 -- do you remember why you introduced the base tag in this commit? 2b8e97b

@codecov-io
Copy link

codecov-io commented Jul 5, 2017

Codecov Report

Merging #3622 into develop will decrease coverage by <.01%.
The diff coverage is 0%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3622      +/-   ##
===========================================
- Coverage    45.03%   45.03%   -0.01%     
===========================================
  Files          261      261              
  Lines        20219    20220       +1     
  Branches      3157     3157              
===========================================
  Hits          9106     9106              
- Misses       11113    11114       +1
Impacted Files Coverage Δ
core/templates/dev/head/i18n.js 52.63% <0%> (-1.43%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bc31014...26e7b10. Read the comment docs.

@kevinlee12
Copy link
Contributor

Ah, that was needed so that we can manipulate the history for the search function (since we're adding browser history entries without reloading the page). More specifically, it was changing the history as the learner was typing in queries into the search bar in the library. The tag itself was to segregate the search page into it's own "app" so that this would work.

@muxout
Copy link
Contributor Author

muxout commented Jul 20, 2017

After further investigation, I found that cookie path is affected by the base url: https://docs.angularjs.org/api/ngCookies/provider/$cookiesProvider#defaults
This explains the behavior on the Library page. As a workaround, maybe we could set the cookie value manually.

@seanlip
Copy link
Member

seanlip commented Jul 20, 2017

Thanks, @muxout. That's a great find, and I think it explains some of the existing behaviour!

It seems like that's a global default, though. I wonder whether it may be better to investigate the alternative instead, which is to make the library/search page function well without needing to overwrite the base tag. What do you think?

@anthkris
Copy link
Contributor

anthkris commented Aug 1, 2017

@muxout How is this going? Did you see Sean's comment above?

@muxout
Copy link
Contributor Author

muxout commented Aug 3, 2017

@anthkris I have yet to find a solution to the approach that @seanlip suggested.
I have pushed a commit that sets the cookie value manually.

@@ -90,6 +90,11 @@ oppia.controller('I18nFooter', [
$http.put(siteLanguageUrl, {
site_language_code: $scope.currentLanguageCode
});
} else {
// This is a workaround for the library page as the base url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach is reasonable, actually. But I'm a little confused about when it's applied. From the conditional, it seems to be applied when the user is not logged in (as opposed to when the user is on the library page)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue only affects users who are not logged in, as per #3551. I thought this might be the easiest workaround.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, now I get it!

Yep, makes sense. My understanding is that sometimes $translate sets this cookie automatically, but we are setting it manually here in all cases for safety's sake. Is that right? If so, that makes sense to me.

Can we explain this a bit more in the comment so that future devs have context? At the moment it reads like "this is something to be applied only in the library page", which is why I asked the question I did.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that's what I meant. Agreed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I meant something a bit different. How about:

// $translate.use() sets a cookie for the translation language, but does so using the
// page's base URL as the cookie path. However, the base URL is modified in pages
// like /library, thus causing the cookie path to change; in such cases, the user's
// preferences are not picked up by other pages. To avoid this, we manually set
// the cookie using the '/' path each time a non-logged-in user changes their
// site language. 

I think this provides the full context (i.e. a bit more info on the cookie path and what's really going on here) that would be useful to devs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it's much clearer.

Copy link
Member

@seanlip seanlip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few small comments. I think this looks fantastic, and solves a long-standing issue. Thanks very much, @muxout!

@@ -90,6 +90,11 @@ oppia.controller('I18nFooter', [
$http.put(siteLanguageUrl, {
site_language_code: $scope.currentLanguageCode
});
} else {
// This is a workaround for the library page as the base url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, now I get it!

Yep, makes sense. My understanding is that sometimes $translate sets this cookie automatically, but we are setting it manually here in all cases for safety's sake. Is that right? If so, that makes sense to me.

Can we explain this a bit more in the comment so that future devs have context? At the moment it reads like "this is something to be applied only in the library page", which is why I asked the question I did.

@@ -90,6 +90,11 @@ oppia.controller('I18nFooter', [
$http.put(siteLanguageUrl, {
site_language_code: $scope.currentLanguageCode
});
} else {
// This is a workaround for the library page as the base url
// changes the default cookie path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: End comments with a period.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} else {
// This is a workaround for the library page as the base url
// changes the default cookie path
$cookies.put('NG_TRANSLATE_LANG_KEY',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: if multiline strings are contained in (...), break after the first (

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@muxout muxout changed the title Fix #3551: Changed base_url to root in an attempt to fix language change on the … Fix #3551: Manually set cookie value during language change Aug 3, 2017
Copy link
Member

@seanlip seanlip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @muxout!

@seanlip seanlip merged commit 0ea0c85 into develop Aug 3, 2017
@seanlip seanlip deleted the language-persistency branch August 3, 2017 19:02
giritheja added a commit to giritheja/oppia that referenced this pull request Aug 8, 2017
* upstream/develop: (39 commits)
  Add a one-off job to list which explorations use gadgets; remove old fallbacks job. (oppia#3717)
  Fix oppia#2998 implement collection skills update commands (oppia#3710)
  Improvements and bug fixes to changes introduced by PR oppia#3671 (oppia#3684)
  Fix oppia#3688: refactoring ExplorationStatusHandler to remove unpublish and publicize functionality as it is not called from frontend. (oppia#3715)
  fix oppia#3698: Shift exception handling for NotLoggedInException so that it does not generate log errors. (oppia#3714)
  Revert "Fix oppia#3707 Change text of collection learner view "start here"" (oppia#3713)
  Fix oppia#3701 Fixed Continue button name issue (oppia#3711)
  Fix oppia#3467: Introduce re-training and add_to_training_queue methods (oppia#3650)
  Fix oppia#3707 Change text of collection learner view "start here" (oppia#3700)
  Audio fixes: make audio download URL safe; don't show flagging modal if all translations are flagged; use templateUrl for modals. (oppia#3709)
  Learner audio updates (oppia#3705)
  Move hint button to left corner for supplemental cards (oppia#3702)
  Make the representation of the learner's 'Continue' answer more conversational. (oppia#3675)
  Add editor for audio translations (oppia#3692)
  Fix oppia#3686 fixed text overflow for contributors list (oppia#3697)
  Fix oppia#3551: Manually set cookie value during language change (oppia#3622)
  Learner view audio translations (oppia#3681)
  The sixth milestone of the learner dashboard. (oppia#3680)
  Routine update of translations. (oppia#3685)
  SiteWide ACL Refactor: Milestone 3.1 (oppia#3682)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants