Skip to content

Localize move notification titles to recipient's preferred language#101

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/add-translation-send-move-notification
Draft

Localize move notification titles to recipient's preferred language#101
Copilot wants to merge 5 commits intomainfrom
copilot/add-translation-send-move-notification

Conversation

Copy link
Contributor

Copilot AI commented Oct 25, 2025

Move notifications were hardcoded to English ("${playerName} made a move"). Now they're sent in the recipient's preferred language.

Changes

Cloud Function (functions/index.js)

  • Sends notification title with {{name}} token placeholder
  • Includes playerName and translationKey in the data payload for client-side translation

Service Worker (public/firebase-messaging-sw.js)

  • Added translation loading from locale files for 14 languages (ar, de, el, en, es, fi, fr, he, id, it, ja, nl, tr, zh)
  • Detects user's language preference from IndexedDB/localStorage or browser settings
  • Uses translationKey from notification data to look up translations generically
  • Replaces {{name}} token with playerName from data payload
  • Caches loaded translations for performance
  • Falls back to English for unknown languages

Locale Files

  • Added "moveNotification": "{{name}} <translated text>" to all 14 language files

Example

// Backend sends
notification: { title: '{{name}} made a move' },
data: { playerName: 'John', translationKey: 'moveNotification' }

// Service worker translates using the key
const translatedTitle = translation[translationKey].replace('{{name}}', playerName);
// → "Juan hizo un movimiento" (Spanish)
// → "Yuki さんが動きました" (Japanese)
// → "Pierre a fait un mouvement" (French)

Implementation

Translation is handled client-side in the service worker when background notifications are received:

  1. Service worker detects user's language preference
  2. Loads appropriate translation file from /locales/{lang}/translation.json
  3. Uses translationKey from data to find the translation
  4. Replaces {{name}} token with playerName from data
  5. Displays translated notification to user

This approach uses token-based translation that can be applied generically to any notification with a matching translation key.

Original prompt

Add translation to the send move notification title based off of the recipients local cow


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits October 25, 2025 01:17
…nt locale

Co-authored-by: ProLoser <67395+ProLoser@users.noreply.github.com>
…etch

Co-authored-by: ProLoser <67395+ProLoser@users.noreply.github.com>
Copilot AI changed the title [WIP] Add translation for send move notification title Localize move notification titles to recipient's preferred language Oct 25, 2025
Copilot AI requested a review from ProLoser October 25, 2025 01:23
@ProLoser
Copy link
Owner

@copilot ACTUALLY don't do it on the backend in the cloud function, JUST do it on the client or in the webworker

Co-authored-by: ProLoser <67395+ProLoser@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 25, 2025

@copilot ACTUALLY don't do it on the backend in the cloud function, JUST do it on the client or in the webworker

Done! Moved translation logic to the service worker (client-side). The backend Cloud Function is reverted to its original state, and the service worker now handles translating notification titles based on the user's language preference. Commit: 35c3a5a

const playerName = title ? title.replace(' made a move', '').trim() : player;

// Translate the title
const translatedTitle = translateTitle(title, playerName, translation);
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot use a token for the playername and don't check what type of notification it is as we should be applying translation to all notifications when one is available

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated! The backend now sends {{name}} token with playerName and translationKey in data. The service worker uses the translationKey to look up the translation and replaces {{name}} with the player name. This generic approach will work for any notification that has a matching translation key. Commit: 6dee5c7

…tifications

Co-authored-by: ProLoser <67395+ProLoser@users.noreply.github.com>
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.

2 participants