frontend-platform icon indicating copy to clipboard operation
frontend-platform copied to clipboard

feat(i18n): add language preference management functionality

Open Ang-m4 opened this issue 10 months ago • 7 comments

Language Management Module

Description

This PR intents to add a new LanguageManager module to standardize language switching functionality. The module provides a new API for handling language changes from any component in the application.

Key Features

  • Unified Language Management: Centralizes all language-related operations into a single module
  • Complete Language Switching Flow: Handles the entire language change process through a single function call
  • User Preference Persistence: Updates both client-side cookies and server-side user preferences
  • Session Management: Integrates with LMS backend to ensure consistent language experience
  • RTL Support: Automatically handles right-to-left language adjustments
  • Event System Integration: Publishes events when locale changes to allow components to react

Implementation Details

New Functions

  • changeUserSessionLanguage(languageCode): Main public function that orchestrates the language change process:

    • Sets the language cookie
    • Updates authenticated user preferences in the backend (if applicable)
    • Updates the session language
    • Publishes a locale change event
    • Handles RTL adjustments
  • updateUserPreferences(username, preferenceData): Updates server-side user language preferences via the API

  • setSessionLanguage(languageCode): Sets the language for the current session using the LMS setlang endpoint

API Usage

Components can now implement language switching with a single function call:

import { changeUserSessionLanguage } from '@edx/frontend-platform/i18n';

// When a user selects a new language
changeUserSessionLanguage('ar'); // Switch to Arabic

Related Work

This functionality is a core dependency for the Language Selector Implementation.

Ang-m4 avatar Apr 01 '25 19:04 Ang-m4

Thanks for the pull request, @Ang-m4!

This repository is currently maintained by @openedx/committers-frontend.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

:radio_button: Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
:radio_button: Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
:radio_button: Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.


Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

:bulb: As a result it may take up to several weeks or months to complete a review and merge your PR.

openedx-webhooks avatar Apr 01 '25 19:04 openedx-webhooks

I was thinking about the list of supported languages to display

I'm thinking as default use an array provided for frontend-platform using the messages object here that is configured during the MFE initialize. So the manager could return a function or variable with the list of locales Object.keys(messages)

@arbrandes what do you thing?

In that way we can get rid of SITE_SUPPORTED_LANGUAGES here, and in any case someone want to pass a more restricted or controlled list, they can do it by the plugin configuration.

dcoa avatar Jun 13 '25 01:06 dcoa

Codecov Report

:x: Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review. :white_check_mark: Project coverage is 87.31%. Comparing base (acf0d46) to head (729b314). :warning: Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
src/i18n/lib.js 85.71% 1 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #786      +/-   ##
==========================================
+ Coverage   87.13%   87.31%   +0.18%     
==========================================
  Files          48       50       +2     
  Lines        1407     1435      +28     
  Branches      297      305       +8     
==========================================
+ Hits         1226     1253      +27     
- Misses        168      169       +1     
  Partials       13       13              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jun 18 '25 19:06 codecov[bot]

Hi @arbrandes,

I made some changes to the code as you requested. Please let me know what you think:

  • Created a new languageApi module for easier testing.
  • Added an optional flag for reloading the page if needed.
  • Added a unit test.

and i would like to know your thoughts on @dcoa’s comments!

The endpoint request a CSRF token as you can see here. I just want to make sure the following line of code is correct as a suggested change from the frontend-platform perspective (getAuthenticatedHttpClient is the right service to use? -@arbrandes could you help me with this doubt?-).


I'm thinking as default use an array provided for frontend-platform using the messages object here that is configured during the MFE initialize. So the manager could return a function or variable with the list of locales Object.keys(messages)

Thank you so much for your time on this!

Ang-m4 avatar Jun 25 '25 16:06 Ang-m4

@arbrandes friendly tag for reviewing here. Thank you!

dcoa avatar Aug 25 '25 21:08 dcoa

@arbrandes friendly ping again. To not be a constant bother since I know you are deepdiving in the frontendbase stuff, I'd like to ask if you need to pass this to someone else to be a reviewer here.

felipemontoya avatar Sep 03 '25 20:09 felipemontoya

This LGTM!! thank you, @Ang-m4 .

@arbrandes the PR was updated accordingly to the feedback, could you please verify?

dcoa avatar Oct 08 '25 12:10 dcoa

We've successfully implemented the language selector feature on our end and everything is working well.

The only question we have is regarding the getSupportedLocaleList() function behavior. Currently, this function returns all available languages with translations, rather than filtering based on the SITE_SUPPORTED_LANGUAGES configuration.

We implemented a client-side workaround in the LanguageSelector component to filter the languages

Is this the intended behavior, or should getSupportedLocaleList() respect the SITE_SUPPORTED_LANGUAGES configuration automatically? @arbrandes Would you mind taking a look at this when you get a chance? Would appreciate your thoughts on the expected behavior here. Thanks!

Asespinel avatar Nov 26 '25 02:11 Asespinel