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

Fix #3786: Fix regex checks for translated strings #3787

Merged
merged 4 commits into from
Sep 14, 2021

Conversation

BenHenning
Copy link
Sponsor Member

@BenHenning BenHenning commented Sep 14, 2021

Explanation

Fixes #3786

#3754 introduced new regex checks to make sure translated strings go into the correct XML files, but the check didn't quite work for translated versions of strings.xml. However, the existing regex check didn't have support for matching against file patterns when exempting files (which is needed in this case since the check should exclude translated values directories).

The changes to the check include:

  • Adding support for exempting files by one or more patterns
  • Rearranging the logic to cache regex pattern compilation & file reading to avoid repeating these. This should be a nice performance improvement, but one drawback is the current implementation requires reading whole files into memory. Given we don't have any super large files in the codebase, this is probably fine. A future improvement would be to parallelize the file searching.

Note that one behavior change with the changes above is that failures will now be organized by file rather than by check (i.e. all failures for a particular file will be printed in order of that file's lines rather than in order of the checks).

See #3764 for a passing demonstration of the check (since the check is currently passing on develop; this is only an issue when trying to merge #3687--the test branch combines this & that branch). Also, new tests were added (one specifically for validating this case). See this test fail/pass without/with the regex change:

regex_test_failing
Test failing with the old pattern

regex_test_passing
Test passing with the new pattern

Finally, per the review an additional check was added to prohibited nested resource directories to simplify the set of potential matches for strings.xml (i.e. to ensure strings.xml must live immediately under a 'values--r/' directory structure).

Essential Checklist

  • The PR title and explanation each start with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".)
  • Any changes to scripts/assets files have their rationale included in the PR explanation.
  • The PR follows the style guide.
  • The PR does not contain any unnecessary code changes from Android Studio (reference).
  • The PR is made from a branch that's not called "develop" and is up-to-date with "develop".
  • The PR is assigned to the appropriate reviewers (reference).

For UI-specific PRs only

N/A

@BenHenning BenHenning marked this pull request as ready for review September 14, 2021 20:14
@Test
fun testFileContent_translatableString_inTranslatedPrimaryStringsFile_fileContentIsCorrect() {
val prohibitedContent = "<string name=\"test\">Translatable</string>"
tempFolder.newFolder("testfiles", "app", "src", "main", "res", "values-ar")
Copy link
Contributor

Choose a reason for hiding this comment

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

can we have a test case for

app/src/main/res/values-ar/<str>/strings.xml too

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Is this actually allowed by Android resources? I was under the impression that value files should generally live directly under their respective values folders?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Hmm https://stackoverflow.com/questions/4930398/can-the-android-layout-folder-contain-subfolders lays out contradictory information. I'm actually thinking that maybe we need a check to prohibit subdirectories instead of supporting subdirectories in these types of patterns.

Copy link
Contributor

Choose a reason for hiding this comment

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

image
This is the new android studio template for new projects
I guess it is allowed

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

@FareesHussain is that representing directory structure? That doesn't seem right at face value since themes.xml (night) means values-night/themes.xml. What's the directory structure for that folder ('project view' not 'Android view')?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah my bad, it is values-night/themes.xml

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

New check added.

Copy link
Contributor

@FareesHussain FareesHussain left a comment

Choose a reason for hiding this comment

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

LGTM,
I guess some test cases are needed
since we don't have a test case for the paths where there is a package between
values and strings.xml

@oppiabot
Copy link

oppiabot bot commented Sep 14, 2021

Unassigning @FareesHussain since the review is done.

@BenHenning
Copy link
Sponsor Member Author

New check has been added. Thanks @FareesHussain! PTAL.

Copy link
Contributor

@FareesHussain FareesHussain left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

@oppiabot
Copy link

oppiabot bot commented Sep 14, 2021

Hi @BenHenning, this PR is ready to be merged. Please address any remaining comments prior to merging, and feel free to merge this PR once the CI checks pass and you're happy with it. Thanks!

@oppiabot oppiabot bot added the PR: LGTM label Sep 14, 2021
@BenHenning
Copy link
Sponsor Member Author

Thanks again @FareesHussain!

@BenHenning BenHenning merged commit 2d47a87 into develop Sep 14, 2021
@BenHenning BenHenning deleted the fix-regex-checks-for-translated-strings branch September 14, 2021 23:16
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.

Regex checks fail for translations
2 participants