From 75774fae2374095dcd7adae046eb33cfdc7e16bb Mon Sep 17 00:00:00 2001 From: jeremy Date: Wed, 24 Jan 2024 00:17:39 +0100 Subject: [PATCH] Fix translator for AdonisJS --- fractal.helpers.js | 8 +++-- .../03-form/03-form-translate-labels.edge | 2 +- fractal/components/03-form/readme.md | 31 ++++++++++++++++--- package.json | 2 +- src/helpers.ts | 4 ++- views/components/form.edge | 2 +- views/components/form/control.edge | 2 +- 7 files changed, 38 insertions(+), 13 deletions(-) diff --git a/fractal.helpers.js b/fractal.helpers.js index b67f2db..8237c75 100644 --- a/fractal.helpers.js +++ b/fractal.helpers.js @@ -67,8 +67,9 @@ const jrmc = { return option.selected ?? false }, - getLabel: (props, context = { translator: { prefix: '' } }) => { - const t = defaultT + getLabel: (props, context = { translator: { prefix: '', t: (k) => k } }) => { + const t = context.translator.t + let name = props.has('name') ? props.get('name') : '' if (props.has('translator.prefix')) { @@ -123,5 +124,6 @@ module.exports = { getNextPageUrl: () => urls[currentPage+1].url, } }, - flashMessages + flashMessages, + t: defaultT } diff --git a/fractal/components/03-form/03-form-translate-labels.edge b/fractal/components/03-form/03-form-translate-labels.edge index f0e6ecc..c2dcf51 100644 --- a/fractal/components/03-form/03-form-translate-labels.edge +++ b/fractal/components/03-form/03-form-translate-labels.edge @@ -1,4 +1,4 @@ -@jrmc.form({ error: false, translator: { prefix: 'user.' }}) +@jrmc.form({ error: false, translator: { prefix: 'user.', t: (key) => t(key) }}) @jrmc.form.control({ name: 'name' }) @!jrmc.form.input({ class: 'input-bordered' }) @end diff --git a/fractal/components/03-form/readme.md b/fractal/components/03-form/readme.md index f56ce96..3059352 100644 --- a/fractal/components/03-form/readme.md +++ b/fractal/components/03-form/readme.md @@ -1,8 +1,29 @@ # Form -| Option | value | | -|---------|------------|--------------------------------------| -| method | GET/POST | HTTP method (default POST) | -| csrf | true/false | add csrf input hidden (default true) | -| error | true/false | show errors message (default true) | +| Option | value | | +|-------------------|--------------|--------------------------------------| +| method | GET/POST | HTTP method (default POST) | +| csrf | true/false | add csrf input hidden (default true) | +| error | true/false | show errors message (default true) | +| translator | object | config for translate | +| translator.prefix | property | prefix translator | +| translator.t | property | function translate | + +## Translator AdonisJS + +``` +{ + prefix: 'user.', + t: (key) => t(key) +} + + +@jrmc.form({ translator: { prefix: 'user.', t: (key) => t(key) }}) + @jrmc.form.control({ name: 'name' }) + @!jrmc.form.input({ class: 'input-bordered' }) + @end +@end + +=> exec i18n helper Adonis t('user.name') +``` diff --git a/package.json b/package.json index 6554d0f..5e4d489 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jrmc/edge-components", - "version": "1.7.0", + "version": "1.7.1", "description": "", "main": "build/index.js", "repository": { diff --git a/src/helpers.ts b/src/helpers.ts index 6110dfe..98f3bf4 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -39,7 +39,9 @@ const obj = { return option.selected ?? false }, - getLabel: (props, context = { translator: { prefix: '' } }, t=(k)=>k) => { + getLabel: (props, context = { translator: { prefix: '', t: (k) => k } }) => { + const t = context.translator.t + let name = props.has('name') ? props.get('name') : '' if (props.has('translator.prefix')) { diff --git a/views/components/form.edge b/views/components/form.edge index f6627d3..0d15095 100644 --- a/views/components/form.edge +++ b/views/components/form.edge @@ -1,6 +1,6 @@ @inject({ object: $props.get('object'), - translator: $props.has('translator') ? $props.get('translator') : null + translator: $props.has('translator') ? $props.get('translator') : { prefix: '', t: (k) => k } })
diff --git a/views/components/form/control.edge b/views/components/form/control.edge index 968a9e9..d7ebb47 100644 --- a/views/components/form/control.edge +++ b/views/components/form/control.edge @@ -14,7 +14,7 @@ @!jrmc.form.label({ text: $props.get('label'), class: 'cursor-pointer', for: jrmc.getId($props), required: jrmc.getRequired($props), }) @end~ @elseif($props.has('name'))~ - @!jrmc.form.label({ text: jrmc.getLabel($props, $context, t ? t : k=>k ), class: 'cursor-pointer', for: jrmc.getId($props), required: jrmc.getRequired($props), }) + @!jrmc.form.label({ text: jrmc.getLabel($props, $context), class: 'cursor-pointer', for: jrmc.getId($props), required: jrmc.getRequired($props), }) @end~ {{{ await $slots.main() || '' }}}