Forums

[Solved] translate JS validation?

SAGO 19 Jan, 2012
Hi,

I'm using v3.2

There is one form that I'd like to have in two languages.
I tried the tutorial and you can translate the labels.
But are the following things possible:

1) Translate the messages in jsvalidation2.js like 'This field is required'
Can this be done with just adding a language file (ini)?

2) If it's not possible like asked in 1, would it be possible to do something in jsvalidation2.js where you have this code:
$ES('input', $(chronoformname)).each(function(field){
		var message_required = 'This field is required';
		var message_validate_number = 'Please enter a valid number in this field.';

Perhaps check which language is selected by looking at index.php?lang=en or index.php?lang=nl
And just using a If-structure in the js file.

3) If there is a other or better way to do this, please let me know.

Thanks.
SAGO 22 Jan, 2012
Hi,

I just want to post a solution for V3.2.

As Bob and Max suggested in the following threads you can assign a title in the Form HTML Code.
And then use the multilanguage plugin and add a string for that title.
Bob's post: http://www.chronoengine.com/forums.html?cont=posts&f=5&t=21671#p69862
Max's post: http://www.chronoengine.com/forums.html?cont=posts&f=5&t=13987&p=30346&hilit=Title+translate+validation+message#p30346
Explanation from Max:

The only way to do this is to setup a custom validation message per field like "REQUIRED MESSAGE" and then translate this, you can setup the custom validation message in the wizard or by adding title tags to your form fields in HTML, for example you have a required email field, set the title to "EMAIL MESSAGE", now at the multi language config, use
en-GB
EMAIL MESSAGE=please write a correct email
es-ES
EMAIL MESSAGE=por favor escribir ... etc



This seem to work but there is a small problem when a field can have multiple kind's of error's.

A better solution.
What you can do is have multiple jsvalidation2.js like jsvalidation2-en.js jsvalidation2-nl.js jsvalidation2-fr.js

And then add a small piece of code that would get the active language and choose the file needed.

What you need to do is the following:

Go and edit this file components/com_chronocontact/chronocontact.html.php
Around line 126 you will see the following code:
<script src="<?php echo $CF_PATH.'components/com_chronocontact/js/'; ?>jsvalidation2.js" type="text/javascript"></script>

Change it by:
<?php
		$lang =& JFactory::getLanguage();
		$langcode= array_shift(explode('-', $lang->getTag()));
		?>		
		<script src="<?php echo $CF_PATH.'components/com_chronocontact/js/'; ?>jsvalidation2-<?php echo $langcode; ?>.js" type="text/javascript"></script>


What this those is get the active language like nl-NL and get the first two letters and then select jsvalidation2-nl.js
Of course this depends which language you have installed.

Now you need to add your own js file in the following location:
components/com_chronocontact/js

So copy jsvalidation2.js and make for each language you have a version like I said before (for example jsvalidation2-nl.js) and upload it. So you need to "-nl" (or whatever langcode you wish to add without the quotes of course)

In the js-file you will need to go to line 60 and change/translate the values there:
var message_required = 'This field is required';
var message_validate_number = 'Please enter a valid number in this field.';
And so...


Hope this helps someone who uses V3.2 and sorry for my english.

Cheers,

Sago
GreyHead 22 Jan, 2012
Hi SAGO,

Nice solution thank you - you'll be pleased to know that the validation library in CFv4 has separate language files.

Bob
SAGO 22 Jan, 2012
Hi Bob,

Great to hear that.
I will definitely try V4 soon and look how the validation work.

Regards,

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