@tagplus/components 5.0.0 → 5.1.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.
@@ -1,2 +1,2 @@
1
- "use strict";(("undefined"!=typeof self?self:this).webpackChunktp=("undefined"!=typeof self?self:this).webpackChunktp||[]).push([[275],{48167:function(e,n,f){f.r(n);var l=f(33088);n.default={...l.default,lingua:"English",tp:{money:{currency:"$"}},locale:{name:"en",prefix:"en-US",number:{decimal:".",thousands:"",prefix:"",suffix:"",precision:2},date:{format:"YYYY-MM-DD"}},common:{placeholder:"placeholder"}}}}]);
2
- //# sourceMappingURL=tp.umd.min.lang-en-js.js.map
1
+ "use strict";(("undefined"!=typeof self?self:this).webpackChunktp=("undefined"!=typeof self?self:this).webpackChunktp||[]).push([[648],{48167:function(e,n,f){f.r(n);var l=f(33088);n.default={...l.default,lingua:"English",tp:{money:{currency:"$"}},locale:{name:"en",prefix:"en-US",number:{decimal:".",thousands:"",prefix:"",suffix:"",precision:2},date:{format:"YYYY-MM-DD"}},common:{placeholder:"placeholder"}}}}]);
2
+ //# sourceMappingURL=tp.umd.min.lang-tp-en-js.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tp.umd.min.lang-tp-en-js.js","mappings":"oLAEA,cACKA,EAAAA,QACHC,OAAQ,UAERC,GAAI,CACFC,MAAO,CACLC,SAAU,MAIdC,OAAQ,CAENC,KAAM,KAGNC,OAAQ,QAERC,OAAQ,CACNC,QAAS,IACTC,UAAW,GACXH,OAAQ,GACRI,OAAQ,GACRC,UAAW,GAGbC,KAAM,CACJC,OAAQ,eAGZC,OAAQ,CACNC,YAAa,e","sources":["webpack://tp/./src/locale/lang/en.js"],"sourcesContent":["import enLocale from 'element-plus/es/locale/lang/en'\n\nexport default {\n ...enLocale,\n lingua: 'English',\n\n tp: {\n money: {\n currency: '$'\n }\n },\n\n locale: {\n // locale name equals filename reference\n name: 'en',\n\n // IANA.ORG convetions according to W3C\n prefix: 'en-US',\n\n number: {\n decimal: '.',\n thousands: '',\n prefix: '',\n suffix: '',\n precision: 2\n },\n\n date: {\n format: 'YYYY-MM-DD'\n }\n },\n common: {\n placeholder: 'placeholder'\n }\n}\n"],"names":["enLocale","lingua","tp","money","currency","locale","name","prefix","number","decimal","thousands","suffix","precision","date","format","common","placeholder"],"sourceRoot":""}
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "email": "bruno@tagplus.com.br"
9
9
  }
10
10
  ],
11
- "version": "5.0.0",
11
+ "version": "5.1.0",
12
12
  "main": "./dist/tp.common.js",
13
13
  "directories": {
14
14
  "lib": "src/lib"
@@ -2,57 +2,73 @@ import { createI18n } from 'vue-i18n'
2
2
  import { reactive } from 'vue'
3
3
 
4
4
  let i18n
5
- /**
6
- * Eventos que podem ser observados com watch
7
- */
8
- const hooks = reactive({
9
- onLanguageChanged: ''
10
- })
11
5
 
12
6
  const loadedLanguages = []
13
7
 
8
+ let importLangFile = false
9
+ const i18nHelper = {
10
+ /**
11
+ * Eventos que podem ser observados com watch
12
+ */
13
+ hooks: reactive({
14
+ onLanguageChanged: ''
15
+ })
16
+ }
17
+
14
18
  /**
15
- * Função padrão para importar arquivos de language
19
+ * Assincronamente importa e mescla arquivos de lang do vue components e do projeto em que essa lib é importada
20
+ * na lingaguem escolhida
16
21
  * caminho padrão src/locale/lang/*.js
17
- * @param {String} filename
18
- * @returns {Promise}
22
+ * @param {String} filename - The name of the language file to import.
23
+ * @returns {Promise} - A promise that resolves to an object containing merged custom and local language messages.
19
24
  */
20
- let importLangFile = (filename) => {
21
- return import(/* webpackChunkName: "lang-[request]" */ `@src/locale/lang/${filename}.js`)
25
+ const importLangFiles = async function (filename) {
26
+ let langsCustom = {}
27
+ const langsLocais = await import(/* webpackChunkName: "lang-tp-[request]" */ `@src/locale/lang/${filename}.js`)
28
+ if (importLangFile && typeof importLangFile === 'function') {
29
+ langsCustom = await importLangFile(filename)
30
+ }
31
+ return { ...langsCustom, ...langsLocais }
22
32
  }
23
33
 
24
34
  /**
25
- * Executa após trocar a lang, pode ser usado para salvar nos cookies ou session
26
- * @param {String} locale
35
+ * Essa função deve ser impementada no i18nHelper para executar ações após trocar a linguagem,
36
+ * como salvar nos cookies ou session a linguagem atual
37
+ *
38
+ * @param {String} locale - The locale string representing the language to be set.
39
+ * @throws {Error} Throws an error if the onLanguageSet function is not implemented.
40
+ * @returns {void}
27
41
  */
28
- let onLanguageSet = (locale) => {
29
-
42
+ i18nHelper.onLanguageSet = function (locale) {
43
+ throw new Error('Implemente a função onLanguageSet na definição do plugin i18n')
30
44
  }
31
45
 
32
46
  /**
33
- * Seta a linguagem atual
34
- * @param {String} locale 'pt-br', 'en'
35
- * @returns {String} locale
47
+ * Sets the current language and updates the i18n instance accordingly.
48
+ * Triggers the onLanguageChanged hook and calls the onLanguageSet function.
49
+ * @param {String} locale - The locale string representing the language to be set. EX: 'pt-br', 'en'
50
+ * @returns {String} - The updated locale string.
36
51
  */
37
- export function setI18nLanguage (locale) {
52
+ i18nHelper.setI18nLanguage = function (locale) {
38
53
  if (i18n.mode === 'legacy') {
39
54
  i18n.global.locale = locale
40
55
  } else {
41
56
  i18n.global.locale.value = locale
42
57
  }
43
58
 
44
- hooks.onLanguageChanged = locale
59
+ i18nHelper.hooks.onLanguageChanged = locale
45
60
  document.querySelector('html').setAttribute('lang', locale)
46
- onLanguageSet(locale)
61
+ i18nHelper.onLanguageSet(locale)
47
62
  return locale
48
63
  }
49
64
 
50
65
  /**
51
- *
52
- * @param {String} locale 'pt-br', 'en'
53
- * @returns {String} locale
66
+ * Asynchronously loads and sets the current language for the i18n instance.
67
+ * If the language is already loaded, it returns immediately.
68
+ * @param {String} locale - The locale string representing the language to be set. EX: 'pt-br', 'en'
69
+ * @returns {Promise}
54
70
  */
55
- export async function loadLanguageAsync (locale) {
71
+ i18nHelper.loadLanguageAsync = async function (locale) {
56
72
  if (i18n.global.locale === locale) {
57
73
  return locale
58
74
  }
@@ -63,7 +79,7 @@ export async function loadLanguageAsync (locale) {
63
79
  }
64
80
 
65
81
  // load locale messages with dynamic import
66
- const messages = await importLangFile(locale)
82
+ const messages = await importLangFiles(locale)
67
83
  // set locale and locale message
68
84
  i18n.global.setLocaleMessage(locale, messages.default)
69
85
  loadedLanguages.push(locale)
@@ -71,12 +87,16 @@ export async function loadLanguageAsync (locale) {
71
87
  }
72
88
 
73
89
  /**
74
- * Essa função cria a instância do i18n
75
- * Exporta isso para poder importar do projeto nuxt sem ter que reimplementar toda a lib
76
- * @param {Object} options
77
- * @returns
90
+ * Asynchronously sets up and initializes the i18n instance with the provided options.
91
+ * If the i18n instance is already created, it returns the existing instance.
92
+ *
93
+ * @param {Object} [options={}] - An optional object containing configuration options for the i18n instance.
94
+ * @param {String} [options.locale='pt-br'] - The default locale for the i18n instance.
95
+ * @param {Object} [options.i18nOptions] - Additional options to be passed to the createI18n function.
96
+ *
97
+ * @returns {Promise<I18n>} - A promise that resolves to the initialized i18n instance.
78
98
  */
79
- export async function setupI18n (options = {}) {
99
+ async function setupI18n (options = {}) {
80
100
  if (!i18n) {
81
101
  const locale = options.locale || 'pt-br'
82
102
 
@@ -89,26 +109,54 @@ export async function setupI18n (options = {}) {
89
109
  ...options
90
110
  })
91
111
 
92
- await loadLanguageAsync(locale)
93
- setI18nLanguage(locale)
112
+ await i18nHelper.loadLanguageAsync(locale)
113
+ i18nHelper.setI18nLanguage(locale)
94
114
  }
95
115
  return i18n
96
116
  }
97
117
 
98
- export default (options) => {
99
- // Sobrescreve a função de importar arquivos
100
- if (typeof options.importLangFile === 'function') {
101
- importLangFile = options.importLangFile
118
+ /**
119
+ * A plugin for Vue.js that initializes and configures the i18n instance with customizable options.
120
+ * It also provides a helper object for managing language settings and loading language files asynchronously.
121
+ *
122
+ * @param {Object} options - An object containing configuration options for the plugin.
123
+ * @param {Object} options.i18nHelper - An object containing custom functions for managing language settings.
124
+ * @param {Function} [options.i18nHelper.importLangFile] - A function for asynchronously importing custom language files.
125
+ * @param {Function} [options.i18nHelper.onLanguageSet] - A function to be called after setting the language.
126
+ * @param {Object} [options.i18nOptions] - Additional options to be passed to the createI18n function.
127
+ *
128
+ * @returns {Object} - An object with an install function for Vue.js plugins.
129
+ */
130
+ export default async (options) => {
131
+ // Override the importLangFile function if provided in options
132
+ if (typeof options.i18nHelper.importLangFile === 'function') {
133
+ i18nHelper.importLangFile = options.i18nHelper.importLangFile
134
+ }
135
+
136
+ // Override the onLanguageSet function if provided in options
137
+ if (typeof options.i18nHelper.onLanguageSet === 'function') {
138
+ i18nHelper.onLanguageSet = options.i18nHelper.onLanguageSet
102
139
  }
103
140
 
104
- if (typeof options.onLanguageSet === 'function') {
105
- onLanguageSet = options.onLanguageSet
141
+ let i18nOptions = {}
142
+ // Use provided i18nOptions if available
143
+ if (options.i18nOptions && typeof options.i18nOptions === 'object') {
144
+ i18nOptions = options.i18nOptions
106
145
  }
107
146
 
147
+ // Initialize the i18n instance with the provided options
148
+ i18n = await setupI18n(i18nOptions)
149
+
108
150
  return {
109
- setupI18n,
110
- loadLanguageAsync,
111
- setI18nLanguage,
112
- hooks
151
+ install: (app, opts) => {
152
+ // Install the original i18n instance
153
+ i18n.install(app, i18nOptions)
154
+
155
+ /**
156
+ * Override project-specific functions and provide the $i18nHelper
157
+ * in the Vue.js app's global properties.
158
+ **/
159
+ app.config.globalProperties.$i18nHelper = i18nHelper
160
+ }
113
161
  }
114
162
  }
package/src/main.js CHANGED
@@ -1,7 +1,6 @@
1
1
  // import 'element-plus/dist/index.css'
2
2
  import * as components from '@src/components'
3
3
  import Currency from '@src/plugins/currency.js'
4
- import i18nLib from '@src/locale/i18n.js'
5
4
 
6
5
  const install = function (app) {
7
6
  app.use(Currency)
@@ -18,4 +17,4 @@ if (typeof window !== 'undefined' && window.Vue) {
18
17
  }
19
18
 
20
19
  export default { ...components, install }
21
- export { i18nLib }
20
+ export * as i18n from '@src/locale/i18n.js'
@@ -1 +0,0 @@
1
- {"version":3,"file":"tp.common.lang-en-js.js","mappings":"sNAEA,cACKA,EAAAA,QACHC,OAAQ,UAERC,GAAI,CACFC,MAAO,CACLC,SAAU,MAIdC,OAAQ,CAENC,KAAM,KAGNC,OAAQ,QAERC,OAAQ,CACNC,QAAS,IACTC,UAAW,GACXH,OAAQ,GACRI,OAAQ,GACRC,UAAW,GAGbC,KAAM,CACJC,OAAQ,eAGZC,OAAQ,CACNC,YAAa,e","sources":["webpack://@tagplus/components/./src/locale/lang/en.js"],"sourcesContent":["import enLocale from 'element-plus/es/locale/lang/en'\n\nexport default {\n ...enLocale,\n lingua: 'English',\n\n tp: {\n money: {\n currency: '$'\n }\n },\n\n locale: {\n // locale name equals filename reference\n name: 'en',\n\n // IANA.ORG convetions according to W3C\n prefix: 'en-US',\n\n number: {\n decimal: '.',\n thousands: '',\n prefix: '',\n suffix: '',\n precision: 2\n },\n\n date: {\n format: 'YYYY-MM-DD'\n }\n },\n common: {\n placeholder: 'placeholder'\n }\n}\n"],"names":["enLocale","lingua","tp","money","currency","locale","name","prefix","number","decimal","thousands","suffix","precision","date","format","common","placeholder"],"sourceRoot":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"tp.umd.lang-en-js.js","mappings":"oLAEA,cACKA,EAAAA,QACHC,OAAQ,UAERC,GAAI,CACFC,MAAO,CACLC,SAAU,MAIdC,OAAQ,CAENC,KAAM,KAGNC,OAAQ,QAERC,OAAQ,CACNC,QAAS,IACTC,UAAW,GACXH,OAAQ,GACRI,OAAQ,GACRC,UAAW,GAGbC,KAAM,CACJC,OAAQ,eAGZC,OAAQ,CACNC,YAAa,e","sources":["webpack://tp/./src/locale/lang/en.js"],"sourcesContent":["import enLocale from 'element-plus/es/locale/lang/en'\n\nexport default {\n ...enLocale,\n lingua: 'English',\n\n tp: {\n money: {\n currency: '$'\n }\n },\n\n locale: {\n // locale name equals filename reference\n name: 'en',\n\n // IANA.ORG convetions according to W3C\n prefix: 'en-US',\n\n number: {\n decimal: '.',\n thousands: '',\n prefix: '',\n suffix: '',\n precision: 2\n },\n\n date: {\n format: 'YYYY-MM-DD'\n }\n },\n common: {\n placeholder: 'placeholder'\n }\n}\n"],"names":["enLocale","lingua","tp","money","currency","locale","name","prefix","number","decimal","thousands","suffix","precision","date","format","common","placeholder"],"sourceRoot":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"tp.umd.min.lang-en-js.js","mappings":"oLAEA,cACKA,EAAAA,QACHC,OAAQ,UAERC,GAAI,CACFC,MAAO,CACLC,SAAU,MAIdC,OAAQ,CAENC,KAAM,KAGNC,OAAQ,QAERC,OAAQ,CACNC,QAAS,IACTC,UAAW,GACXH,OAAQ,GACRI,OAAQ,GACRC,UAAW,GAGbC,KAAM,CACJC,OAAQ,eAGZC,OAAQ,CACNC,YAAa,e","sources":["webpack://tp/./src/locale/lang/en.js"],"sourcesContent":["import enLocale from 'element-plus/es/locale/lang/en'\n\nexport default {\n ...enLocale,\n lingua: 'English',\n\n tp: {\n money: {\n currency: '$'\n }\n },\n\n locale: {\n // locale name equals filename reference\n name: 'en',\n\n // IANA.ORG convetions according to W3C\n prefix: 'en-US',\n\n number: {\n decimal: '.',\n thousands: '',\n prefix: '',\n suffix: '',\n precision: 2\n },\n\n date: {\n format: 'YYYY-MM-DD'\n }\n },\n common: {\n placeholder: 'placeholder'\n }\n}\n"],"names":["enLocale","lingua","tp","money","currency","locale","name","prefix","number","decimal","thousands","suffix","precision","date","format","common","placeholder"],"sourceRoot":""}