-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Send all translations to gutenberg-mobile #9249
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
Conversation
Generated by 🚫 Danger |
|
Danger run resulted in 2 warnings; to find out more, see the checks page. Generated by 🚫 dangerJS |
| return mRetainedGutenbergContainerFragment; | ||
| } | ||
|
|
||
| public Bundle getTranslations() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a local method so we should change method access signature probably to private.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method name doesn't tell us enough information about what is the purpose of this method.
getTranslations() is too abstract I think ?
| FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | ||
| GutenbergContainerFragment gutenbergContainerFragment = | ||
| GutenbergContainerFragment.newInstance(isNewPost); | ||
| GutenbergContainerFragment.newInstance(isNewPost, localeSlug, this.getTranslations()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need for this in this.getTranslations()so we can remove it.
|
Hey @Tug @hypest , I must admit that I am not 100% sure what should method When I see the log, there is a bunch of strings that are not label related, e.g. SVG Path, fonts names, and also class names Please take a look at the file. |
|
|
||
| public Bundle getTranslations() { | ||
| Bundle translations = new Bundle(); | ||
| Locale defaultLocale = new Locale("en"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code will always return English translation, is that idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The english locale yes, the idea is for getTranslations to return a mapping of english string => [ translated string ] so we feed it to the @wordpress/i18n lib.
| try { | ||
| resourceId = stringField.getInt(R.string.class); | ||
| } catch (IllegalArgumentException | IllegalAccessException e) { | ||
| e.printStackTrace(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in Android it's always better to Log the error and not print stack trace so that we can easily find and see what is the error about.
It's not that odd given that we extract all the strings from |
|
Hey @Tug, I have managed to get the strings from the main application. This is the example approach: Probably you will notice that you will need to know the exact name of each string, so maybe you can define those names in Let me know if you need any further help regarding this one. |
| return translations; | ||
| } | ||
|
|
||
| for (Field stringField : R_String.getDeclaredFields()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm interesting, I don't like hacky solutions generally 🙂
as it can just to stop working at some API levels.
Did you test it with different Android API levels?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it's hacky, especially the part where it assumes that the R class is in the same package as the main Application class.
I'm not sure what you mean by different Android API levels though so I guess I didn't test that.
In theory I think we would need to go get the strings as an argument from the main app instead of trying to access a package we should not have to be aware of here.
It's starting to become a lot of changes for this "temporary" i18n solution though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding Android API I am thinking to try this code with some devices that have Lollipop, Marshmallow... Nougat,,, and especially EMUI (Huawei) installed.
|
|
||
| DisplayMetrics metrics = new DisplayMetrics(); | ||
| getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); | ||
| configuration.locale = defaultLocale; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused by this line... should we assign the desired locale here? Like, the same as the one passed down the the RN app? Instead, this line seems to again assign en 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nop this is required to get the default (english) resources. We'll need both the current resource which is whatever the app is set to at the moment and the english ones to generate our english => [ translated ] map
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, right. Yeah, I guess it would be useful if we add some line comments to describe what's going on in the various code paragraphs of this method.
| // Show the GB informative dialog on editing GB posts | ||
| showGutenbergInformativeDialog(); | ||
| return GutenbergEditorFragment.newInstance("", "", mIsNewPost); | ||
| String languageString = LocaleManager.getLanguage(EditPostActivity.this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, let's see, is there some specific reason we're using LocaleManager.getLanguage() here (which gets the device language) but we use Configuration.locale later in getTranslations() (which get's the current app locale)?
Unfortunately, the device and app locales can be out of sync unless the app is restarted so, this ends up having, for example, English UI (see top bar) but Spanish content strings (see the title placeholder):

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Locale.getLanguage(context) will only return the device language if no custom app-level language is set in SharedPreferences.
WordPress-Android/WordPress/src/main/java/org/wordpress/android/util/LocaleManager.java
Line 82 in 699d215
| return prefs.getString(LANGUAGE_KEY, LanguageUtils.getCurrentDeviceLanguageCode()); |
| Bundle translations = new Bundle(); | ||
| Locale defaultLocale = new Locale("en"); | ||
| Resources currentResources = getResources(); | ||
| Configuration configuration = currentResources.getConfiguration(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, but not it makes debugging this method easier: Let's rename this variable to currentConfiguration instead of just configuration and then at line 128 let's create a new one instead of reassigning the old one. Thanks!
|
Tested the PR at this stage by manually adding some gutenberg-mobile strings in the Spanish strings.xml file and verified that they get loaded in the RN app. I think this PR is good to go. |
hypest
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
@Tug , can you make a note to address any remaining review feedback in a follow a PR? Thanks! |
|
Let's wait for the gutenberg-mobile PR to merge and update its hash here, then we can merge. |
Requires wordpress-mobile/gutenberg-mobile#536
This PR adds a new property our react app will receive which will contain all the translations available for WPAndroid. The idea is to have translations in gutenberg-mobile available for the beta.
Testing Instructions
git fetch -a && git checkout add/i18n-cache) and make sure to update the submodules after thatgit submodule update --init --recursivecd libs/gutenberg-mobile && yarn install && yarn start:reset./gradlew installWasabiDebugthenadb shell am start -n "org.wordpress.android.beta/org.wordpress.android.ui.WPLaunchActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER(or boot the app with Android studio)en, and thattranslationsis emptyzh-cnorpt-br)libs/gutenberg-mobile/i18n-cache/data/you should see the translated strings.