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

Implement hot reloading for the locale files during development #5050

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions _scripts/ProcessLocalesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class ProcessLocalesPlugin {
/** @type {(updatedLocales: [string, string][]) => void|null} */
this.notifyLocaleChange = null

if (this.hotReload) {
this.hotReloadScript = readFileSync(`${__dirname}/_hotReloadLocalesScript.js`, 'utf-8')
}

this.loadLocales()
}

Expand Down Expand Up @@ -132,6 +136,19 @@ class ProcessLocalesPlugin {
compilation.fileDependencies.addAll(this.filePaths)
}
})

compiler.hooks.emit.tap(PLUGIN_NAME, (compilation) => {
if (this.hotReload) {
// Find generated JavaScript output file (e.g. renderer.js or web.js)
// and inject the code snippet that listens for locale updates and replaces vue-i18n's locales

/** @type {string} */
const filename = [...[...compilation.chunks][0].files]
.find(file => file.endsWith('.js'))

compilation.assets[filename]._source._children.push(`\n${this.hotReloadScript}`)
}
})
}

loadLocales() {
Expand Down
18 changes: 18 additions & 0 deletions _scripts/_hotReloadLocalesScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const websocket = new WebSocket('ws://localhost:9080/ws')

websocket.onmessage = (event) => {
const message = JSON.parse(event.data)

if (message.type === 'freetube-locale-update') {
const i18n = document.getElementById('app').__vue__.$i18n
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's so much I don't know. This is so cool. Side tip I found from looking this up for @ChunkyProgrammer: the Vue 3 equivalent is apparently __vueParentComponent.ctx.


for (const [locale, data] of message.data) {
// Only update locale data if it was already loaded
if (i18n.availableLocales.includes(locale)) {
const localeData = JSON.parse(data)

i18n.setLocaleMessage(locale, localeData)
}
}
}
}
19 changes: 0 additions & 19 deletions src/renderer/i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,4 @@ export async function loadLocale(locale) {
i18n.setLocaleMessage(locale, data)
}

if (process.env.NODE_ENV === 'development') {
const websocket = new WebSocket('ws://localhost:9080/ws')

websocket.onmessage = (event) => {
const message = JSON.parse(event.data)

if (message.type === 'freetube-locale-update') {
for (const [locale, data] of message.data) {
// Only update locale data if it was already loaded
if (i18n.availableLocales.includes(locale)) {
const localeData = JSON.parse(data)

i18n.setLocaleMessage(locale, localeData)
}
}
}
}
}

export default i18n