Forums

Allow the form to validate in the browser's language

lowrykun 22 Aug, 2011
I support 8 languages and am using Chronoforms multi-language action for translations (see my current posts below). That part is working great.

My problem is that even though the form would be localized, the validation strings were not. After some research I noticed the validation language setting on the form. Then I found that this is loaded in cfaction_show_html.php.

Here's my proposed fix:
Add the following to the top of function _loadValidationScripts($form){

//This just gets the language tag (en vs en-US)
$lang = JLanguageHelper::detectLanguage();
$languages = JLanguageHelper::getLanguages();
foreach ($languages as $language){
  if($language->lang_code == $lang){
    $current_language = $language->sef;
  }
}


Then change the following line from:
$document->addScript($CF_PATH.'components/com_chronoforms/js/formcheck/lang/'.$form->form_params->get('jsvalidation_lang', 'en').'.js');


To:

$js_lang_file = 'components/com_chronoforms/js/formcheck/lang/'. $current_language .'.js';
if (file_exists($js_lang_file)) {
	$document->addScript($CF_PATH.$js_lang_file);
} else {
	$document-addScript($CF_PATH.'components/com_chronoforms/js/formcheck/lang/'.$form->form_params->get('jsvalidation_lang', 'en').'.js');
}


This way it uses the browser language to pick the validation to display. If you have suggestions for simplifying then I'm interested. I know that I could just keep the first two letters of the locale, but did it this way in case the user (me) set the SEF language tag different than the first letters in the locale.

I have to support Norwegian and Finnish and don't see them in your list. I'll assume you're interested in adding these after they are translated?
GreyHead 23 Aug, 2011
Hi lowrykun,

I think that you can add extra language translations for the Validation code in the components/com_chronoforms/js/formcheck/lang folder - your code will then automatically pick them up.

Bob

PS My Show HTML [GH} action includes some code like this and also gives you a box for FormCheck options.
lowrykun 23 Aug, 2011
After digging through the Chronoform code yesterday, here's how it works...

Each form has a Validation Language set on the form. There is a drop down box that allows the administrator to select exactly one language. This is the language that is used to display the validation to the user. However, you also have a Multi-Language Action which allows you to create a single form but localize it to several languages.

For example, you create a single simple form that uses Multi-language action to display in English and French. This works!

But in the form you must select only ONE language to validate in (jsvalidation_lang parameter below). So even though the form displays in French, the validation ("This field is required") continues to show up in English.

Here's the code that sets the jsvalidation_lang parameter:

<?php echo $HtmlHelper->input('params[jsvalidation_lang]', array('type' => 'select', 'label' => 'JS Validation Language', 'value' => $params->get('jsvalidation_lang', 'en'), 'options' => array('en' => 'English', 'fr' => 'French', 'de' => 'Deutsch', 'nl' => 'Dutch', 'es' => 'Spanish', 'da' => 'Danish', 'it' => 'Italian', 'jp' => 'Japanese', 'cn' => 'Chinese', 'ru' => 'Russain', 'pt' => 'Portugese', 'gr' => 'Greek', 'pl' => 'Polish', 'ro' => 'Romanian', 'fa' => 'Farsi'), 'default' => 'en')); ?>


Then a specific language file is loaded here:

    $document->addScript($CF_PATH.'components/com_chronoforms/js/formcheck/lang/'.$form->form_params->get('jsvalidation_lang', 'en').'.js');


The change that I proposed above removes the need for this parameter (or essentially disables it) in favor of setting the validation based on the user's browser's language settings. If I add additional languages, then it will work fine with my code, but your code won't work until the line above is edited - and then it will still only work in the language selected - not if you use the Multi-Language Action to localize the form.

I'm proposing that you change the Form Validation code to also include a "Automatically Detect" option that uses my code, or allow the user to select a specific language (your existing code).

To be more clear, I'm having Norwegian and Finnish translated now and would like to donate the language files to you when they are completed.
GreyHead 23 Aug, 2011
Hi lowrykun,

Thanks - that matches my understanding. And thanks for the language files too.

As I said, my Show HTML [GH] action from here includes the code to automatically select the language in use.

Bob
lowrykun 23 Aug, 2011
Ah! Thanks for the link. That now makes more sense.
Max_admin 28 Aug, 2011
Hi,

As a side note, for other readers, you can change a form parameter on the fly to alter something dynamically, for example, in this situation we can use the code below in a "Custom Code" action BEFORE the "show HTML" action to alter the validation language on the fly:


<?php
//line below will switch the validation language to french, regardless of which language is selected in the form edit page
$form->form_params->set('jsvalidation_lang', 'fr');
?>


Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
lowrykun 23 Nov, 2011
Here's the Norwegian translation for the validation form. It wouldn't allow me to upload a .js file through the Forum.

formcheckLanguage = {
	required:"Tämä kenttä on pakollinen.",
	alpha:  "Tämä kenttä hyväksyy kirjaimia vain.",
	alphanum:  "Tämä kenttä hyväksyy kirjaimia vain.",
	nodigit:  "Ei numeroa hyväksytään.",
	digit:  "Anna kelvollinen kokonaisluku.",
	digitmin:  "Numero on oltava vähintään %0",
	digitltd:  "Arvon on oltava välillä %0, %1",
	number:  "Anna kelvollinen numero.",
	email:  "Anna voimassa oleva sähköpostiosoite: <br /> <span> Esim. yourname@domain.com </ span>",
	image :  "Tämä kenttä saa sisältää vain kuvatyyppien",
	phone:  "Anna kelvollinen puhelin.",
	url:  "Anna kelvollinen URL: <br /> <span> Esim. http://www.domain.no</span>",
	confirm:  "Tämä kenttä eroaa %0",
	differs:  "Tämä luku on erilainen on %0",
	length_str:  "Pituus on väärä, se on välillä %0,%1",
	length_fix:  "Pituus on väärä, se on täsmälleen %0 merkkiä",
	lengthmax:  "Pituus on väärä, se on maksimi %0",
	lengthmin:  "Pituus on väärä, se on vähintään %0",
	words_min:  "Tämä kenttä on concain vähintään %0 sanoen tällä hetkellä: %1 sanaa",
	words_range:  "Tämä kenttä on sisällettävä %0 %1 - eli tällä hetkellä: %2 sanaa",
	words_max:  "Tämä kenttä tulee sisältää max %0 sanoen tällä hetkellä: %1 sanaa",
	checkbox:  "Rasti ruutuun",
	checkboxes_group:  "Tarkista vähintään %0 laatikko (ES)",
	radios: "Valitse radio",
	select: "Valitse arvo",
	select_multiple:  "Valitse ainakin yksi arvo"
}
GreyHead 24 Nov, 2011
Hi lowrykun ,

Tank you very much, hopfully Max will add this to a future release.

Bob
karinaga 07 Dec, 2011
Is this feature added already?
GreyHead 07 Dec, 2011
Hi karinaga,

Which feature are you asking about? There are several language choices already installed, Norwegian isn't one of them.

Bob
This topic is locked and no more replies can be posted.