Skip to content

Commit

Permalink
chore: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Georget committed Nov 17, 2023
0 parents commit 9cf92be
Show file tree
Hide file tree
Showing 42 changed files with 9,947 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_size = 2
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": ["@nuxt/eslint-config"]
}
56 changes: 56 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Dependencies
node_modules

# Logs
*.log*

# Temp directories
.temp
.tmp
.cache

# Yarn
**/.yarn/cache
**/.yarn/*state*

# Generated dirs
dist

# Nuxt
.nuxt
.output
.data
.vercel_build_output
.build-*
.netlify

# Env
.env

# Testing
reports
coverage
*.lcov
.nyc_output

# VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Intellij idea
*.iml
.idea

# OSX
.DS_Store
.AppleDouble
.LSOverride
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shamefully-hoist=true
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"i18n-ally.localesPaths": [
"playground/locales",
"src/locales"
]
}
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Nuxt ZodI18n

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![License][license-src]][license-href]
[![Nuxt][nuxt-src]][nuxt-href]

A Nuxt Module for localizing zod error messages.

Check the playground for usage doc and test will come later

- [ Release Notes](/CHANGELOG.md)
<!-- - [🏀 Online playground](https://stackblitz.com/github/xibman/nuxt-zod-i18n?file=playground%2Fapp.vue) -->
<!-- - [📖 &nbsp;Documentation](https://example.com) -->

## Features

<!-- Highlight some of the features your module provide here -->
- This library depends on [@nuxtjs/i18n](https://i18n.nuxtjs.org/) .
- Provide a Global error map for zod see [Zod ERROR_HANDLING](https://zod.dev/ERROR_HANDLING?id=global-error-map)
- Translation for zod errors based on [ZodIssueCode](https://zod.dev/ERROR_HANDLING?id=zodissuecode)
- A way to translate custom errors

## Quick Setup

1. Add `nuxt-zod-i18n` dependency to your project

```bash
# Using pnpm
pnpm add -D nuxt-zod-i18n

# Using yarn
yarn add --dev nuxt-zod-i18n

# Using npm
npm install --save-dev nuxt-zod-i18n
```

2. Add `nuxt-zod-i18n` to the `modules` section of `nuxt.config.ts`

```js
export default defineNuxtConfig({
modules: [
'nuxt-zod-i18n'
]
})
```

That's it! You can now use Nuxt ZodI18n in your Nuxt app ✨

## Development

```bash
# Install dependencies
npm install

# Generate type stubs
npm run dev:prepare

# Develop with the playground
npm run dev

# Build the playground
npm run dev:build

# Run ESLint
npm run lint

# Run Vitest
npm run test
npm run test:watch

# Release new version
npm run release
```

<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/v/nuxt-zod-i18n/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
[npm-version-href]: https://npmjs.com/package/nuxt-zod-i18n

[npm-downloads-src]: https://img.shields.io/npm/dm/nuxt-zod-i18n.svg?style=flat&colorA=18181B&colorB=28CF8D
[npm-downloads-href]: https://npmjs.com/package/nuxt-zod-i18n

[license-src]: https://img.shields.io/npm/l/nuxt-zod-i18n.svg?style=flat&colorA=18181B&colorB=28CF8D
[license-href]: https://npmjs.com/package/nuxt-zod-i18n

[nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js
[nuxt-href]: https://nuxt.com
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "nuxt-zod-i18n",
"version": "1.0.0",
"description": "A Nuxt Module for localizing zod error messages.",
"repository": "xibman/nuxt-zod-i18n",
"license": "MIT",
"type": "module",
"exports": {
".": {
"types": "./dist/types.d.ts",
"import": "./dist/module.mjs",
"require": "./dist/module.cjs"
}
},
"main": "./dist/module.cjs",
"types": "./dist/types.d.ts",
"files": [
"dist"
],
"scripts": {
"prepack": "nuxt-module-build build",
"dev": "nuxi dev playground",
"dev:build": "nuxi build playground",
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
"lint": "eslint .",
"test": "vitest run",
"test:watch": "vitest watch"
},
"dependencies": {
"@nuxt/kit": "^3.8.1",
"defu": "^6.1.3",
"zod": "^3.22.4"
},
"devDependencies": {
"@nuxt/devtools": "latest",
"@nuxt/eslint-config": "^0.2.0",
"@nuxt/module-builder": "^0.5.3",
"@nuxt/schema": "^3.8.1",
"@nuxt/test-utils": "^3.8.1",
"@nuxtjs/i18n": "npm:@nuxtjs/i18n-edge",
"@types/node": "^20.8.10",
"changelogen": "^0.5.5",
"eslint": "^8.53.0",
"nuxt": "^3.8.1",
"vitest": "^0.33.0"
}
}
18 changes: 18 additions & 0 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
<div>
<header class="bg-background/75 backdrop-blur border-b -mb-px sticky top-0 z-50 border-gray-200 dark:border-gray-800">
<div class="mx-auto px-4 sm:px-6 lg:px-8 max-w-7xl flex items-center justify-between gap-3 h-16">
<VNavigation />
<VLocalSwitcher />
</div>
</header>
<main class="min-h-[calc(100vh - 4rem)]">
<UContainer>
<div class="flex flex-col lg:gap-8 py-8">
<NuxtPage />
</div>
</UContainer>
</main>
</div>
</template>

60 changes: 60 additions & 0 deletions playground/components/VLocalSwitcher.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<script setup lang="ts">
import type { LocaleObject } from '#i18n';
import type { DropdownItem } from '#ui/types'
const { locale, locales } = useI18n()
const switchLocalePath = useSwitchLocalePath()
const items = computed<DropdownItem[][]>(() => {
const availableLocales = (locales.value as LocaleObject[]).filter(i => i.code !== locale.value);
const data = availableLocales.map(({ code }) => {
return {
label: code,
to: switchLocalePath(code),
icon: `i-flag-${code.split('-')[1].toLowerCase()}-4x3`
}
})
return [data]
});
</script>

<template>
<div>
<UDropdown :items="items">
<UButton
color="white"
trailing-icon="i-heroicons-chevron-down-20-solid"
>
<UIcon
:name="`i-flag-${locale.split('-')[1].toLowerCase()}-4x3`"
dynamic
/>
{{ locale }}
</UButton>

<template #item="{ item }">
<UIcon
:name="item.icon"
dynamic
/>
<span class="truncate">{{ item.label }}</span>
</template>
</UDropdown>
</div>

<!-- <template v-for="availableLocale in availableLocales" :key="availableLocale.code">
<NuxtLink :to="switchLocalePath(availableLocale.code)">
{{ availableLocale.code }}
<Icon :name="`flag:${availableLocale.code.split('-')[1].toLowerCase()}-4x3`" color="black" />
</NuxtLink>
</template> -->
</template>
31 changes: 31 additions & 0 deletions playground/components/VNavigation.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script setup lang="ts">
const localeRoute = useLocaleRoute()
const items = [
{
label: 'Home',
name: 'index',
},
{
label: 'Complex',
name: 'complex'
}
]
</script>
<template>
<ul class="flex gap-6">
<li
v-for="{ name, label } in items"
:key="name"
>
<ULink
:to="localeRoute({ name })"
active-class="text-primary"
inactive-class="text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200"
>
{{ label }}
</ULink>
</li>
</ul>
</template>
8 changes: 8 additions & 0 deletions playground/locales/en-GB.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"colors": {
"red": "red",
"green": "green"
},
"myCustomError": "Custom error",
"myCustomErrorWithInterpolation": "My preferred colors are {first} and {second}"
}
8 changes: 8 additions & 0 deletions playground/locales/fr-FR.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"colors": {
"red": "rouge",
"green": "vert"
},
"myCustomError": "Erreur custom",
"myCustomErrorWithInterpolation": "Mes couleurs préférées sont {first} et {second}"
}
37 changes: 37 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export default defineNuxtConfig({
modules: ["../src/module", "@nuxtjs/i18n", "@nuxt/ui"],
devtools: { enabled: true },
ui: {
icons: {
dynamic: true
}

},
i18n: {
defaultDirection: "ltr",
defaultLocale: "en-GB",
langDir: "./locales",
lazy: true,
locales: [
{
code: "en-GB",
dir: "ltr",
file: "en-GB.json",
flag: "gb",
isCatchallLocale: true,
iso: "en-GB",
name: "English",
},
{
code: "fr-FR",
dir: "ltr",
file: "fr-FR.json",
flag: "fr",
iso: "fr-FR",
name: "French",
},
],
strategy: "prefix_except_default",
types: "composition",
},
});
Loading

0 comments on commit 9cf92be

Please sign in to comment.