Was thinking and wondering if October has any built in methods to validate multiple languages. Take the following example:
fname:
label: acme.example::lang.contactForm.firstName
type: text
span: left
lname:
label: acme.example::lang.contactForm.lastName
type: text
span: right
public $rules = [
'fname' => [ 'nullable', 'regex:/(^[a-zA-Z\s\']+$)+/', 'max:100' ],
'lname' => [ 'nullable', 'regex:/(^[a-zA-Z\s\']+$)+/', 'max:100' ],
];
In the above example let's say we want to validation an english first and last name.
It also links to the lang file: lang/en/lang.php
Now let's say I want to create a chinese lang file as well, e.g. lang/cn/lang.php
I don't want to Validate the contact form using a Latin character set, instead now I want to validate it using a Chinese character set.
// English
if ($lang === 'en') {
public $rules = [
'fname' => [ 'nullable', 'regex:/(^[a-zA-Z\s\']+$)+/u', 'max:100' ],
'lname' => [ 'nullable', 'regex:/(^[a-zA-Z\s\']+$)+/u', 'max:100' ],
];
// Chinese
} elseif ($lang === 'cn') {
public $rules = [
'fname' => [ 'nullable', 'regex:/(^[\p{Han}+\s]+$)+/u', 'max:100' ],
'lname' => [ 'nullable', 'regex:/(^[\p{Han}+\s]+$)+/u', 'max:100' ],
];
}
Above: Using a boring if statement to load each validation for each language. BUT not sure what October API, I could use to know and confirm the loaded lang file to use for the $lang string.
I was wondering if there was a magic October CMS solution?
Maybe something like this (that directly connects to the Translate files):
public $rules = [
'fname' => [ 'nullable', 'regex:/(^[a-zA-Z\s\']+$)+/u', 'max:100', 'lang:en' ],
'fname' => [ 'nullable', 'regex:/(^[\p{Han}+\s\']+$)+/u', 'max:100', 'lang:cn' ],
'lname' => [ 'nullable', 'regex:/(^[a-zA-Z\s\']+$)+/u', 'max:100', 'lang:en' ],
'lname' => [ 'nullable', 'regex:/(^[\p{Han}+\s\']+$)+/u', 'max:100', 'lang:cn' ],
];
Or is there any special October API I could use to get the current backend loaded lang file - to connect up to the validation code?
Just wondering what would be an official solution and what could be an enhancement solution.
Thanks.
I believe you're looking for App::locale() which will return the currently active locale for the application.
Github issues really isn't the place for questions like this. Please use one of the many other avenues available instead of submitting new issues for your questions.
Discord: https://discord.gg/gEKgwSZ
Forum: https://octobercms.com/forum
StackOverflow: https://stackoverflow.com/questions/tagged/octobercms
@LukeTowers Thanks for the answer, will use Discord next time.