Detect supported lang based on files in I18N

This commit is contained in:
Daniel Berteaud 2015-06-19 15:54:28 +02:00
parent 4ae712f060
commit 2d8a599cc6
2 changed files with 10 additions and 7 deletions

View File

@ -30,7 +30,7 @@
<% } %>
<li class="hidden-xs">
<select class="form-control navbar-btn" id="switch_lang">
<% foreach my $lang (qw(en fr)){ %>
<% foreach my $lang ($self->get_supported_lang){ %>
<option value="<%= $lang %>" <%= ($self->languages eq $lang) ? 'selected="selected"' : '' %>>
<%= $lang %>
</option>

View File

@ -22,6 +22,7 @@ use Email::Valid;
use Protocol::SocketIO::Handshake;
use Protocol::SocketIO::Message;
use File::Path qw(make_path);
use File::Basename;
use Data::Dumper;
app->log->level('info');
@ -57,15 +58,11 @@ our $error = undef;
# Global peers hash
our $peers = {};
# Load I18N, and declare supported languages
# Initialize localization
plugin I18N => {
namespace => 'Vroom::I18N',
};
# Supported languages must be declared here
# Used to generate the dropdown menu
our @supported_lang = qw(en fr);
# Connect to the database
# Only MySQL supported for now
plugin database => {
@ -453,6 +450,12 @@ helper update_room_last_activity => sub {
return 1;
};
# Return an array of supported languages
helper get_supported_lang => sub {
my $self = shift;
return map { basename(s/\.pm$//r) } glob('lib/Vroom/I18N/*.pm');
};
# Generate a random token
helper get_random => sub {
my $self = shift;
@ -1408,7 +1411,7 @@ any '/api' => sub {
}
# Handle requests authorized for anonymous users righ now
if ($req->{action} eq 'switch_lang'){
if (!grep { $req->{param}->{language} eq $_ } @supported_lang){
if (!grep { $req->{param}->{language} eq $_ } $self->get_supported_lang()){
return $self->render(
json => {
msg => $self->l('UNSUPPORTED_LANG'),