Research solutions for localization #393

Closed
opened 2026-04-17 08:31:28 +00:00 by a24vinla · 6 comments
Collaborator
No description provided.
Collaborator

Option:1

React's Context API
Using Reacts' Context API (createContext, useContext) a language can be set to English/Swedish and stored globally in <App>.
Child components and views can then access the current language and use a translation file in that view/component to get the correct text

positives

  • No external dependencies needed
  • Full Control
  • No extra functionality that we dont use gets implemented, making it lightweight.
  • Each view can have its own translation file

Negatives

  • Setup needed, Context API is well documented but there are fewer examples of using it specifically for translation compared to other common libraries. The implementation pattern is similar to light & dark mode.
  • You must implement advanced features yourself:
    • pluralization
    • interpolation ("Hello {name}")
    • formatting (dates, numbers)

Overall

If we just use it for choosing between translation outputs and do not intend to implement many languages, it is a good choice.
If a lot of formatting (numbers, dates) and interpolation (adding variables to strings) occurs, then more work is required.

### Option:1 _React's Context API_ Using Reacts' Context API (createContext, useContext) a language can be set to English/Swedish and stored globally in `<App>`. Child components and views can then access the current language and use a translation file in that view/component to get the correct text ### positives - No external dependencies needed - Full Control - No extra functionality that we dont use gets implemented, making it lightweight. - Each view can have its own translation file ### Negatives - Setup needed, [Context API is well documented](https://legacy.reactjs.org/docs/context.html) but there are fewer examples of using it specifically for translation compared to other common libraries. The implementation pattern is similar to light & dark mode. - You must implement advanced features yourself: - pluralization - interpolation ("Hello {name}") - formatting (dates, numbers) ### Overall If we just use it for choosing between translation outputs and do not intend to implement many languages, it is a good choice. If a lot of formatting (numbers, dates) and interpolation (adding variables to strings) occurs, then more work is required.
Collaborator

Option:2

i18next/react-i18next
react-i18next is a library that builds on i18next and uses React’s Context API internally, similar to Option:1, with the difference that the functionality is pre-made for ease of use. In the Github repo we can see that it has the MIT license, a large pool of contributors, and an average of 11 million downloads per week, making it a well-tested and widely used library.

Positives

  • Used extensively, meaning many examples and documentation exist
  • Advanced feature implementation exists:
    • pluralization
    • interpolation ("Hello {{name}}")
    • formatting (dates, numbers)
    • lazy loading
  • Each view can have its own translation file
  • Scales better with many languages

Negatives

  • Might be too big for our needs (in terms of extra functionality)
  • External dependencies needed
  • Setup needed. Documented on the official site and many unofficial sites with exampels

Overall

It is well tested and widely used, and if many languages and advanced functionality are required, it is a good choice.
However, depending on usage, it might become too large for what is required.

### Option:2 **i18next/react-i18next** react-i18next is a library that builds on i18next and uses React’s Context API internally, similar to Option:1, with the difference that the functionality is pre-made for ease of use. In the [Github repo](https://github.com/i18next/react-i18next) we can see that it has the MIT license, a large pool of contributors, and an average of 11 million downloads per week, making it a well-tested and widely used library. ### Positives - Used extensively, meaning many examples and documentation exist - Advanced feature implementation exists: - pluralization - interpolation ("Hello {{name}}") - formatting (dates, numbers) - lazy loading - Each view can have its own translation file - Scales better with many languages ### Negatives - Might be too big for our needs (in terms of extra functionality) - External dependencies needed - Setup needed. Documented on the [official site](https://react.i18next.com/latest/using-with-hooks) and many unofficial sites with [exampels](https://medium.com/@xbstrxct/localization-in-react-best-practices-e38dee708aae) ### Overall It is well tested and widely used, and if many languages and advanced functionality are required, it is a good choice. However, depending on usage, it might become too large for what is required.
Collaborator

Might add other options and keep adding to the once existing is later stages

Might add other options and keep adding to the once existing is later stages
Collaborator

The research is good overall, the trade-offs are well explained and the main decision point seem to be whether we expect translation to grow more complex over time. Since that will largely determine whether we should choose a lightweight context API solution or if a more complete library like react-i18next is better.

The comparison makes it clear that option 1 is better for simplicity and control while option 2 would reduce the maintenance risk in the future by handling common i18n concerns such as pluralization, interpolation, formatting, etc.

This might be something that should be brought before the customer such as issue: #402. Since in option2 we need to introduce external dependencies which could be a factor in the requirements.

The research is good overall, the trade-offs are well explained and the main decision point seem to be whether we expect translation to grow more complex over time. Since that will largely determine whether we should choose a lightweight context API solution or if a more complete library like react-i18next is better. The comparison makes it clear that option 1 is better for simplicity and control while option 2 would reduce the maintenance risk in the future by handling common i18n concerns such as pluralization, interpolation, formatting, etc. This might be something that should be brought before the customer such as issue: [#402](https://git.webug.se/Andras/BoundlessFlowCampus2K/issues/402#issuecomment-3779). Since in option2 we need to introduce external dependencies which could be a factor in the requirements.
Collaborator

The same answer as in #402, we should decide which one to use depending on an evaluation. It looks like React has built in localization and Henrik was a big fan since it doesn't add any dependencies. Us group leaders will decide this as soon as possible.

The same answer as in #402, we should decide which one to use depending on an evaluation. It looks like React has built in localization and Henrik was a big fan since it doesn't add any dependencies. Us group leaders will decide this as soon as possible.
Collaborator

It seems to use that React has a sort of built-in localization tool (React's Context API) and since we already use React and want as few dependencies as possible we have decided to use it.

When we talked with Henrik we talked about word count for the page. Since it is relatively low and we can translate it ourselves we should use something like a built in system instead of introducing something that can break.

If this turns out to be still a pain to setup even with our low word count we could consider changing to something else.

It seems to use that React has a sort of built-in localization tool (React's Context API) and since we already use React and want as few dependencies as possible we have decided to use it. When we talked with Henrik we talked about word count for the page. Since it is relatively low and we can translate it ourselves we should use something like a built in system instead of introducing something that can break. If this turns out to be still a pain to setup even with our low word count we could consider changing to something else.
Sign in to join this conversation.
No milestone
No project
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Andras/BoundlessFlowCampus2K#393
No description provided.