@kong-ui-public/i18n 0.3.5-pr.231.276ad83.0 → 0.3.5-pr.231.61cbd08.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/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # @kong-ui-public/i18n
2
2
 
3
3
  - [Purpose](#purpose)
4
+ - [TypeScript Support](#typescript-support)
5
+ - [Global Registration](#global-registration)
6
+ - [Vue Plugin](#vue-plugin)
4
7
  - [Use in application](#use-in-application)
5
8
  - [Use in shared component](#use-in-shared-component)
6
9
  - [Formatting messages](#formatting-messages)
@@ -19,6 +22,52 @@ This is a wrapper around [FormatJS](https://formatjs.io/docs/intl) that allows c
19
22
  - format numbers
20
23
  - format dates
21
24
 
25
+ ## TypeScript Support
26
+
27
+ In order to provide full autocompletion for the translation keys, you need to pass the type of the `messages` object to the various init functions, as shown here:
28
+
29
+ > **Note**: type checking and autocompletion are still a work-in-progress when utilizing the `i18n-t` component.
30
+ >
31
+
32
+ ### Global Registration
33
+
34
+ ```ts
35
+ // main.ts
36
+ import { createI18n, Translation } from '@kong-ui-public/i18n'
37
+ import english from './locales/en.json'
38
+
39
+ const i18n = createI18n<typeof english>('en-us', english, true)
40
+ app.use(Translation, { i18n })
41
+
42
+ // composables/index.ts
43
+ import { useI18n as useI18nComposable } from '@kong-ui-public/i18n'
44
+ import english from './locales/en.json'
45
+
46
+
47
+ const useI18n = useI18nComposable<typeof english>
48
+
49
+ export default {
50
+ useI18n,
51
+ }
52
+
53
+ // Component.vue
54
+ import composables from './composables'
55
+ const i18n = composables.useI18n()
56
+ ```
57
+
58
+ ### Vue Plugin
59
+
60
+ ```ts
61
+ import { Translation, createI18n } from '@kong-ui-public/i18n'
62
+ import english from './locales/en.json'
63
+
64
+ const i18n = createI18n<typeof english>('en-us', english, true)
65
+ const app = createApp(App)
66
+ app.use(Translation, { i18n })
67
+
68
+ app.mount('#app')
69
+ ```
70
+
22
71
  ## Use in application
23
72
 
24
73
  When used in application we only need to instantiate `Intl` object once, during application hydration, and then we can use it anywhere in the app via `useI18n` composable.
@@ -32,7 +81,7 @@ import english from './locales/en.json'
32
81
  ...
33
82
 
34
83
  // this will create Application instance of Intl object
35
- createI18n('en-us', english, true)
84
+ createI18n<typeof english>('en-us', english, true)
36
85
  ```
37
86
 
38
87
  And then, anywhere in application code where `i18n` is needed:
@@ -48,17 +97,6 @@ const i18n = useI18n()
48
97
  </script>
49
98
  ```
50
99
 
51
- If you want translation key suggestions on the global instance, update your import as shown here, passing the JSON file type to the function:
52
-
53
- ```html
54
- <script setup lang="ts">
55
- import { useI18n } from '@kong-ui-public/i18n'
56
- import english from './locales/en.json'
57
-
58
- const i18n = useI18n<typeof english>()
59
- </script>
60
- ```
61
-
62
100
  ## Use in shared component
63
101
 
64
102
  When used in [shared component](https://github.com/Kong/shared-ui-components/pull/118/files#diff-f296a0775f650cac9ecd4b5d7ccef015ca73587be833b40eeae1e396426b0f4fR45) (outside of application code), we need `Intl` that is local to that component, so that it is using component's own localization strings:
@@ -76,7 +114,7 @@ import english from '../locales/en.json'
76
114
 
77
115
  // this will instantiate `Intl` local to this component, using component's english messages.
78
116
  // TODO: load and pass messages based on language passed from consuming application
79
- const i18n = createI18n(props.locale || 'en-us', english)
117
+ const i18n = createI18n<typeof english>(props.locale || 'en-us', english)
80
118
  </script>
81
119
  ```
82
120
 
@@ -195,7 +233,7 @@ import english from './locales/en.json'
195
233
  ...
196
234
 
197
235
  //this will create Application instance of Intl object
198
- const i18n = createI18n('en-us', english, true)
236
+ const i18n = createI18n<typeof english>('en-us', english, true)
199
237
  // this will register <i18n-t> component
200
238
  app.use(Translation, { i18n })
201
239
  ```
@@ -259,7 +297,6 @@ And then, anywhere in application code where `i18n` is needed
259
297
 
260
298
  In some cases we do not have access to the Vue `app` and cannot relay on registered i18nT plugin. Working on standalone components in `public-ui-components` is of those cases. And for this your component will look like:
261
299
 
262
-
263
300
  ```html
264
301
  <template>
265
302
  <i18n-t
@@ -273,8 +310,8 @@ In some cases we do not have access to the Vue `app` and cannot relay on registe
273
310
  import { createI18n, i18nTComponent } from '@kong-ui-public/i18n'
274
311
  import english from './locales/en.json'
275
312
 
276
- const i18n = createI18n('en-us', english)
277
- const i18nT = i18nTComponent(i18n)
313
+ const i18n = createI18n<typeof english>('en-us', english)
314
+ const i18nT = i18nTComponent<typeof english>(i18n)
278
315
 
279
316
  </script>
280
317
  ```
@@ -299,10 +336,10 @@ Or in old `defineComponent` way
299
336
  import english from './locales/en.json'
300
337
 
301
338
  export default defineComponent({
302
- components: { i18nT: i18nTComponent() },
339
+ components: { i18nT: i18nTComponent<typeof english>() },
303
340
  setup() {
304
341
  return {
305
- i18n: createI18n('en-us', english)
342
+ i18n: createI18n<typeof english>('en-us', english)
306
343
  }
307
344
  }
308
345
  })
package/dist/i18n.es.js CHANGED
@@ -3304,7 +3304,7 @@ const gr = (e, t, n = !1) => {
3304
3304
  function pr() {
3305
3305
  return Ve;
3306
3306
  }
3307
- const cr = (e) => ke({
3307
+ const cr = (e = null) => ke({
3308
3308
  name: "I18nT",
3309
3309
  props: {
3310
3310
  i18n: {
@@ -3313,6 +3313,7 @@ const cr = (e) => ke({
3313
3313
  },
3314
3314
  keypath: {
3315
3315
  type: String,
3316
+ // type: String as unknown as PropType<PathToDotNotation<MessageSource, string>>, // This breaks the type interface, enable to debug
3316
3317
  required: !0
3317
3318
  },
3318
3319
  tag: {