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

Rewrite locale file updating to be more Vue 3 friendly #5660

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

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

this.loadLocales()
}

/** @param {import('webpack').Compiler} compiler */
apply(compiler) {
const { CachedSource, RawSource } = compiler.webpack.sources;
const { Compilation } = compiler.webpack
const { Compilation, DefinePlugin } = compiler.webpack

new DefinePlugin({
'process.env.HOT_RELOAD_LOCALES': this.hotReload
}).apply(compiler)

compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
const IS_DEV_SERVER = !!compiler.watching
Expand Down Expand Up @@ -136,19 +136,6 @@ 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: 0 additions & 18 deletions _scripts/_hotReloadLocalesScript.js

This file was deleted.

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

// Set by _scripts/ProcessLocalesPlugin.js
if (process.env.HOT_RELOAD_LOCALES) {
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