Skip to content

Conversation

@mzorz
Copy link
Contributor

@mzorz mzorz commented Aug 8, 2019

Fixes translation issues in the Editor when using Gutenberg.

Problem

Working with @daniloercoli it was found that when using mobile Gutenberg, some strings were defaulted to English even when the device was set to another supported language (for example, Spanish or Italian as per our tests).

Specifically, the overflow menu in the Editor was all in English, and within Gutenberg, the strings such as "ADD BLOCK" separator, or "Start writing..." hint on a new post were all in English:

Screenshot_20190807-083723

Furthermore, after attempting to use GB, the rest of the app would be back to be translated to English (except for the main navigation, for instance).
device-2019-08-07-143942

Research & discovery

So, we apparently had 2 problems.
One problem:

  • Options menu is not translated. This is on the native side, so I explored possibilities in which the native context would be re-set to default or english somehow. I've placed calls to check the application context configuration and each time I always get the correct locale set. I even queried the activity and application context configuration in onCreateOptionsMenu() and it still had the correct locale, but it still showed english.

Another problem:

  • I saw the gutenberg mobile strings are all in english as well: "Start writing..." hint, or "ADD BLOCK", etc. all of them are in english. But, the Title placeholder and the block list in the inserter are all translated. That seems weird. Looks like it's not getting translated somehow.

Looking further, found out these two:

            String currentResourceString = currentResources.getString(resourceId);
            String defaultResourceString = defaultResources.getString(resourceId);

in getTranslations() would both return the english string, when the currentResourceString should be the translated one, even when inspecting both currentResources and deafultResources had the right locale set (one the current, i.e. spanish, and the other one the default, that is en).

Solution

From testing various things, turned out both problems were the same; it seems these 2 changes were necessary:

  • using createConfigurationContext in 34257d6 to create a "copy" of the current context (both for checking current resources and creating the english reference) based on the configuration object as per the code shown here.
  • use the Activity's context rather than the global app context.

Doing either thing alone did not prove to work - rationale behind this is given I was still seeing the same default string being retrieved ("problem 2"), it looked like one of the internal references in Context was changed to the default one, in fact affecting the whole app with this change ("problem 1"). Using a more local context (Activity) and copying the configuration from there seems to behave in a good manner, without disrupting the app's context

To test:

  1. switch your device language to a supported language other than English
  2. make sure to opt-in to mobile gutenberg to start new posts in app settings
  3. start a new draft
  4. observe the strings in the overflow menu and all in Gutenberg show in the selected language.

Here are some various screenshots showing it works:

Italian:
Screenshot_20190808-013028

Spanish:
Screenshot_20190808-012854

German:
Screenshot_20190808-012423

Update release notes:

  • If there are user facing changes, I have added an item to RELEASE-NOTES.txt.

Context localizedContextCurrent = getActivity()
.createConfigurationContext(currentResources.getConfiguration());
// if the current locale of the app is english stop here and return an empty map
Configuration currentConfiguration = localizedContextCurrent.getResources().getConfiguration();
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if we can use currentResources.getConfiguration() here. currentResources are read from the activity and should work.

@daniloercoli
Copy link
Contributor

Nice work @mzorz! Tested your PR and worked without any problem on my IT device.

I've made some small changes to the code, and posted it below, since it was hard to follow with all that variables with similar names. I know, those were already there.

        Bundle translations = new Bundle();
        Locale defaultLocale = new Locale("en");
        Resources currentResources = getActivity().getResources();
        Configuration currentConfiguration = currentResources.getConfiguration();
        // if the current locale of the app is english stop here and return an empty map
        if (currentConfiguration.locale.equals(defaultLocale)) {
            return translations;
        }

        // Let's create a Resources object for the default locale (english) to get the original values for our strings
        Configuration defaultENLocaleConfiguration = new Configuration(currentConfiguration);
        defaultENLocaleConfiguration.setLocale(defaultLocale);
        Context localizedContextDefault = getActivity()
                .createConfigurationContext(defaultENLocaleConfiguration);
        Resources defaultResources = localizedContextDefault.getResources();

diff here: https://gist.github.com/daniloercoli/cf6381c3bb4316bf625689a08466bbf8

Feel free to discard proposed changes above.

Copy link
Contributor

@hypest hypest left a comment

Choose a reason for hiding this comment

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

Tested and the issue is fixed on my side 👍

@daniloercoli
Copy link
Contributor

Tested and LGTM!

We can take my comments in consideration for another PR.

@daniloercoli daniloercoli merged commit 4663ec6 into release/13.0 Aug 8, 2019
@daniloercoli daniloercoli deleted the fix/gb-editor-translations branch August 8, 2019 10:17
@mzorz
Copy link
Contributor Author

mzorz commented Aug 8, 2019

Thank you for your review and comments @daniloercoli @hypest !

And let's make the changes on another PR for sure! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants