-
Notifications
You must be signed in to change notification settings - Fork 9
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 metadata length computation logic #383
Conversation
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.
Tested it on Pocket Casts iOS via this PR
gp_downloadmetadata
output using version 4.2.0 (what resulted in this issue being noticed in the first place)
[14:20:55]: Project URL: https://translate.wordpress.com/projects/pocket-casts/ios/release-notes/
[14:20:55]: Locales: {"de"=>"de-DE", "es"=>"es-ES", "fr"=>"fr-FR", "it"=>"it", "ja"=>"ja", "nl"=>"nl-NL", "pt-br"=>"pt-BR", "ru"=>"ru", "sv"=>"sv", "zh-cn"=>"zh-Hans", "zh-tw"=>"zh-Hant"}
[14:20:55]: Source locale: -
[14:20:55]: Path: /Users/gio/Developer/a8c/pcios/fastlane/metadata
Downloading language: de-DE
[14:20:56]: Rejecting de-DE traslation for app_store_subtitle: translation length: 31 - max allowed length: 30
Downloading language: es-ES
[14:20:56]: Rejecting es-ES traslation for app_store_desc: translation length: 4031 - max allowed length: 4000
Downloading language: fr-FR
[14:20:57]: Rejecting fr-FR traslation for app_store_desc: translation length: 4031 - max allowed length: 4000
Downloading language: it
Downloading language: ja
Downloading language: nl-NL
Downloading language: pt-BR
Downloading language: ru
Downloading language: sv
Downloading language: zh-Hans
Downloading language: zh-Hant
Output from this branch:
[14:20:08]: ---------------------------------
[14:20:08]: --- Step: gp_downloadmetadata ---
[14:20:08]: ---------------------------------
[14:20:08]: Project URL: https://translate.wordpress.com/projects/pocket-casts/ios/release-notes/
[14:20:08]: Locales: {"de"=>"de-DE", "es"=>"es-ES", "fr"=>"fr-FR", "it"=>"it", "ja"=>"ja", "nl"=>"nl-NL", "pt-br"=>"pt-BR", "ru"=>"ru", "sv"=>"sv", "zh-cn"=>"zh-Hans", "zh-tw"=>"zh-Hant"}
[14:20:08]: Source locale: -
[14:20:08]: Path: /Users/gio/Developer/a8c/pcios/fastlane/metadata
Downloading language: de-DE
[14:20:09]: Rejecting de-DE translation for app_store_subtitle: translation length: 31 - max allowed length: 30
Downloading language: es-ES
Downloading language: fr-FR
Downloading language: it
Downloading language: ja
Downloading language: nl-NL
Downloading language: pt-BR
Downloading language: ru
Downloading language: sv
Downloading language: zh-Hans
Downloading language: zh-Hant
The de-DE
31 characters violation is a genuine one:
I noticed there are not tests for this fix in the diff, then noticed there are no tests for metadata_download_helper
in the first place.
@@ -58,15 +58,15 @@ def reparse_alternates(target_locale, loc_data, is_source) | |||
if file[0].to_s == key | |||
puts "Alternate: #{key}" |
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.
Unrelated to this PR, but I wonder whether we should update this puts
and the one above at line 57 to UI.message
?
Yeah I thought about adding unit tests but wanted this workaround to be shipped ASAP (wanted to timebox my effort on this bug)… but also mostly because we intend to re-implement this whole action completely some time soon (remember the long RFC P2s we wrote about generating then parsing back metadata strings from po files when working on the L10n tooling project? 😛) so I figured we'd TDD and make that more testable when we get to that instead. |
I'm going to merge this so it doesn't get stale while Olivier's AFK. Too bad I missed the 5.0.0 cut. |
Fixes #382
Why
The logic to compute the translation's lengths in metadata was incorrect.
How things work
When exporting Store metadata translations from GlotPress, the code from
gp_downloadmetadata_action
downloads the export as JSON format, which looks something like this (extract modified for simplicity, readability, and showing all cases)Notice how:
\u0004
.JSON.parse
them with Ruby they will be unescaped by the parser.\n
, not\r\n
.The bug
The core of the bug was was that to evaluate the translation's length, we used
msg.to_s.length - 4
on line 69.But on that line,
msg
can either be the original copy — extracted from the part after the\u0004
from the JSON key — or the actual Hash value — which is then an array with zero or one translation.That means that
msg = []
, thenmessage_len
would equal-2
(as[].to_s
is the string"[]"
of length 2, minus 4 equals -2)is_source == true
),message_len
would be 4 less than the actual lengthmsg = ["some\ntext"]
, then the fact thatmessage_len
used.to_s
on the Array meant that the string representation of that array not only contained the["
and"]
characters at start and end respectively (hence the-4
we added, I'd guess), but also every character escape like\n
would be re-escaped like\\n
(because.to_s
sees the Array as a whole, not individual strings). This was the root of the issue.Solution
When extracting the Hash's value to determine the value of
msg
, we're now usingd[1].first || ''
instead of justd[1]
.That means that
msg
will now always be aString
(both when it's mapped tosource
and when it's extracted from the Hash's value to get the translation), as opposed to before where it was either aString
(source
) or anArray
(Hash's valued[1]
)As a consequence, we can drop the
to_s
that introduced the bug, as well as the-4
that was supposed to (incorrectly) offset the value to account for theArray
delimiters when that array wasto_s
'd.To Test
I have NOT tested this solution with real use case.
The only thing I did to test it was to download a couple of
.json
exports from GlotPress as examples, then launchpry
, usedJSON.parse
to parse the json files, and play around with the parsed data to test my theories.As such, it would be nice to test this change with a real client app — for example point PocketCast to this branch of the toolkit, and run the
gp_downloadmetadata
action again, and check that this time it doesn't consider that the translations forapp_store_desc
are too long anymore. \cc @mokagio