Skip to content

Commit ed442f6

Browse files
committed
Moved to the fluent library package
1 parent ad400cc commit ed442f6

File tree

9 files changed

+7366
-5046
lines changed

9 files changed

+7366
-5046
lines changed

.babelrc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
{
2-
"presets": [
3-
"env",
4-
"react"
5-
]
1+
{
2+
"presets": [
3+
"@babel/preset-env",
4+
"@babel/preset-react"
5+
]
66
}

build/react-l20n.js

Lines changed: 179 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,195 +1,206 @@
1-
'use strict';
1+
"use strict";
22

33
Object.defineProperty(exports, "__esModule", {
4-
value: true
4+
value: true
55
});
6-
exports.L20nElement = undefined;
6+
exports.default = exports.L20nElement = void 0;
77

8-
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8+
var _react = _interopRequireDefault(require("react"));
99

10-
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
10+
var _propTypes = _interopRequireDefault(require("prop-types"));
1111

12-
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
12+
var _fluent = require("fluent");
1313

14-
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); // react-l20n.js
15-
// version: 0.0.17
16-
// author: Marc Selman
17-
// license: MIT
14+
var _htmlReactParser = _interopRequireDefault(require("html-react-parser"));
1815

19-
var _react = require('react');
16+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2017

21-
var _react2 = _interopRequireDefault(_react);
18+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2219

23-
var _propTypes = require('prop-types');
20+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
2421

25-
var _propTypes2 = _interopRequireDefault(_propTypes);
22+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
2623

27-
require('l20n');
24+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
2825

29-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
27+
28+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
29+
30+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
31+
32+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
3033

31-
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
34+
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
3235

33-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
36+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
37+
38+
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
3439

3540
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3641

42+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
43+
44+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
45+
3746
var fsiCharacter = new RegExp(String.fromCharCode(8296), 'g');
3847
var psiCharacter = new RegExp(String.fromCharCode(8297), 'g');
3948

40-
var L20n = function () {
41-
function L20n() {
42-
_classCallCheck(this, L20n);
43-
44-
this.contexts = new Map();
45-
this.defaultLocale = 'en';
46-
this.fallbackToDefault = true;
47-
}
48-
49-
_createClass(L20n, [{
50-
key: 'load',
51-
value: function load(locale, ftl) {
52-
var ctx = this.contexts.get(locale);
53-
if (!ctx) {
54-
ctx = new Intl.MessageContext(locale);
55-
this.contexts.set(locale, ctx);
56-
}
57-
58-
var errors = ctx.addMessages(ftl);
59-
if (errors.length) {
60-
console.log(errors);
61-
}
62-
}
63-
}, {
64-
key: 'getRaw',
65-
value: function getRaw(key, props) {
66-
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.defaultLocale;
67-
68-
var ctx = this.contexts.get(locale);
69-
if (!ctx) {
70-
if (this.fallbackToDefault) {
71-
ctx = this.contexts.get(this.defaultLocale);
72-
}
73-
74-
if (!ctx) {
75-
return 'Locale \'' + locale + '\' missing';
76-
}
77-
}
78-
79-
var template = ctx.messages.get(key);
80-
if (template != undefined && (typeof template === 'undefined' ? 'undefined' : _typeof(template)) === 'object') {
81-
template = ctx.format(template, props);
82-
}
83-
84-
if (template == undefined || typeof template === 'undefined') {
85-
if (this.fallbackToDefault && ctx.lang != this.defaultLocale) {
86-
return this.getRaw(key, props, this.defaultLocale);
87-
}
88-
return undefined;
89-
} else if (typeof template === 'string') {
90-
return template.replace(fsiCharacter, '').replace(psiCharacter, '');
91-
} else {
92-
var _formatted = formatted,
93-
_formatted2 = _slicedToArray(_formatted, 2),
94-
message = _formatted2[0],
95-
errors = _formatted2[1];
96-
97-
if (errors.length > 0) {
98-
console.log(errors);
99-
return undefined;
100-
}
101-
102-
return message.replace(fsiCharacter, '').replace(psiCharacter, '');
103-
}
104-
}
105-
}, {
106-
key: 'get',
107-
value: function get(key, props) {
108-
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.defaultLocale;
109-
110-
var message = this.getRaw(key, props, locale);
111-
return _react2.default.createElement('span', { dangerouslySetInnerHTML: { __html: message } });
112-
}
113-
}, {
114-
key: 'getContext',
115-
value: function getContext() {
116-
var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.defaultLocale;
117-
118-
var ctx = this.contexts.get(locale);
119-
if (!ctx) {
120-
if (this.fallbackToDefault) {
121-
ctx = this.contexts.get(this.defaultLocale);
122-
}
123-
124-
if (!ctx) {
125-
return null;
126-
}
127-
}
128-
129-
return ctx;
130-
}
131-
}]);
132-
133-
return L20n;
49+
var L20n =
50+
/*#__PURE__*/
51+
function () {
52+
function L20n() {
53+
_classCallCheck(this, L20n);
54+
55+
this.contexts = new Map();
56+
this.defaultLocale = 'en';
57+
this.fallbackToDefault = true;
58+
}
59+
60+
_createClass(L20n, [{
61+
key: "load",
62+
value: function load(locale, ftl) {
63+
var ctx = this.contexts.get(locale);
64+
65+
if (!ctx) {
66+
ctx = new _fluent.FluentBundle(locale);
67+
this.contexts.set(locale, ctx);
68+
}
69+
70+
var errors = ctx.addMessages(ftl);
71+
72+
if (errors.length) {
73+
console.log(errors);
74+
}
75+
}
76+
}, {
77+
key: "getRaw",
78+
value: function getRaw(key, props) {
79+
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.defaultLocale;
80+
var ctx = this.contexts.get(locale);
81+
82+
if (!ctx) {
83+
if (this.fallbackToDefault) {
84+
ctx = this.contexts.get(this.defaultLocale);
85+
}
86+
87+
if (!ctx) {
88+
return "Locale '".concat(locale, "' missing");
89+
}
90+
}
91+
92+
var template = ctx.getMessage(key);
93+
94+
if (template != undefined && _typeof(template) === 'object') {
95+
template = ctx.format(template, props);
96+
}
97+
98+
if (template == undefined || typeof template === 'undefined') {
99+
if (this.fallbackToDefault && !ctx.locales.includes(this.defaultLocale)) {
100+
return this.getRaw(key, props, this.defaultLocale);
101+
}
102+
103+
return undefined;
104+
} else if (typeof template === 'string') {
105+
return template.replace(fsiCharacter, '').replace(psiCharacter, '');
106+
} else {
107+
var _formatted = formatted,
108+
_formatted2 = _slicedToArray(_formatted, 2),
109+
message = _formatted2[0],
110+
errors = _formatted2[1];
111+
112+
if (errors.length > 0) {
113+
console.log(errors);
114+
return undefined;
115+
}
116+
117+
return message.replace(fsiCharacter, '').replace(psiCharacter, '');
118+
}
119+
}
120+
}, {
121+
key: "get",
122+
value: function get(key, props) {
123+
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.defaultLocale;
124+
var message = this.getRaw(key, props, locale);
125+
return _react.default.createElement(_react.default.Fragment, null, (0, _htmlReactParser.default)(message));
126+
}
127+
}, {
128+
key: "getContext",
129+
value: function getContext() {
130+
var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.defaultLocale;
131+
var ctx = this.contexts.get(locale);
132+
133+
if (!ctx) {
134+
if (this.fallbackToDefault) {
135+
ctx = this.contexts.get(this.defaultLocale);
136+
}
137+
138+
if (!ctx) {
139+
return null;
140+
}
141+
}
142+
143+
return ctx;
144+
}
145+
}]);
146+
147+
return L20n;
134148
}();
135149

136-
var L20nElement = exports.L20nElement = function (_React$Component) {
137-
_inherits(L20nElement, _React$Component);
138-
139-
function L20nElement(props) {
140-
_classCallCheck(this, L20nElement);
141-
142-
return _possibleConstructorReturn(this, (L20nElement.__proto__ || Object.getPrototypeOf(L20nElement)).call(this, props));
143-
}
144-
145-
_createClass(L20nElement, [{
146-
key: 'render',
147-
value: function render() {
148-
var _this2 = this;
149-
150-
this.attrs = Object.assign({}, this.props);
151-
delete this.attrs['id'];
152-
delete this.attrs['renderAs'];
153-
delete this.attrs['locale'];
154-
delete this.attrs['elementRef'];
155-
156-
var value = l20n.getRaw(this.props.id, {}, this.props.locale);
157-
if (value) {
158-
this.attrs['defaultValue'] = value;
159-
}
160-
161-
var ctx = l20n.getContext(this.props.locale);
162-
if (ctx) {
163-
var template = ctx.messages.get(this.props.id);
164-
if (template && template.traits) {
165-
var attributes = template.traits.filter(function (t) {
166-
return t.key.ns == 'html';
167-
}).map(function (t) {
168-
return [t.key.name, t.val];
169-
});
170-
attributes.forEach(function (a) {
171-
_this2.attrs[a[0]] = a[1];
172-
});
173-
}
174-
}
175-
176-
return _react2.default.createElement(
177-
this.props.renderAs,
178-
_extends({ ref: this.props.elementRef }, this.attrs),
179-
this.props.children
180-
);
181-
}
182-
}]);
183-
184-
return L20nElement;
185-
}(_react2.default.Component);
150+
var L20nElement =
151+
/*#__PURE__*/
152+
function (_React$Component) {
153+
_inherits(L20nElement, _React$Component);
154+
155+
function L20nElement(props) {
156+
_classCallCheck(this, L20nElement);
157+
158+
return _possibleConstructorReturn(this, _getPrototypeOf(L20nElement).call(this, props));
159+
}
186160

161+
_createClass(L20nElement, [{
162+
key: "render",
163+
value: function render() {
164+
var _this = this;
165+
166+
this.attrs = Object.assign({}, this.props);
167+
delete this.attrs['id'];
168+
delete this.attrs['renderAs'];
169+
delete this.attrs['locale'];
170+
delete this.attrs['elementRef'];
171+
var value = l20n.getRaw(this.props.id, {}, this.props.locale);
172+
173+
if (value) {
174+
this.attrs['defaultValue'] = value;
175+
}
176+
177+
var ctx = l20n.getContext(this.props.locale);
178+
179+
if (ctx) {
180+
var template = ctx.getMessage(this.props.id);
181+
182+
if (template && template.attrs) {
183+
Object.entries(template.attrs).forEach(function (a) {
184+
_this.attrs[a[0]] = a[1];
185+
});
186+
}
187+
}
188+
189+
return _react.default.createElement(this.props.renderAs, _extends({
190+
ref: this.props.elementRef
191+
}, this.attrs), this.props.children);
192+
}
193+
}]);
194+
195+
return L20nElement;
196+
}(_react.default.Component);
197+
198+
exports.L20nElement = L20nElement;
187199
L20nElement.propTypes = {
188-
id: _propTypes2.default.string.isRequired,
189-
renderAs: _propTypes2.default.string.isRequired,
190-
locale: _propTypes2.default.string
200+
id: _propTypes.default.string.isRequired,
201+
renderAs: _propTypes.default.string.isRequired,
202+
locale: _propTypes.default.string
191203
};
192-
193204
var l20n = new L20n();
194-
195-
exports.default = l20n;
205+
var _default = l20n;
206+
exports.default = _default;

0 commit comments

Comments
 (0)