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

[WIP] [Locale] Intl C4R #741

Closed
wants to merge 24 commits into from
Closed

[WIP] [Locale] Intl C4R #741

wants to merge 24 commits into from

Conversation

menusal
Copy link
Contributor

@menusal menusal commented Jul 13, 2023

Description

Shortcut: 145211

Use multi-language in projects

Type of change

(choose one and remove the others)

  • Feature

Acceptance

Please describe how to validate the feature or fix

  1. go to X
  2. test Y
  3. assert Z

If feature deals with theme / UI or internal elements used also in CARTO 3, please also add a note on how to do acceptance on that part.

Basic checklist

  • Good PR name
  • Shortcut link
  • Changelog entry
  • Just one issue per PR
  • GitHub labels
  • Proper status & reviewers
  • Tests
  • Documentation

@menusal menusal marked this pull request as draft July 13, 2023 21:06
@github-actions
Copy link

github-actions bot commented Jul 13, 2023

Pull Request Test Coverage Report for Build 6236847753

  • 48 of 49 (97.96%) changed or added relevant lines in 15 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 71.716%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/react-ui/src/widgets/TableWidgetUI/TableWidgetUI.js 2 3 66.67%
Totals Coverage Status
Change from base Build 6182739362: 0.2%
Covered Lines: 2301
Relevant Lines: 2972

💛 - Coveralls

@github-actions
Copy link

github-actions bot commented Jul 13, 2023

Visit the preview URL for this PR (updated for commit e4b04b6):

https://cartodb-fb-storybook-react-dev--pr741-feature-menusal-zassqukm.web.app

(expires Fri, 21 Jul 2023 07:01:04 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 517cc4d31d7e09cf277774e034094b67c301cd4c

@menusal menusal requested a review from aaranadev July 14, 2023 07:07
Copy link
Contributor

@zbigg zbigg left a comment

Choose a reason for hiding this comment

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

looks good 👍 , only minor comments

packages/react-ui/src/hooks/useImperativeIntl.js Outdated Show resolved Hide resolved
@@ -182,7 +187,14 @@ function Header({
</Tooltip>
)}
{switchable && (
<Tooltip title={(visible ? 'Hide' : 'Show') + ' layer'}>
Copy link
Contributor

Choose a reason for hiding this comment

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

create two proper distinct translations for 'Hide layer' and 'Show layer'

(current approach assumes language without declension like polish: layer=warstwa, Show layer=Pokaż warstwę)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@VictorVelarde
Copy link
Contributor

Hi @aaranadev this is almost there.

If you can please have a general look / do some basic cleanup I think we'll be ready for an alpha. With the alpha, we will make a PR on cloud-native to check it works and we will appreciate a very small PR in carto-react-template to validate it works as expected

@menusal menusal marked this pull request as ready for review September 19, 2023 14:05
@menusal
Copy link
Contributor Author

menusal commented Sep 19, 2023

Hi @aaranadev this is almost there.

If you can please have a general look / do some basic cleanup I think we'll be ready for an alpha. With the alpha, we will make a PR on cloud-native to check it works and we will appreciate a very small PR in carto-react-template to validate it works as expected

@VictorVelarde @zbigg @aaranadev
I have resumed PR. Please check the latest commits.
Next step before merging the PR. Try using the Provider in the template and check that the language changes.

Update: After testing useImperative hook version into the C4R template without success, we need to discuss the following options:

Imperative API

Imperative API

Cannot be used with IntlProvider, use createIntl to create an intl object without using a provider.

Option A (currently implemented in the PR)

  1. Expose a property to which the intl configuration is passed.
// Widget
<CategoryWidget
    id='revenueByStoreType'
    title='Category'
    dataSource={source.id}
    column='storetype'
    operationColumn='revenue'
    operation={AggregationTypes.SUM}
    intlConfig={INTL_CONFIG}
/>

Pros

  • The property is optional; the default configuration is used if not passed.
  • No need to create a provider for each component that needs translation.
  • Can be used in any component and leave others with the default translation.
  • Does not force the use of an Intl provider in applications that use a different translation system, such as react-i18next or a different version of react-intl than the one used in C4R.

Cons

  • A property is exposed in the widgets.
  1. Use redux with the intl configuration to inject messages into the store and use them in any component that needs translation.
import { setItlConfig } from 'from '@carto/react-redux'

dispatch(setItlConfig(INTL_CONFIG))

Pros

  • No property is exposed. One dispatch for all translations.
  • No need to create a provider for each component that needs translation.
  • Does not force the use of an Intl provider in applications that use a different translation system, such as react-i18next or a different version of react-intl than the one used in C4R.

Cons

  • Dependency on redux.
  • All widgets are translated.
  • The intl configuration must be passed as a property to the UI of the widgets.

NOTE: Both options use the same object to pass the Intl configuration:

// External application using Carto for React
export const INTL_CONFIG = {
  locale: 'id',
  messages: {
    c4r: {
      widgets: {
        category: {
          apply: 'Apply',
          unlock: 'Unlock',
          lock: 'Lock',
          clear: 'Clear',
          noResults: 'No results',
          noResultsMessage:
            'Your search "{searchValue}" did not yield any matches.',
          // Rest of the messages
        }
      },
    },
  },
};

Option B: IntlProvider (Not tested)

IntlProvider

  1. Create an IntlProvider provider with the intl configuration and use it in the component.

    Pros

    • No property is exposed in the widgets.

    Cons

    • All widgets are translated.
    • Need to create an Intl provider in each component that needs translation.
    • Users must use an Intl provider in their applications, even if they might want to use a different translation system, such as react-i18next or a different version of react-intl than the one used in C4R.

@menusal menusal changed the title [Locale] Intl C4R [WIP] [Locale] Intl C4R Oct 2, 2023
const messagesLocale = findMatchingMessagesLocale(locale);
const intMessages = intlConfig?.messages
? flattenMessages(intlConfig?.messages)
: messages[messagesLocale];
Copy link
Contributor

Choose a reason for hiding this comment

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

i think all lines above should be in memo together with createIntl

mainly findMatchingMessagesLocale and flattenMessages are not trivial and everything depends only on intlConfig

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

@@ -81,6 +81,7 @@ const EMPTY_ARRAY = [];
* @param {string[]=} [props.palette] - Optional palette
* @param {object} [props.wrapperProps] - Extra props to pass to [WrapperWidgetUI](https://storybook-react.carto.com/?path=/docs/widgets-wrapperwidgetui--default).
* @param {object} [props.noDataAlertProps] - Extra props to pass to [NoDataAlert]().
* @param {object} [props.intlConfig] - Object with intl configuration. If not provided, default messages will be used.
Copy link
Contributor

Choose a reason for hiding this comment

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

please use {object=} to indicate that this is optional parameter

(not necessarily here, as there is proper .d.ts, but mainly for other widgets and places when you add optional param

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to remove all intlConfig props, and we are to implement the Provider option.

@menusal menusal marked this pull request as draft October 18, 2023 07:56
@menusal
Copy link
Contributor Author

menusal commented Oct 18, 2023

Closed in favor to B option

@menusal menusal closed this Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants