1
/*
2
 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
 * the License. A copy of the License is located at
6
 *
7
 *     http://aws.amazon.com/apache2.0/
8
 *
9
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
 * and limitations under the License.
12
 */
13

14 1
import { I18n as I18nClass } from './I18n';
15

16 1
import { ConsoleLogger as Logger } from '../Logger';
17 1
import { Amplify } from '../Amplify';
18

19 1
const logger = new Logger('I18n');
20

21 1
let _config = null;
22 1
let _i18n = null;
23

24
/**
25
 * Export I18n APIs
26
 */
27 1
export class I18n {
28
	/**
29
	 * @static
30
	 * @method
31
	 * Configure I18n part
32
	 * @param {Object} config - Configuration of the I18n
33
	 */
34 1
	static configure(config) {
35 1
		logger.debug('configure I18n');
36 1
		if (!config) {
37 0
			return _config;
38
		}
39

40 1
		_config = Object.assign({}, _config, config.I18n || config);
41

42 1
		I18n.createInstance();
43

44 1
		return _config;
45
	}
46

47 1
	static getModuleName() {
48 1
		return 'I18n';
49
	}
50

51
	/**
52
	 * @static
53
	 * @method
54
	 * Create an instance of I18n for the library
55
	 */
56 1
	static createInstance() {
57 1
		logger.debug('create I18n instance');
58 1
		if (_i18n) {
59 1
			return;
60
		}
61 1
		_i18n = new I18nClass(_config);
62
	}
63

64
	/**
65
	 * @static @method
66
	 * Explicitly setting language
67
	 * @param {String} lang
68
	 */
69 1
	static setLanguage(lang) {
70 0
		I18n.checkConfig();
71

72 0
		return _i18n.setLanguage(lang);
73
	}
74

75
	/**
76
	 * @static @method
77
	 * Get value
78
	 * @param {String} key
79
	 * @param {String} defVal - Default value
80
	 */
81 1
	static get(key, defVal?) {
82 1
		if (!I18n.checkConfig()) {
83 1
			return typeof defVal === 'undefined' ? key : defVal;
84
		}
85

86 0
		return _i18n.get(key, defVal);
87
	}
88

89
	/**
90
	 * @static
91
	 * @method
92
	 * Add vocabularies for one language
93
	 * @param {String} langurage - Language of the dictionary
94
	 * @param {Object} vocabularies - Object that has key-value as dictionary entry
95
	 */
96 1
	static putVocabulariesForLanguage(language, vocabularies) {
97 0
		I18n.checkConfig();
98

99 0
		return _i18n.putVocabulariesForLanguage(language, vocabularies);
100
	}
101

102
	/**
103
	 * @static
104
	 * @method
105
	 * Add vocabularies for one language
106
	 * @param {Object} vocabularies - Object that has language as key,
107
	 *                                vocabularies of each language as value
108
	 */
109 1
	static putVocabularies(vocabularies) {
110 0
		I18n.checkConfig();
111

112 0
		return _i18n.putVocabularies(vocabularies);
113
	}
114

115 1
	public static checkConfig() {
116 1
		if (!_i18n) {
117 0
			_i18n = new I18nClass(_config);
118
		}
119

120 0
		return true;
121
	}
122 1
}
123

124 1
Amplify.register(I18n);
125

126
/**
127
 * @deprecated use named import
128
 */
129 1
export default I18n;

Read our documentation on viewing source code .

Loading