You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix getLocale calling back twice and repeating the failing locale - #4357
getLocale fetched the two letter locale code on the first try, not the full locale.
Region locales such as pt_BR, en_GB, and es_419 in _locales were never requested directly.
On failure it called getLocale('en', callback) twice.
Callback and updateContextMenu each ran twice for every locale lookup that missed.
getLocale now tries the full locale first, then the two letter code, then en.
Sorry, this function yet only translates the 2 words on right click on the extension (rate me, donate).
So (as of now) we should avoid loading the whole file there, so we could skip / save the computing in background.js
For the buttons added to youtube.com we currently use static Englisch terms only 😆
For our settings menu we use the following function falling back per message in two steps per message,
to make it possible to deduplicate our locale files beyond standard
(pt_BR and pt_PT only require their have unique changes, compared to pt. Finally the gaps in pt are fetched from en.)
(if this was cached, then this function would only run once in the background.js at if (installed.reason == 'update') {
or on change of the users browser language/s or ourr language settings)
agreed background.js needs only two strings. I can cache donate and rateMe in storage.local. Cache refresh runs on install, update and language change. Top-level updateContextMenu() reads the cache and skips getLocale on every service worker wake.
Good catch — the original getLocale was definitely double-calling the callback (once on the short-locale branch and once in the unconditional fallback). The new structure removes the redundant call.
A couple of concerns:
fallbackToEnglish is defined but never called when language === 'en' — tryShortLanguage checks if (shortLanguage === language) and just returns without falling back. That means if en itself 404s (it shouldn't, but), the user gets an empty callback. The old code had the same blind spot. Probably fine in practice, but consider at least a final hardcoded {} fallback so the extension never crashes on a missing locale.
fetch(...).catch(tryShortLanguage) and tryShortLanguage().catch(fallbackToEnglish) — .catch here returns the promise from the fallback, but tryShortLanguage itself doesn't return the fetch promise in a way that's reachable. Actually it does, via fetch(...).then(...).catch(fallbackToEnglish). OK, that works.
getLocale now has three nested function declarations inside (fallbackToEnglish, tryShortLanguage, plus the outer fetch). For a small helper this is fine, but the original was also nested and the indentation is inconsistent with the rest of background.js — please run the project's formatter (Prettier?) and confirm the diff matches existing style.
Tests? I don't see a test file change. If the project has any unit-test harness for background.js (or a Jest config), a couple of cases would be valuable:
getLocale('en_US') → resolves to en_US/messages.json
getLocale('en_US') → falls back to en/messages.json when en_US/messages.json is missing
getLocale('en') → resolves to en/messages.json (no infinite recursion)
getLocale('en') 404 → callback called with en/messages.json content (or {})
Otherwise: clean fix. Removing the duplicate getLocale('en', callback) is the actual bug fix and the new structure is easier to reason about.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4108.
getLocale fetched the two letter locale code on the first try, not the full locale.
Region locales such as pt_BR, en_GB, and es_419 in _locales were never requested directly.
On failure it called getLocale('en', callback) twice.
Callback and updateContextMenu each ran twice for every locale lookup that missed.
getLocale now tries the full locale first, then the two letter code, then en.