-
Notifications
You must be signed in to change notification settings - Fork 1
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
MPDX-8309 Send user's language to GraphQL server #1095
Conversation
Bundle sizes [mpdx-react]Compared against e4cb967 No significant changes found |
src/lib/apollo/link.ts
Outdated
new ApolloLink((operation, forward) => { | ||
const languageHeader = language ? { 'Accept-Language': language } : {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't the server automatically honor the user's language preference? This code looks like it uses the language preference and defaults to the accept header: https://github.com/CruGlobal/mpdx_api/blob/7eff744504fe52358a0a975cb5f36ee6b5f1e5ad/app/controllers/graphql_controller.rb#L59-L68
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm reading it right, scope_request_to_locale
runs for every GraphQL request https://github.com/CruGlobal/mpdx_api/blob/7eff744504fe52358a0a975cb5f36ee6b5f1e5ad/app/controllers/graphql_controller.rb#L2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't the server automatically honor the user's language preference? This code looks like it uses the language preference and defaults to the accept header:
Yes, it does, but by default, GraphQL sends the en-US
value as the Accept-Language
if it is missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I misread it. The server uses the Accept-Language
header first and falls back to the user's preference. I feel like we should ask the server people to switch that. In the meantime, can you just set Accept-Language
to an empty string? When I do that, translated constants are coming through. I think that would be a simpler solution and it won't require the user to log out to update their language.
operation.setContext(({ headers }) => ({ | ||
headers: { | ||
...headers, | ||
...languageHeader, | ||
Authorization: `Bearer ${apiToken}`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This worked for me
Authorization: `Bearer ${apiToken}`, | |
Authorization: `Bearer ${apiToken}`, | |
'Accept-Language': '', |
src/components/Settings/preferences/accordions/LanguageAccordion/LanguageAccordion.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! I assume we've tested that this won't break code that expects the constants to be in English?
Preview branch generated at https://MPDX-8309.d3dytjb8adxkk5.amplifyapp.com |
Description
In this PR, I send the user's language to the GraphQL API.
Client-side GraphQL
In my first commit, I change the client-side GraphQL to send the user's language to the API.
This includes grabbing the user's language preferences and adding it to the Next Auth session so we can use it throughout the site.
Server-side GraphQL
In the second commit, I ensure the server-side is also sending the user's language to the API. (Not proxy like I put in my commit message)
Show message to users to re-login
In the third commit I add a message to users asking them to logout and log back in after changing their language to ensure the language is fully updated. This is because we only get to set the NextAuth session on login.
Checklist: