UNPKG

1.65 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const messages = require('../messages/en.json');
18
19/**
20 * Dummy globalize replacement.
21 * @param {string} message The message.
22 * @return {function} A function for formatting the message.
23 * @private
24 */
25function messageFormatter(message) {
26 return function (inserts) {
27 let result = messages.en[message];
28 for (let key in inserts) {
29 result = result.replace(new RegExp(`\\{${key}\\}`, 'g'), inserts[key]);
30 }
31 return result;
32 };
33}
34
35/**
36 * Dummy globalize replacement.
37 * @param {string} message The message.
38 * @return {function} The formatted message.
39 * @private
40 */
41function formatMessage(message) {
42 return messages.en[message];
43}
44
45/**
46 * Dummy globalize replacement.
47 * @param {string} locale The locale.
48 * @return {Object} A mock globalize instance.
49 * @private
50 */
51function Globalize(locale) {
52 return {
53 messageFormatter: messageFormatter,
54 formatMessage: formatMessage
55 };
56}
57
58Globalize.messageFormatter = messageFormatter;
59Globalize.formatMessage = formatMessage;
60
61module.exports = Globalize;