From 68be98c3f0957d8662f9b5f7dea86610c7e6fa29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Tue, 2 Jun 2015 13:31:12 +0200 Subject: [PATCH] integrate string formating functionality --- js/actions/piece_actions.js | 2 +- js/actions/piece_list_actions.js | 1 - .../accordion_list_item_table.js | 4 ++- .../accordion_list_item_table_editions.js | 6 ++-- js/constants/languages.js | 4 ++- js/utils/general_utils.js | 35 ++++++++++++++++++- js/utils/lang_utils.js | 14 ++++---- 7 files changed, 52 insertions(+), 14 deletions(-) diff --git a/js/actions/piece_actions.js b/js/actions/piece_actions.js index d657539b..274ef458 100644 --- a/js/actions/piece_actions.js +++ b/js/actions/piece_actions.js @@ -15,7 +15,7 @@ class PieceActions { this.actions.updatePiece(res.piece); }) .catch((err) => { - console.log(err); + console.log(err); }); } } diff --git a/js/actions/piece_list_actions.js b/js/actions/piece_list_actions.js index 4ec237ca..29c8413f 100644 --- a/js/actions/piece_list_actions.js +++ b/js/actions/piece_list_actions.js @@ -14,7 +14,6 @@ class PieceListActions { PieceListFetcher .fetch(page, pageSize, search, orderBy, orderAsc) .then((res) => { - console.log(res); this.actions.updatePieceList({ page, pageSize, diff --git a/js/components/ascribe_accordion_list/accordion_list_item_table.js b/js/components/ascribe_accordion_list/accordion_list_item_table.js index 9ddb3308..953fffa1 100644 --- a/js/components/ascribe_accordion_list/accordion_list_item_table.js +++ b/js/components/ascribe_accordion_list/accordion_list_item_table.js @@ -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({ - {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)} ); } diff --git a/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js b/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js index e18b7a3f..356e5fe0 100644 --- a/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js +++ b/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js @@ -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 ( diff --git a/js/constants/languages.js b/js/constants/languages.js index db14a490..c49caf53 100644 --- a/js/constants/languages.js +++ b/js/constants/languages.js @@ -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' } }; diff --git a/js/utils/general_utils.js b/js/utils/general_utils.js index 7ab180fa..14118d94 100644 --- a/js/utils/general_utils.js +++ b/js/utils/general_utils.js @@ -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; diff --git a/js/utils/lang_utils.js b/js/utils/lang_utils.js index a936294e..0eddcd30 100644 --- a/js/utils/lang_utils.js +++ b/js/utils/lang_utils.js @@ -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; \ No newline at end of file +export default getLangText; \ No newline at end of file