@juit/vue-i18n 0.0.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/LICENSE.md ADDED
@@ -0,0 +1,211 @@
1
+ # Apache License
2
+
3
+ Version 2.0, January 2004
4
+
5
+ [http://www.apache.org/licenses/](http://www.apache.org/licenses/)
6
+
7
+ ##### TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ ### 1. Definitions.
10
+
11
+ "**License**" shall mean the terms and conditions for use, reproduction,
12
+ and distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ "**Licensor**" shall mean the copyright owner or entity authorized by
15
+ the copyright owner that is granting the License.
16
+
17
+ "**Legal Entity**" shall mean the union of the acting entity and all
18
+ other entities that control, are controlled by, or are under common
19
+ control with that entity. For the purposes of this definition,
20
+ "**control**" means **(i)** the power, direct or indirect, to cause the
21
+ direction or management of such entity, whether by contract or
22
+ otherwise, _or_ **(ii)** ownership of fifty percent (50%) or more of
23
+ the outstanding shares, _or_ **(iii)** beneficial ownership of such
24
+ entity.
25
+
26
+ "**You**" (or "**Your**") shall mean an individual or Legal Entity
27
+ exercising permissions granted by this License.
28
+
29
+ "**Source**" form shall mean the preferred form for making
30
+ modifications, including but not limited to software source code,
31
+ documentation source, and configuration files.
32
+
33
+ "**Object**" form shall mean any form resulting from mechanical
34
+ transformation or translation of a Source form, including but not
35
+ limited to compiled object code, generated documentation, and
36
+ conversions to other media types.
37
+
38
+ "**Work**" shall mean the work of authorship, whether in Source or
39
+ Object form, made available under the License, as indicated by a
40
+ copyright notice that is included in or attached to the work (an example
41
+ is provided in the Appendix below).
42
+
43
+ "**Derivative Works**" shall mean any work, whether in Source or Object
44
+ form, that is based on (or derived from) the Work and for which the
45
+ editorial revisions, annotations, elaborations, or other modifications
46
+ represent, as a whole, an original work of authorship. For the purposes
47
+ of this License, Derivative Works shall not include works that remain
48
+ separable from, or merely link (or bind by name) to the interfaces of,
49
+ the Work and Derivative Works thereof.
50
+
51
+ "**Contribution**" shall mean any work of authorship, including the
52
+ original version of the Work and any modifications or additions to that
53
+ Work or Derivative Works thereof, that is intentionally submitted to
54
+ Licensor for inclusion in the Work by the copyright owner or by an
55
+ individual or Legal Entity authorized to submit on behalf of the
56
+ copyright owner. For the purposes of this definition, "**submitted**"
57
+ means any form of electronic, verbal, or written communication sent to
58
+ the Licensor or its representatives, including but not limited to
59
+ communication on electronic mailing lists, source code control systems,
60
+ and issue tracking systems that are managed by, or on behalf of, the
61
+ Licensor for the purpose of discussing and improving the Work, but
62
+ excluding communication that is conspicuously marked or otherwise
63
+ designated in writing by the copyright owner as "**Not a
64
+ Contribution**".
65
+
66
+ "**Contributor**" shall mean Licensor and any individual or Legal Entity
67
+ on behalf of whom a Contribution has been received by Licensor and
68
+ subsequently incorporated within the Work.
69
+
70
+ ### 2. Grant of Copyright License.
71
+
72
+ Subject to the terms and conditions of this License, each Contributor
73
+ hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
74
+ royalty-free, irrevocable copyright license to reproduce, prepare
75
+ Derivative Works of, publicly display, publicly perform, sublicense,
76
+ and distribute the Work and such Derivative Works in Source or Object
77
+ form.
78
+
79
+ ### 3. Grant of Patent License.
80
+
81
+ Subject to the terms and conditions of this License, each Contributor
82
+ hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
83
+ royalty-free, irrevocable (except as stated in this section) patent
84
+ license to make, have made, use, offer to sell, sell, import, and
85
+ otherwise transfer the Work, where such license applies only to those
86
+ patent claims licensable by such Contributor that are necessarily
87
+ infringed by their Contribution(s) alone or by combination of their
88
+ Contribution(s) with the Work to which such Contribution(s) was
89
+ submitted. If You institute patent litigation against any entity
90
+ (including a cross-claim or counterclaim in a lawsuit) alleging that
91
+ the Work or a Contribution incorporated within the Work constitutes
92
+ direct or contributory patent infringement, then any patent licenses
93
+ granted to You under this License for that Work shall terminate as of
94
+ the date such litigation is filed.
95
+
96
+ ### 4. Redistribution.
97
+
98
+ You may reproduce and distribute copies of the Work or Derivative Works
99
+ thereof in any medium, with or without modifications, and in Source or
100
+ Object form, provided that You meet the following conditions:
101
+
102
+ <ol type="a">
103
+ <li>You must give any other recipients of the Work or Derivative Works
104
+ a copy of this License; and
105
+ <li>You must cause any modified files to carry prominent notices
106
+ stating that You changed the files; and
107
+ <li>You must retain, in the Source form of any Derivative Works that
108
+ You distribute, all copyright, patent, trademark, and attribution
109
+ notices from the Source form of the Work, excluding those notices
110
+ that do not pertain to any part of the Derivative Works; and
111
+ <li>If the Work includes a "NOTICE" text file as part of its
112
+ distribution, then any Derivative Works that You distribute must
113
+ include a readable copy of the attribution notices contained within
114
+ such NOTICE file, excluding those notices that do not pertain to any
115
+ part of the Derivative Works, in at least one of the following
116
+ places: within a NOTICE text file distributed as part of the
117
+ Derivative Works; within the Source form or documentation, if
118
+ provided along with the Derivative Works; or, within a display
119
+ generated by the Derivative Works, if and wherever such third-party
120
+ notices normally appear. The contents of the NOTICE file are for
121
+ informational purposes only and do not modify the License. You may
122
+ add Your own attribution notices within Derivative Works that You
123
+ distribute, alongside or as an addendum to the NOTICE text from the
124
+ Work, provided that such additional attribution notices cannot be
125
+ construed as modifying the License.
126
+ </ol>
127
+
128
+ You may add Your own copyright statement to Your modifications and may
129
+ provide additional or different license terms and conditions for use,
130
+ reproduction, or distribution of Your modifications, or for any such
131
+ Derivative Works as a whole, provided Your use, reproduction, and
132
+ distribution of the Work otherwise complies with the conditions stated
133
+ in this License.
134
+
135
+ ### 5. Submission of Contributions.
136
+
137
+ Unless You explicitly state otherwise, any Contribution intentionally
138
+ submitted for inclusion in the Work by You to the Licensor shall be
139
+ under the terms and conditions of this License, without any additional
140
+ terms or conditions. Notwithstanding the above, nothing herein shall
141
+ supersede or modify the terms of any separate license agreement you may
142
+ have executed with Licensor regarding such Contributions.
143
+
144
+ ### 6. Trademarks.
145
+
146
+ This License does not grant permission to use the trade names,
147
+ trademarks, service marks, or product names of the Licensor, except as
148
+ required for reasonable and customary use in describing the origin of
149
+ the Work and reproducing the content of the NOTICE file.
150
+
151
+ ### 7. Disclaimer of Warranty.
152
+
153
+ Unless required by applicable law or agreed to in writing, Licensor
154
+ provides the Work (and each Contributor provides its Contributions) on
155
+ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
156
+ express or implied, including, without limitation, any warranties or
157
+ conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR
158
+ A PARTICULAR PURPOSE. You are solely responsible for determining the
159
+ appropriateness of using or redistributing the Work and assume any risks
160
+ associated with Your exercise of permissions under this License.
161
+
162
+ ### 8. Limitation of Liability.
163
+
164
+ In no event and under no legal theory, whether in tort (including
165
+ negligence), contract, or otherwise, unless required by applicable law
166
+ (such as deliberate and grossly negligent acts) or agreed to in writing,
167
+ shall any Contributor be liable to You for damages, including any
168
+ direct, indirect, special, incidental, or consequential damages of any
169
+ character arising as a result of this License or out of the use or
170
+ inability to use the Work (including but not limited to damages for loss
171
+ of goodwill, work stoppage, computer failure or malfunction, or any and
172
+ all other commercial damages or losses), even if such Contributor has
173
+ been advised of the possibility of such damages.
174
+
175
+ ### 9. Accepting Warranty or Additional Liability.
176
+
177
+ While redistributing the Work or Derivative Works thereof, You may
178
+ choose to offer, and charge a fee for, acceptance of support, warranty,
179
+ indemnity, or other liability obligations and/or rights consistent with
180
+ this License. However, in accepting such obligations, You may act only
181
+ on Your own behalf and on Your sole responsibility, not on behalf of any
182
+ other Contributor, and only if You agree to indemnify, defend, and hold
183
+ each Contributor harmless for any liability incurred by, or claims
184
+ asserted against, such Contributor by reason of your accepting any
185
+ such warranty or additional liability.
186
+
187
+ ##### END OF TERMS AND CONDITIONS
188
+
189
+ ## APPENDIX: How to apply the Apache License to your work.
190
+
191
+ To apply the Apache License to your work, attach the following
192
+ boilerplate notice, with the fields enclosed by brackets "[]"
193
+ replaced with your own identifying information. (Don't include the
194
+ brackets!) The text should be enclosed in the appropriate comment
195
+ syntax for the file format. We also recommend that a file or class name
196
+ and description of purpose be included on the same "printed page" as the
197
+ copyright notice for easier identification within third-party archives.
198
+
199
+ > #### Copyright [yyyy] [name of copyright owner]
200
+ >
201
+ > Licensed under the Apache License, Version 2.0 (the "License");
202
+ > you may not use this file except in compliance with the License.
203
+ > You may obtain a copy of the License at
204
+ >
205
+ > [http://www.apache.org/licenses/](http://www.apache.org/licenses/)
206
+ >
207
+ > Unless required by applicable law or agreed to in writing, software
208
+ > distributed under the License is distributed on an "AS IS" BASIS,
209
+ > WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
210
+ > See the License for the specific language governing permissions and
211
+ > limitations under the License.
package/README.md ADDED
@@ -0,0 +1,291 @@
1
+ Juit I18n for Vue
2
+ =================
3
+
4
+ The `@juit/vue-i18n` package provides a _minimalistic_ plugin for VueJS (v. 3)
5
+ to support basic internationalization (translations, numbers, and date formats).
6
+
7
+ It heavily relies on the [`Intl.Locale`][1], [`Intl.NumberFormat`][2], and
8
+ [`Intl.DateTimeFormat`][3] global objects widely supported by modern browsers.
9
+
10
+ It also deeply integrates with TypeScript to provide compile-time checking on
11
+ required translation languages and translation keys.
12
+
13
+
14
+ ## Table of Contents
15
+
16
+ - [Installation](#installation)
17
+ - [Configuration](#configuration)
18
+ - [Usage](#usage)
19
+ - [Translating messages](#translating-messages)
20
+ - [Parameterizing translations](#parameterizing-translations)
21
+ - [Pluralization](#pluralization)
22
+ - [Formatting numbers](#formatting-numbers)
23
+ - [Formatting dates](#formatting-dates)
24
+ - [Configuring Types](#configuring-types)
25
+ - [Legal Stuff](#legal-stuff)
26
+
27
+
28
+ ## Installation
29
+
30
+ As usual, install with NPM (or the cool package-manager du jour):
31
+
32
+ ```bash
33
+ npm install '@juit/vue-i18n'
34
+ ```
35
+
36
+ And add the plugin to your Vue app:
37
+
38
+ ```typescript
39
+ import { createApp } from 'vue'
40
+ import { i18n } from '@juit/vue-i18n'
41
+ import MyApp from './app.vue'
42
+
43
+ const app = createApp(MyApp).use(i18n, {
44
+ defaultLanguage: 'en',
45
+ translations: {
46
+ hello: {
47
+ en: 'Hello, world!',
48
+ de: 'Hallo, Welt!',
49
+ },
50
+ }
51
+ })
52
+ ```
53
+
54
+
55
+ ## Configuration
56
+
57
+ The plugin can be configured with a simple string (the `defaultLanguage`)
58
+ described below, or some options:
59
+
60
+ * `defaultLanguage`: the default language to use; all translations should be
61
+ available in this language.
62
+ * `translations`: an object containing the translations for the messages to
63
+ translate, keyed by its identifier.
64
+ * `formats`: configurations for number and date formatting:
65
+ * `dateTimeFormat`: a string (`short`, `medium`, `long`, or `full`) used to
66
+ format date-and-time values, or the options to be
67
+ provided to the `Intl.DateTimeFormat` constructor.
68
+ * `dateOnlyFormat`: a string (`short`, `medium`, `long`, or `full`) used to
69
+ format dates, or the options to be provided to the
70
+ `Intl.DateTimeFormat` constructor.
71
+ * `timeOnlyFormat`: a string (`short`, `medium`, `long`, or `full`) used to
72
+ format times, or the options to be provided to the
73
+ `Intl.DateTimeFormat` constructor.
74
+
75
+
76
+ ## Usage
77
+
78
+ In any component `setup()` method, you can use the `useTranslator()` function
79
+ to get a hold on the `Translator` configured for the current app.
80
+
81
+ ```typescript
82
+ import { useTranslator } from '@juit/vue-i18n'
83
+
84
+ const translator = useTranslator()
85
+ ```
86
+
87
+
88
+ ## Translating messages
89
+
90
+ The base function to translate messages is exposed as `translator.t(...)` or
91
+ (within components) the `$t(...)` function.
92
+
93
+ This function takes a translation _key_ (specified in the configuration
94
+ phase, see above).
95
+
96
+ ```typescript
97
+ import { useTranslator } from '@juit/vue-i18n'
98
+
99
+ const translator = useTranslator()
100
+
101
+ const hello = translator.t('hello')
102
+ // the "hello" string will be "Hello, world!" or "Hallo, Welt!"
103
+ ```
104
+
105
+ It can also take an inline translation:
106
+
107
+ ```typescript
108
+ import { useTranslator } from '@juit/vue-i18n'
109
+
110
+ const translator = useTranslator()
111
+
112
+ const panagram = translator.t({
113
+ en: 'The quick fox jumped over the lazy dog',
114
+ de: 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'
115
+ })
116
+ ```
117
+
118
+
119
+ ### Parameterizing translations
120
+
121
+ Translations can include parameters by enclosing them in curly braces `{param}`.
122
+
123
+ For example:
124
+
125
+ ```typescript
126
+ const name = 'John Doe'
127
+
128
+ const string: translator.t({
129
+ en: 'Your name is {name}'
130
+ de: 'Ihr Name ist {name}'
131
+ }, { name })
132
+ // This will result in either "Your name is John Doe" or "Ihr Name ist John Doe"
133
+ ```
134
+
135
+ When parameters are numbers, those will be formatted as numbers:
136
+
137
+ ```typescript
138
+ const string: translator.t({
139
+ en: 'Score {points} points'
140
+ de: 'Punktestand {points} Punkte'
141
+ }, { points: 1234.56 })
142
+ // This will result in "Score 1,234.56 points" or "Punktestand 1.234,56 Punkte"
143
+ ```
144
+
145
+
146
+ ### Pluralization
147
+
148
+ The translator supports minimal rules for pluralization by separating
149
+ translation messages with the `|` (pipe) character.
150
+
151
+ Messages can contain two variants `singular|plural` or three variants
152
+ `zero|singular|plural`, with each variant used when the reference number to
153
+ pluralize is either zero, one, or another number:
154
+
155
+ To contextualize the number, either use the `n` parameter, or use the `tc(...)`
156
+ function which will take, as a second parameter, the reference number.
157
+
158
+ For example:
159
+
160
+ ```typescript
161
+ const string: translator.t({
162
+ en: 'no cats | one cat | {n} cats'
163
+ de: 'keine Katzen | eine Katze | {n} Katzen'
164
+ }, { n })
165
+ // This will result in "no cats" or "keine katzen" when "n" is zero,
166
+ // "one cat" or "eine Katze" when "n" is 1, or
167
+ // "1,234.56 cats" or "1.234,56 Katzen" when "n" is 1234.56
168
+ ```
169
+
170
+ Is equivalent to:
171
+
172
+ ```typescript
173
+ const string: translator.tc({
174
+ en: 'no cats | one cat | {n} cats'
175
+ de: 'keine Katzen | eine Katze | {n} Katzen'
176
+ }, n)
177
+ ```
178
+
179
+
180
+ ## Formatting numbers
181
+
182
+ The `n(...)` function can be used to format numbers in the current locale:
183
+
184
+ ```typescript
185
+ import { useTranslator } from '@juit/vue-i18n'
186
+
187
+ const translator = useTranslator()
188
+
189
+ const number = translator.n(1234.5)
190
+ // the "number" string will be "1,234.5", "1.234,5" or whatever locale specified
191
+ ```
192
+
193
+ A currency can be specified as a second parameter for quick formatting:
194
+
195
+ ```typescript
196
+ import { useTranslator } from '@juit/vue-i18n'
197
+
198
+ const translator = useTranslator()
199
+
200
+ const amount = translator.n(1234.5, 'USD')
201
+ // the "amount" will be "$1,234.5", "1.234,5 $" or whatever locale specified
202
+ ```
203
+
204
+ A full [`Intl.NumberFormatOptions`][4] set of options can also be specified
205
+ as a second parameter to fine-tune the formatting.
206
+
207
+ The default format can be specified when configuring the plugin as the
208
+ `formats.numberFormat` option (intentionally, there is no default).
209
+
210
+
211
+ ## Formatting dates
212
+
213
+ The `d(...)` function can be used to format date-and-time values in the current
214
+ locale, together with the `d.date(...)` to format only dates, or `d.time(...)`
215
+ to format only times.
216
+
217
+ ```typescript
218
+ import { useTranslator } from '@juit/vue-i18n'
219
+
220
+ const translator = useTranslator()
221
+
222
+ const dateTime = translator.d(new Date()) // e.g. '03.02.2025, 18:08:05' in de-DE
223
+ const dateOnly = translator.d.date(new Date()) // e.g. '03.02.2025' in de-DE
224
+ const dateTime = translator.d.time(new Date()) // e.g. '18:08:05' in de-DE
225
+ ```
226
+
227
+ A full [`Intl.DateTimeFormatOptions`][5] set of options can also be specified
228
+ as a second parameter to fine-tune the formatting.
229
+
230
+ The second parameter can also be a simple string `full`, `long`, `medium`, or
231
+ `short`, as a shortcut to `options.dateStyle` or `options.timeStyle`.
232
+
233
+ The default formatting options for each method can be specified at configuration
234
+ time, using the `formats.dateTimeFormat`, `formats.dateOnlyFormat`, or
235
+ `formats.timeOnlyFormat` parameters.
236
+
237
+ By default, they are all set to `medium`.
238
+
239
+
240
+ ## Configuring Types
241
+
242
+ One of the keys to this package is to provide compile-time safety for all
243
+ translation languages (we don't want to forget to translate a message in
244
+ a new language) and translation keys (we don't want to mistype a translation
245
+ key by accident).
246
+
247
+ To do so, we can _merge_ the `I18nConfiguration` interface of this package
248
+ with our specific configurations. Two properties are expected to be defined
249
+ in the configuration:
250
+
251
+ * `languages`: the list of supported languages for the application. These
252
+ are ISO 639-1 language codes, and when specified, _every_
253
+ translation _must_ include a translation for each.
254
+ * `translationKeys`: the list of translation keys known by the application.
255
+ These are the arbitrary keys used to identify the
256
+ messages to be translated with the `t` and `tc`
257
+ methods of `Translator`.
258
+
259
+ To configure the types, follow the example below:
260
+
261
+ ```typescript
262
+ const translations = {
263
+ 'hello': { en: 'Hello, world!', de: 'Hallo, Welt!' }
264
+ } as const satisfies TranslationsOptions
265
+
266
+ declare module '@juit/vue-i18n' {
267
+ export interface I18nConfiguration {
268
+ translationKeys: keyof typeof translations
269
+ languages: 'de' | 'en'
270
+ }
271
+ }
272
+ ```
273
+
274
+ In the example above, if any of the translation objects in our app is missing
275
+ a language (either `en` or `de`), TypeScript will complain.
276
+
277
+ In the same way, if we pass any other string but `hello` to `t(...)` or `tc(...)`,
278
+ TypeScript will report the wrong key.
279
+
280
+
281
+ ## Legal Stuff
282
+
283
+ * [Copyright Notice](NOTICE.md)
284
+ * [License](LICENSE.md)
285
+
286
+
287
+ [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale
288
+ [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
289
+ [3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat
290
+ [4]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options
291
+ [5]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options