Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n #1

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions index.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"controls": [
{
"name": "translator",
"options": {
"url": "sv.json",
"intl": "sv"
}
},
{
"name": "home",
"options": {
Expand Down
1 change: 1 addition & 0 deletions src/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export { default as Splash } from './controls/splash';
export { default as Zoom } from './controls/zoom';
export { default as Externalurl } from './controls/externalurl';
export { default as Scalepicker } from './controls/scalepicker';
export { default as Translator } from './controls/translation/component';
43 changes: 43 additions & 0 deletions src/controls/translation/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Component } from '../../ui';
import { TranslationModel } from './model';
import Intl from './intl';

const Translator = function Translator(options = {}) {
const {
url = '',
intl = ''
} = options;

// Get the translation json config
function getTranslation() {
fetch(options.url)
.then((res) => res.json())
.then((translation) => {
// Bind response to model
new TranslationModel(translation);
})
.catch(() => {
console.error('Could not load translation json.');
})
}
getTranslation()

return Component({
name: 'translator',
onAdd() {

// Find out what lang the user wants for the internationalization API
const lang = navigator.languages[0].substr(0, 2);
const opt = options;
const langOption = opt.intl ? opt.intl : lang;

const date = Intl.date(langOption, new Date());
const number = Intl.number(langOption, 123456.789);

// A dispatch is mandatory
this.dispatch('render');
}
});
};

export default Translator;
20 changes: 20 additions & 0 deletions src/controls/translation/intl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Helper methods for ECMAScript Internationalization API
*
*/
const intl = {
date: (lang, date) => {
const options = {
day: 'numeric',
month: 'long',
weekday: 'short',
hour: 'numeric',
minute: 'numeric'
};
return new Intl.DateTimeFormat([lang, 'en-EN'], options).format(date);
},
number: (lang, number) => {
return new Intl.NumberFormat([lang, 'en-EN']).format(number);
}
}
export default intl;
10 changes: 10 additions & 0 deletions src/controls/translation/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

class TranslationModel {
constructor(translationRespons) {
this.maxLengthText = translationRespons.controls.editor.editform.maxLengthText
// console.log(this.maxLengthText);
}
}
export {
TranslationModel
}
24 changes: 24 additions & 0 deletions sv.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"controls": {
"editor": {
"editform": {
"maxLengthText": "Max antal tecken i editform",
"firstOptionText": "Välj"
},
"edithandler": {
"maxLengthText": "",
"firstOptionText": ""
}
},
"templates": {
"editform": {
"maxLengthText": "",
"firstOptionText": ""
},
"edithandler": {
"maxLengthText": "",
"firstOptionText": ""
}
}
}
}