Skip to content

Commit d49c3ba

Browse files
author
Jamie Curnow
committed
I18n support, fixed version in footer
1 parent c629deb commit d49c3ba

File tree

10 files changed

+91
-19
lines changed

10 files changed

+91
-19
lines changed

src/frontend/js/app/cache.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
const UserModel = require('../models/user');
44

55
let cache = {
6-
User: new UserModel.Model()
6+
User: new UserModel.Model(),
7+
locale: 'en',
8+
version: null
79
};
810

911
module.exports = cache;

src/frontend/js/app/i18n.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const Cache = ('./cache');
4+
const messages = require('../i18n/messages.json');
5+
6+
/**
7+
* @param {String} namespace
8+
* @param {String} key
9+
* @param {Object} [data]
10+
*/
11+
module.exports = function (namespace, key, data) {
12+
let locale = Cache.locale;
13+
// check that the locale exists
14+
if (typeof messages[locale] === 'undefined') {
15+
locale = 'en';
16+
}
17+
18+
if (typeof messages[locale][namespace] !== 'undefined' && typeof messages[locale][namespace][key] !== 'undefined') {
19+
return messages[locale][namespace][key](data);
20+
} else if (locale !== 'en' && typeof messages['en'][namespace] !== 'undefined' && typeof messages['en'][namespace][key] !== 'undefined') {
21+
return messages['en'][namespace][key](data);
22+
}
23+
24+
return 'INVALID I18N: ' + namespace + '/' + key;
25+
};

src/frontend/js/app/main.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ const Router = require('./router');
99
const Api = require('./api');
1010
const Tokens = require('./tokens');
1111
const UI = require('./ui/main');
12+
const i18n = require('./i18n');
1213

1314
const App = Mn.Application.extend({
1415

1516
Cache: Cache,
1617
Api: Api,
1718
UI: null,
1819
Controller: Controller,
19-
version: null,
2020

2121
region: {
2222
el: '#app',
2323
replaceElement: true
2424
},
2525

2626
onStart: function (app, options) {
27-
console.log('Welcome to Nginx Proxy Manager');
27+
console.log(i18n('main', 'welcome'));
2828

2929
// Check if token is coming through
3030
if (this.getParam('token')) {
@@ -34,12 +34,12 @@ const App = Mn.Application.extend({
3434
// Check if we are still logged in by refreshing the token
3535
Api.status()
3636
.then(result => {
37-
this.version = [result.version.major, result.version.minor, result.version.revision].join('.');
37+
Cache.version = [result.version.major, result.version.minor, result.version.revision].join('.');
3838
})
3939
.then(Api.Tokens.refresh)
4040
.then(this.bootstrap)
4141
.then(() => {
42-
console.info('You are logged in');
42+
console.info(i18n('main', 'logged-in', Cache.User.attributes));
4343
this.bootstrapTimer();
4444
this.refreshTokenTimer();
4545

@@ -60,7 +60,6 @@ const App = Mn.Application.extend({
6060
console.warn('Not logged in:', err.message);
6161
Controller.showLogin();
6262
});
63-
6463
},
6564

6665
History: {
@@ -86,7 +85,7 @@ const App = Mn.Application.extend({
8685
let ErrorView = Mn.View.extend({
8786
tagName: 'section',
8887
id: 'error',
89-
template: _.template('Error loading stuff. Please reload the app.')
88+
template: _.template(i18n('main', 'unknown-error'))
9089
});
9190

9291
this.getRegion().show(new ErrorView());
@@ -130,7 +129,7 @@ const App = Mn.Application.extend({
130129
Api.status()
131130
.then(result => {
132131
let version = [result.version.major, result.version.minor, result.version.revision].join('.');
133-
if (version !== this.version) {
132+
if (version !== Cache.version) {
134133
document.___location.reload();
135134
}
136135
})

src/frontend/js/app/ui/footer/main.ejs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
<div class="row align-items-center">
44
<div class="col-auto">
55
<ul class="list-inline list-inline-dots mb-0">
6-
<li class="list-inline-item"><a href="https://github.com/jc21/docker-registry-ui?utm_source=docker-registry-ui">Fork me on Github</a></li>
6+
<li class="list-inline-item"><a href="https://github.com/jc21/nginx-proxy-manager?utm_source=nginx-proxy-manager"><%- i18n('footer', 'fork-me') %></a></li>
77
</ul>
88
</div>
99
</div>
1010
</div>
1111
<div class="col-12 col-lg-auto mt-3 mt-lg-0 text-center">
12-
v<%- getVersion() %> &copy; 2018 <a href="https://jc21.com?utm_source=nginx-proxy-manager" target="_blank">jc21.com</a>. Theme by <a href="https://tabler.github.io/?utm_source=nginx-proxy-manager" target="_blank">Tabler</a>
12+
<%- i18n('footer', 'version', {version: getVersion()}) %>
13+
<%= i18n('footer', 'copy', {url: 'https://jc21.com?utm_source=nginx-proxy-manager'}) %>
14+
<%= i18n('footer', 'theme', {url: 'https://tabler.github.io/?utm_source=nginx-proxy-manager'}) %>
1315
</div>
1416
</div>

src/frontend/js/app/ui/footer/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
const Mn = require('backbone.marionette');
44
const template = require('./main.ejs');
5-
const App = require('../../main');
5+
const Cache = require('../../cache');
66

77
module.exports = Mn.View.extend({
88
className: 'container',
99
template: template,
1010

1111
templateContext: {
1212
getVersion: function () {
13-
return App.version;
13+
return Cache.version || '0.0.0';
1414
}
1515
}
1616
});

src/frontend/js/app/ui/header/main.ejs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<div class="container">
22
<div class="d-flex">
33
<a class="navbar-brand" href="/">
4-
<img src="/images/favicons/favicon-32x32.png" border="0"> &nbsp; Nginx Proxy Manager
4+
<img src="/images/favicons/favicon-32x32.png" border="0"> &nbsp; <%- i18n('main', 'app') %>
55
</a>
66

77
<div class="d-flex order-lg-2 ml-auto">
88
<div class="dropdown">
99
<a href="#" class="nav-link pr-0 leading-none" data-toggle="dropdown">
1010
<span class="avatar" style="background-image: url(<%- getUserField('avatar', '/images/default-avatar.jpg') %>)"></span>
1111
<span class="ml-2 d-none d-lg-block">
12-
<span class="text-default"><%- getUserField('nickname', null) || getUserField('name', 'Unknown User') %></span>
12+
<span class="text-default"><%- getUserField('nickname', null) || getUserField('name', i18n('main', 'unknown-user')) %></span>
1313
<small class="text-muted d-block mt-1"><%- getRole() %></small>
1414
</span>
1515
</a>
1616
<div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
1717
<a class="dropdown-item edit-details" href="#">
18-
<i class="dropdown-icon fe fe-user"></i> Edit Details
18+
<i class="dropdown-icon fe fe-user"></i> <%- i18n('user', 'edit-details') %>
1919
</a>
2020
<a class="dropdown-item change-password" href="#">
21-
<i class="dropdown-icon fe fe-lock"></i> Change Password
21+
<i class="dropdown-icon fe fe-lock"></i> <%- i18n('user', 'change-password') %>
2222
</a>
2323
<div class="dropdown-divider"></div>
2424
<a class="dropdown-item logout" href="/logout">

src/frontend/js/app/ui/header/main.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const $ = require('jquery');
44
const Mn = require('backbone.marionette');
5+
const i18n = require('../../i18n');
56
const Cache = require('../../cache');
67
const Controller = require('../../controller');
78
const Tokens = require('../../tokens');
@@ -50,15 +51,15 @@ module.exports = Mn.View.extend({
5051
},
5152

5253
getRole: function () {
53-
return Cache.User.isAdmin() ? 'Administrator' : 'Apache Helicopter';
54+
return i18n('roles', Cache.User.isAdmin() ? 'admin' : 'user');
5455
},
5556

5657
getLogoutText: function () {
5758
if (Tokens.getTokenCount() > 1) {
58-
return 'Sign back in as ' + Tokens.getNextTokenName();
59+
return i18n('main', 'sign-in-as', {name: Tokens.getNextTokenName()});
5960
}
6061

61-
return 'Sign out';
62+
return i18n('main', 'sign-out');
6263
}
6364
},
6465

src/frontend/js/i18n/messages.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"en": {
3+
"main": {
4+
"app": "Nginx Proxy Manager",
5+
"welcome": "Welcome to Nginx Proxy Manager",
6+
"logged-in": "You are logged in as {name}",
7+
"unknown-error": "Error loading stuff. Please reload the app.",
8+
"unknown-user": "Unknown User",
9+
"sign-out": "Sign out",
10+
"sign-in-as": "Sign back in as {name}"
11+
},
12+
"user": {
13+
"edit-details": "Edit Details",
14+
"change-password": "Change Password"
15+
},
16+
"roles": {
17+
"admin": "Administrator",
18+
"user": "Apache Helicopter"
19+
},
20+
"footer": {
21+
"fork-me": "Fork me on Github",
22+
"copy": "&copy; 2018 <a href=\"{url}\" target=\"_blank\">jc21.com</a>.",
23+
"version": "v{version}",
24+
"theme": "Theme by <a href=\"{url}\" target=\"_blank\">Tabler</a>"
25+
}
26+
}
27+
}

src/frontend/js/lib/marionette.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ const _ = require('underscore');
44
const Mn = require('backbone.marionette');
55
const moment = require('moment');
66
const numeral = require('numeral');
7+
const i18n = require('../app/i18n');
78

89
let render = Mn.Renderer.render;
910

1011
Mn.Renderer.render = function (template, data, view) {
1112

1213
data = _.clone(data);
1314

15+
data.i18n = i18n;
16+
1417
/**
1518
* @param {Integer} number
1619
* @returns {String}

webpack.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ module.exports = {
4545
},
4646

4747
// other:
48+
{
49+
type: 'javascript/auto', // <= Set the module.type explicitly
50+
test: /\bmessages\.json$/,
51+
loader: 'messageformat-loader',
52+
options: {
53+
biDiSupport: false,
54+
disablePluralKeyChecks: false,
55+
formatters: null,
56+
intlSupport: false,
57+
locale: ['en'/*, 'es'*/],
58+
strictNumberSign: false
59+
}
60+
},
4861
{
4962
test: /\.js$/,
5063
exclude: /node_modules/,

0 commit comments

Comments
 (0)