Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions src/core/i18n/middleware.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/core/i18n/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ export function isRtlLang(lang) {
export function getDirection(lang) {
return isRtlLang(lang) ? 'rtl' : 'ltr';
}

export function getLangFromRouter(renderProps) {
if (renderProps) {
// Get the lang from the url param by default
// if it exists.
if (renderProps.params && renderProps.params.lang) {
return getLanguage(renderProps.params.lang);
} else if (renderProps.location && renderProps.location.query &&
renderProps.location.query.lang) {
return getLanguage(renderProps.location.query.lang);
}
}
return defaultLang;
}
7 changes: 2 additions & 5 deletions src/core/server/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import ServerHtml from 'core/containers/ServerHtml';
import config from 'config';
import { setJWT } from 'core/actions';
import log from 'core/logger';
import setLanguage from 'core/i18n/middleware';
import { getDirection, langToLocale } from 'core/i18n/utils';
import { getDirection, getLangFromRouter, langToLocale } from 'core/i18n/utils';
import I18nProvider from 'core/i18n/Provider';
import Jed from 'jed';

Expand All @@ -45,8 +44,6 @@ function baseServer(routes, createStore, { appInstanceName = appName } = {}) {
const app = new Express();
app.disable('x-powered-by');

app.use(setLanguage);

app.use(logRequests);

// Sets X-Frame-Options
Expand Down Expand Up @@ -114,7 +111,7 @@ function baseServer(routes, createStore, { appInstanceName = appName } = {}) {
fs.readFileSync(path.join(config.get('basePath'), 'dist/sri.json'))
) : {};

const lang = res.locals.lang;
const lang = getLangFromRouter(renderProps);
const dir = getDirection(lang);
const locale = langToLocale(lang);

Expand Down
2 changes: 1 addition & 1 deletion src/disco/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default (
<Route path="/" component={App}>
<IndexRoute component={DiscoPane} />
<Route
path="/:locale/firefox/discovery/pane/:version/:platform/:compatibilityMode"
path="/:lang/firefox/discovery/pane/:version/:platform/:compatibilityMode"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is technically a locale since we accept en-US and pt-BR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout all the i18n scripts a lang is formatted consistently as en-US and a locale is en_US.

The lang format is what is used for the lang attribute. The locale format is used for translations and the directories of translations. All the utils provide can convert from lang to locale.

Since the url input is hyphenated it's called a lang in this case.

component={DiscoPane}
/>
</Route>
Expand Down
58 changes: 0 additions & 58 deletions tests/client/core/i18n/test_middleware.js

This file was deleted.

42 changes: 42 additions & 0 deletions tests/client/core/i18n/test_utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as utils from 'core/i18n/utils';
import config from 'config';


describe('i18n utils', () => {
Expand Down Expand Up @@ -142,4 +143,45 @@ describe('i18n utils', () => {
assert.equal(utils.isValidLang('pt'), true);
});
});

describe('getLangFromRouter()', () => {
it('should return default lang if no lang is provided', () => {
const fakeRenderProps = {};
assert.equal(utils.getLangFromRouter(fakeRenderProps), config.get('defaultLang'));
});

it('should return lang if provided via the URL', () => {
const fakeRenderProps = {
params: {
lang: 'fr',
},
};
assert.equal(utils.getLangFromRouter(fakeRenderProps), 'fr');
});

it('should return lang if provided via a query param', () => {
const fakeRenderProps = {
location: {
query: {
lang: 'pt-PT',
},
},
};
assert.equal(utils.getLangFromRouter(fakeRenderProps), 'pt-PT');
});

it('should use url param if both that and query string are present', () => {
const fakeRenderProps = {
params: {
lang: 'fr',
},
location: {
query: {
lang: 'pt-PT',
},
},
};
assert.equal(utils.getLangFromRouter(fakeRenderProps), 'fr');
});
});
});