@crisgfx/kirbyup-plugin-i18n-updater
v1.0.0
Published
For kirby panel plugin devs: See instant updates of Your locale files edits.
Maintainers
Readme
i18nUpdater for kirbyup
A very basic Vite / kirbyup plugin for panel plugin devs (😱 Pluginception) to instantly show changes in the current view, whenever You edit Your plugin's locale files.
Installation
npm i -D @crisgfx/kirbyup-i18nUpdaterUsage
Register the plugin in Your kirbyup config file.
Add a source path to the folder that contains all language files (they must be .json files, i.e. en.json, de.json, etc...).
// `kirbyup.config.js`
import i18nUpdater from '@crisgfx/kirbyup-i18n-updater'
export default defineConfig({
vite: {
// ...
plugins: [
i18nUpdater({
source: 'src/i18n/'
})
],
// ...
}
});Then, in Your plugin's main entry point add the callback, that takes care of refreshing the current panel view:
// `index.js`
import { updateLocale } from '@crisgfx/kirbyup-i18n-updater/updateLocale'
// Listen for locale file changes in dev mode.
if (import.meta.env.DEV && import.meta.hot) {
import.meta.hot.on('i18n-update', updateLocale)
}
panel.plugin("florian-silberrücken/kirby-monkeyboy", {
// Plugin magic.
});[!TIP]
You probably already know how to do this, but I wanted to use Github's fancy alerts – so, here's a quick and dirty example on how to loadjsonfiles into Your translations array – though it might be better to write the code a bit more readable and outsource it to a dedicated file, in order to keep Your plugin structure clean and tidy.
<?php
// index.php
use Kirby\Cms\App;
use Kirby\Filesystem\Dir;
use Kirby\Toolkit\A;
use Kirby\Data\Json;
$sourceDir = __DIR__ . '/src/i18n';
App::plugin(
'florian-silberrücken/kirby-monkeyboy',
extends: [
// ...
'translations' => array_merge(
...A::map(Dir::files($sourceDir),
function ($file) use($sourceDir) {
if ($translations = Json::read($sourceDir . '/' . $file)) {
// Store translations in corresponding locale array.
return [basename($file, '.json') => $translations];
}
})
)
]
);NOTES
Thanks to @johannschopplich and @jonaskuske for creating kirbyup! ❤︎
