@lingui/core 4.7.0 → 4.8.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +31 -24
- package/dist/index.mjs +31 -20
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -3,10 +3,6 @@
|
|
|
3
3
|
const unraw = require('unraw');
|
|
4
4
|
const compileMessage = require('@lingui/message-utils/compileMessage');
|
|
5
5
|
|
|
6
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
7
|
-
|
|
8
|
-
const unraw__default = /*#__PURE__*/_interopDefaultCompat(unraw);
|
|
9
|
-
|
|
10
6
|
const isString = (s) => typeof s === "string";
|
|
11
7
|
const isFunction = (f) => typeof f === "function";
|
|
12
8
|
|
|
@@ -66,6 +62,7 @@ const formats = {
|
|
|
66
62
|
};
|
|
67
63
|
|
|
68
64
|
const UNICODE_REGEX = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g;
|
|
65
|
+
const OCTOTHORPE_PH = "%__lingui_octothorpe__%";
|
|
69
66
|
const getDefaultFormats = (locale, passedLocales, formats = {}) => {
|
|
70
67
|
const locales = passedLocales || locale;
|
|
71
68
|
const style = (format) => {
|
|
@@ -74,7 +71,7 @@ const getDefaultFormats = (locale, passedLocales, formats = {}) => {
|
|
|
74
71
|
const replaceOctothorpe = (value, message) => {
|
|
75
72
|
const numberFormat = Object.keys(formats).length ? style("number") : void 0;
|
|
76
73
|
const valueStr = number(locales, value, numberFormat);
|
|
77
|
-
return message.replace("
|
|
74
|
+
return message.replace(new RegExp(OCTOTHORPE_PH, "g"), valueStr);
|
|
78
75
|
};
|
|
79
76
|
return {
|
|
80
77
|
plural: (value, cases) => {
|
|
@@ -89,40 +86,50 @@ const getDefaultFormats = (locale, passedLocales, formats = {}) => {
|
|
|
89
86
|
},
|
|
90
87
|
select: selectFormatter,
|
|
91
88
|
number: (value, format) => number(locales, value, style(format)),
|
|
92
|
-
date: (value, format) => date(locales, value, style(format))
|
|
93
|
-
undefined: undefinedFormatter
|
|
89
|
+
date: (value, format) => date(locales, value, style(format))
|
|
94
90
|
};
|
|
95
91
|
};
|
|
96
92
|
const selectFormatter = (value, rules) => rules[value] ?? rules.other;
|
|
97
|
-
const undefinedFormatter = (value) => value;
|
|
98
93
|
function interpolate(translation, locale, locales) {
|
|
99
94
|
return (values = {}, formats) => {
|
|
100
95
|
const formatters = getDefaultFormats(locale, locales, formats);
|
|
101
|
-
const formatMessage = (
|
|
102
|
-
if (!Array.isArray(
|
|
103
|
-
return
|
|
104
|
-
return
|
|
105
|
-
if (
|
|
106
|
-
return
|
|
96
|
+
const formatMessage = (tokens) => {
|
|
97
|
+
if (!Array.isArray(tokens))
|
|
98
|
+
return tokens;
|
|
99
|
+
return tokens.reduce((message, token) => {
|
|
100
|
+
if (token === "#") {
|
|
101
|
+
return message + OCTOTHORPE_PH;
|
|
102
|
+
}
|
|
103
|
+
if (isString(token)) {
|
|
104
|
+
return message + token;
|
|
105
|
+
}
|
|
107
106
|
const [name, type, format] = token;
|
|
108
107
|
let interpolatedFormat = {};
|
|
109
|
-
if (
|
|
110
|
-
Object.entries(format).forEach(
|
|
111
|
-
|
|
112
|
-
|
|
108
|
+
if (type === "plural" || type === "selectordinal" || type === "select") {
|
|
109
|
+
Object.entries(format).forEach(
|
|
110
|
+
([key, value2]) => {
|
|
111
|
+
interpolatedFormat[key] = formatMessage(value2);
|
|
112
|
+
}
|
|
113
|
+
);
|
|
113
114
|
} else {
|
|
114
115
|
interpolatedFormat = format;
|
|
115
116
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
117
|
+
let value;
|
|
118
|
+
if (type) {
|
|
119
|
+
const formatter = formatters[type];
|
|
120
|
+
value = formatter(values[name], interpolatedFormat);
|
|
121
|
+
} else {
|
|
122
|
+
value = values[name];
|
|
123
|
+
}
|
|
124
|
+
if (value == null) {
|
|
125
|
+
return message;
|
|
126
|
+
}
|
|
127
|
+
return message + value;
|
|
121
128
|
}, "");
|
|
122
129
|
};
|
|
123
130
|
const result = formatMessage(translation);
|
|
124
131
|
if (isString(result) && UNICODE_REGEX.test(result)) {
|
|
125
|
-
return
|
|
132
|
+
return unraw.unraw(result.trim());
|
|
126
133
|
}
|
|
127
134
|
if (isString(result))
|
|
128
135
|
return result.trim();
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import unraw from 'unraw';
|
|
1
|
+
import { unraw } from 'unraw';
|
|
2
2
|
import { compileMessage } from '@lingui/message-utils/compileMessage';
|
|
3
3
|
|
|
4
4
|
const isString = (s) => typeof s === "string";
|
|
@@ -60,6 +60,7 @@ const formats = {
|
|
|
60
60
|
};
|
|
61
61
|
|
|
62
62
|
const UNICODE_REGEX = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g;
|
|
63
|
+
const OCTOTHORPE_PH = "%__lingui_octothorpe__%";
|
|
63
64
|
const getDefaultFormats = (locale, passedLocales, formats = {}) => {
|
|
64
65
|
const locales = passedLocales || locale;
|
|
65
66
|
const style = (format) => {
|
|
@@ -68,7 +69,7 @@ const getDefaultFormats = (locale, passedLocales, formats = {}) => {
|
|
|
68
69
|
const replaceOctothorpe = (value, message) => {
|
|
69
70
|
const numberFormat = Object.keys(formats).length ? style("number") : void 0;
|
|
70
71
|
const valueStr = number(locales, value, numberFormat);
|
|
71
|
-
return message.replace("
|
|
72
|
+
return message.replace(new RegExp(OCTOTHORPE_PH, "g"), valueStr);
|
|
72
73
|
};
|
|
73
74
|
return {
|
|
74
75
|
plural: (value, cases) => {
|
|
@@ -83,35 +84,45 @@ const getDefaultFormats = (locale, passedLocales, formats = {}) => {
|
|
|
83
84
|
},
|
|
84
85
|
select: selectFormatter,
|
|
85
86
|
number: (value, format) => number(locales, value, style(format)),
|
|
86
|
-
date: (value, format) => date(locales, value, style(format))
|
|
87
|
-
undefined: undefinedFormatter
|
|
87
|
+
date: (value, format) => date(locales, value, style(format))
|
|
88
88
|
};
|
|
89
89
|
};
|
|
90
90
|
const selectFormatter = (value, rules) => rules[value] ?? rules.other;
|
|
91
|
-
const undefinedFormatter = (value) => value;
|
|
92
91
|
function interpolate(translation, locale, locales) {
|
|
93
92
|
return (values = {}, formats) => {
|
|
94
93
|
const formatters = getDefaultFormats(locale, locales, formats);
|
|
95
|
-
const formatMessage = (
|
|
96
|
-
if (!Array.isArray(
|
|
97
|
-
return
|
|
98
|
-
return
|
|
99
|
-
if (
|
|
100
|
-
return
|
|
94
|
+
const formatMessage = (tokens) => {
|
|
95
|
+
if (!Array.isArray(tokens))
|
|
96
|
+
return tokens;
|
|
97
|
+
return tokens.reduce((message, token) => {
|
|
98
|
+
if (token === "#") {
|
|
99
|
+
return message + OCTOTHORPE_PH;
|
|
100
|
+
}
|
|
101
|
+
if (isString(token)) {
|
|
102
|
+
return message + token;
|
|
103
|
+
}
|
|
101
104
|
const [name, type, format] = token;
|
|
102
105
|
let interpolatedFormat = {};
|
|
103
|
-
if (
|
|
104
|
-
Object.entries(format).forEach(
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
if (type === "plural" || type === "selectordinal" || type === "select") {
|
|
107
|
+
Object.entries(format).forEach(
|
|
108
|
+
([key, value2]) => {
|
|
109
|
+
interpolatedFormat[key] = formatMessage(value2);
|
|
110
|
+
}
|
|
111
|
+
);
|
|
107
112
|
} else {
|
|
108
113
|
interpolatedFormat = format;
|
|
109
114
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
let value;
|
|
116
|
+
if (type) {
|
|
117
|
+
const formatter = formatters[type];
|
|
118
|
+
value = formatter(values[name], interpolatedFormat);
|
|
119
|
+
} else {
|
|
120
|
+
value = values[name];
|
|
121
|
+
}
|
|
122
|
+
if (value == null) {
|
|
123
|
+
return message;
|
|
124
|
+
}
|
|
125
|
+
return message + value;
|
|
115
126
|
}, "");
|
|
116
127
|
};
|
|
117
128
|
const result = formatMessage(translation);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingui/core",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.8.0-next.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "I18n tools for javascript",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
],
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"@babel/runtime": "^7.20.13",
|
|
57
|
-
"@lingui/message-utils": "4.
|
|
57
|
+
"@lingui/message-utils": "^4.8.0-next.0",
|
|
58
58
|
"unraw": "^3.0.0"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@lingui/jest-mocks": "
|
|
61
|
+
"@lingui/jest-mocks": "^3.0.3",
|
|
62
62
|
"unbuild": "2.0.0"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "d085ac65d66ce6c2f866b703b6346e53ff0bf6b3"
|
|
65
65
|
}
|