integrate string formating functionality

This commit is contained in:
Tim Daubenschütz 2015-06-02 13:31:12 +02:00
parent a19ea989ef
commit 68be98c3f0
7 changed files with 52 additions and 14 deletions

View File

@ -15,7 +15,7 @@ class PieceActions {
this.actions.updatePiece(res.piece);
})
.catch((err) => {
console.log(err);
console.log(err);
});
}
}

View File

@ -14,7 +14,6 @@ class PieceListActions {
PieceListFetcher
.fetch(page, pageSize, search, orderBy, orderAsc)
.then((res) => {
console.log(res);
this.actions.updatePieceList({
page,
pageSize,

View File

@ -5,6 +5,8 @@ import TableItem from '../ascribe_table/table_item';
import TableColumnContentModel from '../../models/table_column_content_model';
import getLangText from '../../utils/lang_utils';
let AccordionListItemTable = React.createClass({
getInitialState() {
return {
@ -78,7 +80,7 @@ let AccordionListItemTableToggle = React.createClass({
<span
className={this.props.className}
onClick={this.props.onClick}>
{this.props.show ? 'Hide all ' + this.props.numOfTableItems + ' Editions' : 'Show all ' + this.props.numOfTableItems + ' Editions'}
{this.props.show ? getLangText('Hide all %d Editions', this.props.numOfTableItems) : getLangText('Show all %d Editions', this.props.numOfTableItems)}
</span>
);
}

View File

@ -12,7 +12,7 @@ import TableItemText from '../ascribe_table/table_item_text';
import TableItemCheckbox from '../ascribe_table/table_item_checkbox';
import TableItemAclFiltered from '../ascribe_table/table_item_acl_filtered';
import getText from '../../utils/lang_utils';
import getLangText from '../../utils/lang_utils';
let AccordionListItemTableEditions = React.createClass({
@ -50,8 +50,8 @@ let AccordionListItemTableEditions = React.createClass({
let columnList = [
new TableColumnContentModel((item) => { return { 'editionId': item.id, 'pieceId': this.props.parentId, 'selectItem': this.selectItem, 'selected': item.selected }}, '', '', TableItemCheckbox, 1, false),
new TableColumnContentModel((item) => { return { 'content': item.edition_number }}, 'num_editions', '#', TableItemText, 1, false),
new TableColumnContentModel((item) => { return { 'content': item.bitcoin_id }}, 'bitcoin_id', getText('Bitcoin Address'), TableItemText, 5, false),
new TableColumnContentModel((item) => { return { 'content': item.acl }}, 'acl', 'Actions', TableItemAclFiltered, 4, false)
new TableColumnContentModel((item) => { return { 'content': item.bitcoin_id }}, 'bitcoin_id', getLangText('Bitcoin Address'), TableItemText, 5, false),
new TableColumnContentModel((item) => { return { 'content': item.acl }}, 'acl', getLangText('Actions'), TableItemAclFiltered, 4, false)
];
return (

View File

@ -1,7 +1,9 @@
const languages = {
'en-US': {
'Bitcoin Address': 'Bitcoin Address',
'Actions': 'Actions'
'Actions': 'Actions',
'Hide all %d Editions': 'Hide all %d Editions',
'Show all %d Editions': 'Show all %d Editions'
}
};

View File

@ -34,8 +34,41 @@ let GeneralUtils = {
let sum = 0;
l.forEach((num) => sum += parseFloat(num) || 0);
return sum;
}
},
/*
Taken from http://stackoverflow.com/a/4795914/1263876
Behaves like C's format string function
Does not throw errors though if a argument's type is not
matching its template's representative!!!
This essentially means you can use %d or %s for anything...
*/
formatText() {
var args = arguments,
string = args[0],
i = 1;
return string.replace(/%((%)|s|d)/g, function (m) {
// m is the matched format, e.g. %s, %d
var val = null;
if (m[2]) {
val = m[2];
} else {
val = args[i];
// A switch statement so that the formatter can be extended. Default is %s
switch (m) {
case '%d':
val = parseFloat(val);
if (isNaN(val)) {
val = 0;
}
break;
}
i++;
}
return val;
});
}
};
export default GeneralUtils;

View File

@ -1,18 +1,20 @@
import languages from '../constants/languages';
import template from 'lodash.template';
let getText = function(s, ...args) {
import GeneralUtils from './general_utils';
let getLangText = function(s, ...args) {
let lang = navigator.language || navigator.userLanguage;
try {
if(lang in languages) {
return languages[lang][s];
return GeneralUtils.formatText(languages[lang][s], args);
} else {
// just use the english language
return languages['en-US'][s];
return GeneralUtils.formatText(languages['en-US'][s], args);
}
} catch(err) {
console.error(err);
console.error(new Error('Language-string is not in constants file.'));
}
};
export default getText;
export default getLangText;