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

Add method to obtain native string resource in localization plugin. #24575

Merged
merged 4 commits into from Feb 25, 2021

Conversation

chingjun
Copy link
Contributor

Engine side implementation for go/flutter-locale-split.

Only Android implementation for now just for review. After this is reviewed I'll work on the iOS implementation in a separate PR.

Tests will follow.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

String parts[] = localeString.split("-", -1);
String languageCode = parts[0];
String countryCode = parts.length > 1 ? parts[1] : null;
String scriptCode = parts.length > 2 ? parts[2] : null;
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe scriptCode comes before countrycode in locale strings. Though I'd have to double check to see the exact spec. It should be something like country/region code is 2char, while scriptcode is 4char. In most instances, the scriptcode has come first, eg, zh-Hans-cn

There is also divergent behavior with using - and _. I'd replace one to the other to handle both for robustness.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I got confused by the order of the country code in the constructor.

Fixed this as suggested!

@GaryQian
Copy link
Contributor

This needs tests!

@chingjun
Copy link
Contributor Author

Added tests, PTAL, thanks!

stringToReturn = localContext.getResources().getString(resId);
}

if (localeString != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, comment the API number for Build.VERSION_CODES.JELLY_BEAN_MR1

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

if (localeString != null) {
Locale locale = localeFromString(localeString);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Copy link
Contributor

@GaryQian GaryQian Feb 24, 2021

Choose a reason for hiding this comment

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

nit, comment the API number for Build.VERSION_CODES.JELLY_BEAN_MR1, same as below.

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

Locale locale = localeFromString(localeString);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Configuration conf = new Configuration(context.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.

nit, spell out config or configuration.

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. I went with config since because this file has already used config.

*/
public interface LocalizationMessageHandler {
/**
* The Flutter application would like to obtain the string resource of given {@code key} in
Copy link
Contributor

Choose a reason for hiding this comment

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

Rephrase to be less passive/third person.

perhaps Obtains the string resource of the provided {@code key} ...

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 copied the tone from PlatformChannel.java.

The way I understand it is this comment is meant for the implementor of this interface.

What do you think?

Copy link
Contributor

@GaryQian GaryQian left a comment

Choose a reason for hiding this comment

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

Some nits, otherwise, LGTM

@chingjun
Copy link
Contributor Author

Updated according to reviews :)

@VisibleForTesting
public static Locale localeFromString(String localeString) {
// Use Locale.forLanguageTag if available (API 21+).
if (false && Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any chance you remember why this if block exist?

Copy link
Contributor Author

@chingjun chingjun Feb 28, 2024

Choose a reason for hiding this comment

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

Most likely a mistake. The false && was probably added to temporarily test the else branch and I forgot to remove that.

Do you want me to send a PR to undo this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants