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

Adding I18n string tracking decorator #36493

Merged
merged 2 commits into from
Sep 1, 2020

Conversation

daynew
Copy link
Member

@daynew daynew commented Aug 28, 2020

As a code.org developer, I want to record what I18n strings we are using in our Javascript code.

This PR adds a decorator to our i18n strings (*_locale.js files) which stores the string key and the file it came from to window.stringUsageSources. The records stored in this object will periodically be uploaded by a worker thread to our webservers and uploaded to Firehose (the worker thread will be implemented in a future PR).

  • Adds i18nStringTracker.js which decorates a given *_locale.js object so its usage can be recorded.
  • Records string usage to a new global context object called window.stringUsageSources

Example

window.stringUsageSources

{ 
  common_locale: [ "curriculum", "teacherForum", "professionalLearning", ...],
  maze_locale: [...]
}

Links

Testing story

Reviewer Checklist:

  • Tests provide adequate coverage
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

@daynew daynew requested a review from a team August 28, 2020 06:46
@@ -8,4 +8,9 @@
*/
// locale for applab

export default window.locales.applab_locale;
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why this file doesn't use safeLoadLocale...

Copy link
Contributor

Choose a reason for hiding this comment

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

probably an oversight on my part when I originally implemented that helper. I certainly don't recall intentionally excluding applab

}

export default function localeWithI18nStringTracker(locale, source) {
if (!experiments.isEnabled(experiments.I18N_TRACKING)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not against it but why do we need an experiment here? We're not uploading anything yet, plus this flag needs a deploy to change.

Copy link
Member Author

@daynew daynew Sep 1, 2020

Choose a reason for hiding this comment

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

This is for protection in case this wrapper does some how screw things up when deployed to the wild. I did try testing on all the level types, but I'm trying to be extra careful since this is a wide impacting change. I'd rather do another deploy to turn on the feature rather than an emergency deploy to turn it off.

Copy link
Contributor

@bethanyaconnor bethanyaconnor left a comment

Choose a reason for hiding this comment

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

Looks great!

@@ -8,4 +8,9 @@
*/
// locale for applab

export default window.locales.applab_locale;
Copy link
Contributor

Choose a reason for hiding this comment

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

probably an oversight on my part when I originally implemented that helper. I certainly don't recall intentionally excluding applab

// Iterates each function in the given locale object and creates a wrapper function.
Object.keys(locale).forEach(function(stringKey, index) {
localeWithTracker[stringKey] = function(d) {
let value = locale[stringKey](d);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
let value = locale[stringKey](d);
const value = locale[stringKey](d);

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

return locale;
}

let localeWithTracker = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
let localeWithTracker = {};
const localeWithTracker = {};

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

// A map of "string source" -> "list of strings used"
// For example: window.stringUsageSources['maze'] = ['level_1.instruction.header']
// The data recorded in this will periodically be uploaded.
if (!window.stringUsageSources) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the motivation behind attaching this to the global window object?

Copy link
Member Author

Choose a reason for hiding this comment

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

I just wanted somewhere to buffer them and have evidence that it's intercepting the string usage and logging it.
In a follow-up PR, where I actually use the buffer where the string usage is logged, I will re-evaluate what the best way to implement this buffer is. I will follow-up with you offline to see if you have any recommendations.

@daynew daynew force-pushed the fnd-1243-javascript-string-tracking-decorator branch from 35444c8 to f3f8331 Compare September 1, 2020 00:55
@daynew daynew merged commit 3f5694b into staging Sep 1, 2020
@daynew daynew deleted the fnd-1243-javascript-string-tracking-decorator branch September 1, 2020 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants