-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide validator messages via translations
- Loading branch information
Harminder Virk
authored and
Harminder Virk
committed
Oct 12, 2021
1 parent
6b1044c
commit 9b76e12
Showing
7 changed files
with
311 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* @adonisjs/i18n | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { I18nManagerContract } from '@ioc:Adonis/Addons/I18n' | ||
import { HttpContextConstructorContract } from '@ioc:Adonis/Core/HttpContext' | ||
|
||
/** | ||
* Shares the i18n with the HTTP context as a getter | ||
*/ | ||
export function contextBindings( | ||
Context: HttpContextConstructorContract, | ||
I18n: I18nManagerContract | ||
) { | ||
Context.getter('i18n', () => I18n.locale(I18n.defaultLocale), true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* @adonisjs/i18n | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { validator } from '@ioc:Adonis/Core/Validator' | ||
import { I18nManagerContract } from '@ioc:Adonis/Addons/I18n' | ||
|
||
/** | ||
* Registers a hook to deliver default messages to the validator. | ||
*/ | ||
export function validatorBindings(Validator: typeof validator, I18n: I18nManagerContract) { | ||
Validator.messages((ctx) => { | ||
if (ctx && 'i18n' in ctx === true) { | ||
return ctx.i18n.validatorMessages() | ||
} | ||
|
||
return I18n.locale(I18n.defaultLocale).validatorMessages() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* @adonisjs/i18n | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { ViewContract } from '@ioc:Adonis/Core/View' | ||
import { I18nManagerContract } from '@ioc:Adonis/Addons/I18n' | ||
|
||
/** | ||
* Registers the "t" helper and the i18n instance for the default | ||
* locale. | ||
* | ||
* HTTP requests can share the request specific i18n with the template | ||
* to overwrite the default one | ||
*/ | ||
export function viewBindings(View: ViewContract, I18n: I18nManagerContract) { | ||
/** | ||
* The "i18n" is a reference to the default locale instance. | ||
*/ | ||
View.global('i18n', I18n.locale(I18n.defaultLocale)) | ||
|
||
/** | ||
* The "t" helper to translate messages within the template | ||
*/ | ||
View.global('t', function (...args: any[]) { | ||
return this.i18n.formatMessage(...args) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.