From 6be1de94e0924c7cea4b207fc17c89fe36eb1214 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 25 Jul 2022 12:00:09 +0200 Subject: [PATCH] Bug 32030: I18N - Load de-DE, es-ES, fr-FR strings Signed-off-by: Jonathan Field Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- .../intranet-tmpl/prog/en/modules/erm/erm.tt | 2 ++ .../prog/js/vue/components/ERM/ERMMain.vue | 2 ++ .../intranet-tmpl/prog/js/vue/main-erm.ts | 8 +++-- misc/translate_json.pl | 35 +++++++++++++------ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/erm/erm.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/erm/erm.tt index 8264252376..225a5b2083 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/erm/erm.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/erm/erm.tt @@ -54,6 +54,8 @@ const ERMModule = [% IF Koha.Preference('ERMModule') %]true[% ELSE %]false[% END %]; + const lang = "[% lang || 'en' | html %]"; + [% Asset.js("js/vue/dist/main.js") | $raw %] diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/ERMMain.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/ERMMain.vue index f00550753f..bccf63e5fa 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/ERMMain.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/ERMMain.vue @@ -142,6 +142,7 @@ export default { vendorStore, erm_providers, ERMModule, + lang, } }, data() { @@ -151,6 +152,7 @@ export default { }, beforeCreate() { fetchVendors().then((vendors) => this.vendorStore.vendors = vendors) + this.$i18n.locale = this.lang }, components: { Breadcrumb, diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/main-erm.ts b/koha-tmpl/intranet-tmpl/prog/js/vue/main-erm.ts index 0f229789a0..7656bbfbe3 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/main-erm.ts +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/main-erm.ts @@ -18,8 +18,12 @@ import { useMainStore } from "./stores/main"; import { createI18n } from "vue-i18n"; +// FIXME How do we load the locale list? import * as en from "./locales/en.json"; // We could async the load here, see https://vue-i18n.intlify.dev/guide/advanced/lazy.html -const languages = { en }; +import * as de_DE from "./locales/de-DE.json"; +import * as es_ES from "./locales/es-ES.json"; +import * as fr_FR from "./locales/fr-FR.json"; +const languages = { en, "de-DE": de_DE, "es-ES": es_ES, "fr-FR": fr_FR }; const messages = Object.assign(languages); const i18n = createI18n({ locale: "en", messages }); @@ -28,7 +32,7 @@ const app = createApp(App) .use(router) .use(i18n) .component("font-awesome-icon", FontAwesomeIcon); -app.config.unwrapInjectedRef = true +app.config.unwrapInjectedRef = true; app.mount("#erm"); const mainStore = useMainStore(); const { removeMessages } = mainStore; diff --git a/misc/translate_json.pl b/misc/translate_json.pl index 67ecb77924..a074816714 100644 --- a/misc/translate_json.pl +++ b/misc/translate_json.pl @@ -2,30 +2,45 @@ use Modern::Perl; use File::Slurp qw( read_file ); use JSON qw( from_json to_json ); +use Encode qw( decode_utf8 ); -my $json = read_file('koha-tmpl/intranet-tmpl/prog/js/vue/locales/en.json'); +our $lang = $ARGV[0] || 'en'; +my $json = read_file(sprintf 'koha-tmpl/intranet-tmpl/prog/js/vue/locales/%s.json', $lang); +$lang =~ s|-.*||; my $h = from_json($json); +my $i; my $size = scalar keys %$h; my $translated = {}; while (my ($k, $v) = each %$h ){ + warn sprintf "%s - Translating string %s/%s\n", $lang, ++$i, $size; if ( ref($v) ) { for my $kk ( keys %$v ) { ( my $vv = $k ) =~ s|\s*$||; if ( $kk eq 'counter' ) { - $translated->{$k}->{counter} = "$vv \%{counter}"; + $translated->{$k}->{counter} = translate("$vv \%{counter}"); } elsif ( $kk eq 'id' ) { - $translated->{$k}->{id} = "$vv #\%{id}"; + $translated->{$k}->{id} = translate("$vv #\%{id}"); } else { die "INVALID structure with key " . $kk; } } - } else { - if ( $k =~ /^There are no/ ) { - $translated->{$k} = "$k." - } - else { - $translated->{$k} = $k - } + } elsif ( $k =~ /^There are no/ ) { + $translated->{$k} = translate("$k."); + } + else { + $translated->{$k} = translate($k); } } say to_json($translated, {utf8 => 1, pretty => 1, canonical => 1}); + +sub translate { + my ( $string) = @_; + my $translated = $string; + return "/" if $string eq "/"; + if ( $lang ne 'en' ) { + my $cmd = sprintf 'trans --brief :%s "%s"', $lang, $string; + $translated = decode_utf8 qx{$cmd}; + chomp $translated; + } + return $translated; +} -- 2.39.5