plone / volto

React-based frontend for the Plone Content Management System
https://demo.plone.org/
MIT License
459 stars 624 forks source link

Using Volto in Brazilian Portuguese causes api requests to be duplicated #3786

Closed wesleybl closed 1 year ago

wesleybl commented 2 years ago

Describe the bug

When we configure Voto to use Brazilian Portuguese as the default language, Plone api requests are made twice. If you check the browser development tool, after pressing F5, you will see two requests to the following URLs:

http://localhost:3000/++api++/@navigation?expand.navigation.depth=1
http://localhost:3000/++api++/@breadcrumbs
http://localhost:3000/++api++/@actions
http://localhost:3000/++api++/

To Reproduce Steps to reproduce the behavior:

  1. Create a Volto project:
    
    npm install -g yo @plone/generator-volto
    yo @plone/volto --canary

follow the prompts questions, provide myvoltoproject as project name then, when it finishes:

cd myvoltoproject

2. Edit the `src/config.js` file, to use the pt-br language. The file should look like this:

```javascript
import '@plone/volto/config';

export default function applyConfig(config) {
  config.settings = {
    ...config.settings,

    isMultilingual: false,
    supportedLanguages: ['pt-br'],
    defaultLanguage: 'pt-br',

  };

  return config;
}
  1. Create a Plone Site Volto, using the Brazilian Portuguese language.
  2. Start Volto.
  3. Access http://localhost:3000/ in the browser and press F5.
  4. See duplicate requests in the browser development tool.

Expected behavior I expect requests to be made only once. When the site is in English, these duplicate requests do not occur.

Software (please complete the following information):

wesleybl commented 2 years ago

If I press f5 several times, I can see that the interface is initially loaded in English and then converted to Portuguese.

wesleybl commented 2 years ago

When I use pt-BR instead of pt-br, it works as expected.

pt-br should it work too? If not, the documentation needs to be updated:

https://github.com/plone/volto/blob/fd4566ae08e240becc0e617bad9f244ed3c1c40e/docs/source/configuration/multilingual.md?plain=1#L26

tiberiuichim commented 2 years ago

(hearsay) there's all kind of shenanigans when the frontend language code doesn't match the backend language codes. I think we need a better "language negociation" mechanism.

wesleybl commented 2 years ago

@sneridagh In this commit: https://github.com/plone/volto/commit/c579124a00d6da0b32e3d26bc291e595a3e4ee85, you save the I18N_LANGUAGE cookie in the lang_COUNTRY format (e.g. pt_BR). But everywhere you read a language to get the translation file, you use the normalizeLanguageName method, to convert it to pt_BR format. So I ask, couldn't the cookie be saved in pt-br format? I think part of this problem is caused by the cookie in the pt_BR format, since the content of Plone has the language format as pt-br.

sneridagh commented 2 years ago

I would have to revisit it to remember the whole problem. I think that it was that internally gettext uses _ and Plone - so yiu have to "normalize" them at some point. We decided to normalize it to Plone way. If the issue are duplicated requests somewhere, we need to address it but we'll have to deal with this situation anyways I guess.

wesleybl commented 2 years ago

@sneridagh it looks like react-intl uses the pt-BR format. See:https://stackoverflow.com/questions/48988389/what-format-of-locale-does-react-intl-use

It even seems that the pt_BR format is not even supported by react-intl. I saw this comment: https://github.com/plone/volto/blob/848cbe155f97662d510613a4e9f0c6e2ba482ee1/src/helpers/Utils/Utils.js#L194-L196

So I propose to write cookie I18N_LANGUAGE in pt-BR format instead of pt_BR and convert the pt-br Plone content to pt-BR

I renamed the folder https://github.com/plone/volto/tree/master/locales/pt_BR to pt-BR and yarn i18n worked.

Can it be like this?

One thing I noticed is that Plone Classic writes the I18N_LANGUAGE cookie in the pt_BR format. Would it be a problem for Volto to record in a different format? Is that why Volto used the pt_BR format until now?

ericof commented 1 year ago

@sneridagh This (and #4644) are blocking the usage of Volto with Brazilian sites right now.

davisagli commented 1 year ago

@wesleybl @ericof With #4741 I am seeing 2 requests for /@actions even in an English site, so I'm not sure this is entirely related to language codes.

davisagli commented 1 year ago

The I18N_LANGUAGE cookie comes from the Plone backend language negotiation in plone.i18n; it needs to remain in the format expected there.

davisagli commented 1 year ago

Fixed in https://github.com/plone/volto/pull/4741. (There are still 2 requests for @actions, but that is unrelated to the language. See https://github.com/plone/volto/issues/2781 and https://github.com/plone/volto/issues/4263)