diff --git a/public/js/vroom.js b/public/js/vroom.js index 3505a1f..17879ec 100644 --- a/public/js/vroom.js +++ b/public/js/vroom.js @@ -36,10 +36,6 @@ $('.date-picker').datepicker({ language: currentLang }); -// Strings we need translated -var locale = {}, - def_locale = {}; - // When pagination is done, how many item per page var itemPerPage = 20; @@ -67,18 +63,6 @@ var peers = { // Mark current page link as active $('#lnk_' + page).addClass('active'); -// Localize the strings we need -$.getJSON(rootUrl + 'localize/' + currentLang, function(data){ - locale = data; -}); - -// If current locale isn't EN, retrieve EN locale as a fallback -if (currentLang !== 'en'){ - $.getJSON(rootUrl + 'localize/en' , function(data){ - def_locale = data; - }); -} - // Default ajax setup $.ajaxSetup({ url: rootUrl + 'api', @@ -99,8 +83,8 @@ function localize(string){ if (locale[string]){ return locale[string]; } - else if (def_locale[string]){ - return def_locale[string]; + else if (fallback_locale[string]){ + return fallback_locale[string]; } return string; } diff --git a/templates/default/js_common.html.ep b/templates/default/js_common.html.ep index 51e4788..4879147 100644 --- a/templates/default/js_common.html.ep +++ b/templates/default/js_common.html.ep @@ -7,6 +7,7 @@ var page = '<%= stash('page') ? stash('page') : "" %>'; var roomName; + <% my @js = qw(jquery-1.11.3.js bootstrap.js notify-combined.js bootstrap-switch.js jquery.bootpag.js toc.js bootstrap-datepicker.js); # Load supported languages for datpicker, if they exists diff --git a/vroom.pl b/vroom.pl index b96fb70..cf823b6 100755 --- a/vroom.pl +++ b/vroom.pl @@ -1572,17 +1572,35 @@ any [qw(GET POST)] => '/invitation/:token' => { token => '' } => sub { } }; -# Translation for JS resources -get '/localize/:lang' => { lang => 'en' } => sub { - my $self = shift; +# Create a json script which contains localization +get '/locales/(:lang).js' => { lang => 'en' } => sub { + my $self = shift; + my $usr_lang = $self->languages; + my $req_lang = $self->stash('lang'); + # Temporarily switch to the requested locale + # eg, we can be in en and ask for /locales/fr.js + $self->languages($req_lang); my $strings = {}; - my $l = $self->languages; - $self->languages($self->stash('lang')); + my $fallback_strings = {}; foreach my $string (keys %Vroom::I18N::fr::Lexicon){ + next if $string eq ''; $strings->{$string} = $self->l($string); } - $self->languages($l); - return $self->render(json => $strings); + # If lang is not en, send also en as a fallback locale + # useful if a locale is not complete + if ($req_lang ne 'en'){ + $self->languages('en'); + foreach my $string (keys %Vroom::I18N::fr::Lexicon){ + next if $string eq ''; + $fallback_strings->{$string} = $self->l($string); + } + } + # Set the user locale back + $self->languages($usr_lang); + my $res = 'locale = ' . Mojo::JSON::to_json($strings) . ';'; + $res .= 'fallback_locale = ' . Mojo::JSON::to_json($fallback_strings) . ';'; + # And send the response + $self->render(text => $res, format => 'application/javascript;charset=UTF-8'); }; # API requests handler