/i18nMixin.js Secret
Created
September 14, 2016 11:27
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
i18nVars: {}, | |
setI18nVar: function(name, value) { | |
this.i18nVars[name] = value; | |
}, | |
iterateComponents: function(component, callback) { | |
if (!callback(component)) { | |
if (component = component._owner) { | |
this.iterateComponents(component, callback); | |
} | |
} | |
}, | |
getI18nVar: function(name) { | |
var value; | |
this.iterateComponents(this, function(component) { | |
if (component.i18nVars && (value = component.i18nVars[name])) { | |
return true; | |
} | |
}); | |
return value; | |
}, | |
t: function() { | |
var Connection = this.props.Connection; | |
var props = arguments[0]; | |
props.component = this; | |
return Connection.I18n.t.apply(this, arguments) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsx React.DOM */ | |
var TypedNumber = require('kp/commonWidgets/TypedNumber'); | |
module.exports = { | |
// datepicker | |
MyPicker: { | |
previousMonth : 'предыдущий месяц', | |
nextMonth : 'следующий месяц', | |
months : ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'], | |
weekdays : ['Воскресенье','Понедельник','Вторник','Среда','Четверг','Пятница','Суббота'], | |
weekdaysShort : ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'] | |
}, | |
// common phrases | |
Kp: { | |
moreDetails: 'Подробнее', | |
continue: 'Продолжить покупку', | |
forExample: 'Например', | |
goBack: 'Назад', | |
goNext: 'Далее', | |
economyClass: 'эконом', | |
businessClass: 'бизнес', | |
inNewTab: 'в новой вкладке', | |
apply: 'применить', | |
find: 'найти', | |
attention: 'Внимание!', | |
priceValue: function(props) { | |
var currencyKoef = this.getI18nVar('currencyKoef'); | |
return <span><TypedNumber value={Math.ceil(props.value * currencyKoef)} /><i className="fa fa-rub"></i></span> | |
}, | |
priceValueText: function(props) { | |
var currencyKoef = this.getI18nVar('currencyKoef'); | |
return <TypedNumber value={Math.ceil(props.value * currencyKoef)} one="рубль" few="рубля" many="рублей" /> | |
}, | |
bonusAmount: function(props) { | |
return <TypedNumber value={props.value} one="балл" few="балла" many="баллов" /> | |
}, | |
noTransfers: function() { | |
return <span>без пересадок</span> | |
}, | |
nTransfers: function(props) { | |
return <TypedNumber value={props.value} one="пересадка" few="пересадки" many="пересадок" /> | |
}, | |
nDays: function(props) { | |
return <TypedNumber value={props.value} one="день" few="дня" many="дней" /> | |
}, | |
additionalFare: function(props) { | |
var fare, req; | |
switch (props.value) { | |
case 'basic': | |
fare = 'Базовый'; | |
break; | |
case 'flex': | |
fare = 'Гибкий'; | |
break; | |
default: | |
fare = props.value; | |
} | |
switch (props.requestedCabinClass) { | |
case 'C': | |
req = 'бизнес'; | |
break; | |
default: | |
req = 'эконом'; | |
} | |
return fare + ' ' + req; | |
}, | |
additionalFareInfo: function(props) { | |
var t = this.t; | |
switch (props.requestedCabinClass) { | |
case 'C': | |
switch (props.value) { | |
case 'basic': | |
return ( | |
<div className="s7-info"> | |
<div className="s7-info--title">Базовый Бизнес</div> | |
<div className="mb5 fs12">Тариф невозвратный. Изменение в билете платное: <t key="priceValueText" value={5000} /> за сегмент</div> | |
<div className="mb5 fs12">Норма бесплатного провоза багажа для 1 пассажира: Ручная кладь 15 кг. Багаж 1 сумка 32 кг</div> | |
</div> | |
); | |
case 'flex': | |
return ( | |
<div className="s7-info"> | |
<div className="s7-info--title">Гибкий Бизнес</div> | |
<div className="mb5 fs12">Беслатный возврат билета. Изменение в билете бесплатное</div> | |
<div className="mb5 fs12">Норма бесплатного провоза багажа для 1 пассажира: Ручная кладь 15 кг. Багаж 2 сумки 32 кг</div> | |
</div> | |
); | |
default: | |
return null; | |
............................................ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(Connection) { | |
return function(props) { | |
var | |
component, | |
componentName, | |
localeForComponent, | |
localeForKey, | |
locale = Connection.I18n.currentLocale; | |
props.component.iterateComponents(props.component, function(component) { | |
componentName = component.constructor.displayName; | |
localeForComponent = locale[componentName]; | |
if (!localeForComponent) return false; | |
localeForKey = localeForComponent[props.key]; | |
if (localeForKey) return true; | |
}); | |
if (!localeForKey) { | |
return ['I18nMixin: no locale found in all components tree (from ', props.component.constructor.displayName, ' to ', componentName, ') for key ', props.key].join('') | |
} | |
if (localeForKey.call) { // function | |
return localeForKey.call(props.component, props); | |
} else { | |
return localeForKey; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment