Skip to content

Centralize JS translations in skin.js.php with zmPrimeTranslations() for lazy priming#4804

Draft
Copilot wants to merge 5 commits intomasterfrom
copilot/improve-js-translations
Draft

Centralize JS translations in skin.js.php with zmPrimeTranslations() for lazy priming#4804
Copilot wants to merge 5 commits intomasterfrom
copilot/improve-js-translations

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 6, 2026

Each *.js.php view file independently defined a translate JS object and common translated string constants, duplicating the same keys across multiple files with inconsistent escaping.

Changes

  • skin.js.php — Defines an empty translate object and a zmPrimeTranslations(t) helper (calls Object.assign(translate, t)) that any file can call to register translations without overwriting the shared object. Primes all common translations in one place, including alarm-related keys (EnableAlarms, DisableAlarms, ForceAlarm, CancelForcedAlarm) that MonitorStream.js needs across multiple views. Also defines common standalone string constants (deleteString, archivedString, unarchivedString, emailedString, yesString, noString, confirmDeleteEventsString), all using validJsStr() uniformly.

  • Removed from view files — Deleted the per-view var translate = {...} definitions from watch.js.php, event.js.php, and zone.js.php. Removed duplicated standalone string constants from event.js.php, events.js.php, snapshot.js.php, snapshots.js.php, timeline.js.php, watch.js.php, and zone.js.php.

  • Per-view priming patternevent.js.php demonstrates the pattern by calling zmPrimeTranslations({"Show Zones": "...", "Hide Zones": "..."}) for its page-specific keys; event.js accesses them via translate["Show Zones"] / translate["Hide Zones"].

  • MonitorStream.js — Updated 8 references from standalone variables (enableAlarmsStr etc.) to translate["EnableAlarms"] etc., since MonitorStream.js is loaded across 5 different views.

After — common translations registered once in skin.js.php:

const translate = {};
function zmPrimeTranslations(t) { Object.assign(translate, t); }
zmPrimeTranslations({
  "seconds": "...",
  "Fullscreen": "...",
  "EnableAlarms": "...",
  // all common keys...
});

Per-view additions in a view's .js.php:

zmPrimeTranslations({
  "Show Zones": "<?php echo validJsStr(translate('Show Zones')) ?>",
  "Hide Zones": "<?php echo validJsStr(translate('Hide Zones')) ?>",
});

The Object.assign approach makes zmPrimeTranslations additive and safe to call multiple times, laying the groundwork for future AJAX-loaded translations.

Copilot AI and others added 2 commits May 6, 2026 01:53
… skin.js.php refs #4290

Agent-Logs-Url: https://github.com/ZoneMinder/zoneminder/sessions/f8ffacbf-358b-47f5-a732-afdd9e5854a8

Co-authored-by: connortechnology <925519+connortechnology@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve centralized handling of JS text translations Centralize JS translations in skin.js.php instead of per-view redefinition May 6, 2026
Copilot AI requested a review from connortechnology May 6, 2026 01:55
@connortechnology
Copy link
Copy Markdown
Member

@copilot it would be nice if they would lazy load on use... and perhaps in each .js.php or even .js, we could call a function to prime it

Copilot AI and others added 2 commits May 6, 2026 02:43
…er view refs #4290

Agent-Logs-Url: https://github.com/ZoneMinder/zoneminder/sessions/3607efe7-9a75-41ed-82f3-e4975e4c3654

Co-authored-by: connortechnology <925519+connortechnology@users.noreply.github.com>
Agent-Logs-Url: https://github.com/ZoneMinder/zoneminder/sessions/3607efe7-9a75-41ed-82f3-e4975e4c3654

Co-authored-by: connortechnology <925519+connortechnology@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 6, 2026

@copilot it would be nice if they would lazy load on use... and perhaps in each .js.php or even .js, we could call a function to prime it

Done in commit 6aeaba5. Here's how the lazy-priming pattern works:

skin.js.php defines the empty registry and the priming function, then registers common translations:

const translate = {};
function zmPrimeTranslations(t) { Object.assign(translate, t); }
zmPrimeTranslations({
  "Fullscreen": "...",
  "EnableAlarms": "...",
  // all common keys...
});

Any view's .js.php file can now prime its own page-specific translations without touching the global object — e.g. event.js.php:

zmPrimeTranslations({
  "Show Zones": "<?php echo validJsStr(translate('Show Zones')) ?>",
  "Hide Zones": "<?php echo validJsStr(translate('Hide Zones')) ?>",
});

And the static event.js accesses them via translate["Show Zones"].

The Object.assign approach is intentional: it merges keys in, so calling zmPrimeTranslations multiple times is safe and additive — which makes it easy to extend with AJAX-loaded translations in the future.

Copilot AI changed the title Centralize JS translations in skin.js.php instead of per-view redefinition Centralize JS translations in skin.js.php with zmPrimeTranslations() for lazy priming May 6, 2026
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.

Improvement: The "translate" variable for translating JS text is defined in each *.js.php file.

2 participants