Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2005-11-02 05:31:22

boblet
Member
Registered: 2005-08-08
Posts: 53

plugins and i18n/translations

Hi All,

I’m hoping someone can give me a little advice on a couple of things:

First, what’s the best way to take a kanji input value from a tag calling a plugin and add it as an HTML value? For example (from zem_contact):
<code>
$name = (empty($atts[‘name’]) ? preg_replace(‘/\W/’, ‘’, $label) : $atts[‘name’]);
if (ps($name))
return ‘<div><label for=”’.$name.’”>’;
</code>

With a kanji input value ($name) all I get are a bunch of ???s as output. However with kanji in $label, the $l variable displays it without problems:
<code>
$label = (empty($atts[‘label’]) ? ‘Text’ : $atts[‘label’]); $l = ($required ? “<strong>$label</strong>” : $label);
if (ps($name)) $zem_contact_form[$label] = $v; $l = ($required ? “<strong>$label</strong>” : $label); return ‘<div><label for=”’.$name.’”>’.$l;
</code>

I’m guessing something in the preg_replace or ps function is killing it, what should I do instead?

Second, has there been a consensus on translating plugins? I noticed Kusor’s comment “Plugins i18n: Of course, we can translate a set of strings for a given plugin, and share them with Textpattern forum users. – Allow plugins to store their own data on the prefs table” and I can see one plugin storing variables in there. Are there any plugins using this method for i18n that you could point me at?

As background I’m trying to internationalise Zem’s most excellent zem_contact without knowing much about PHP :-) I’ve got most of the way there by turning all error text into variables with defaults that can be overridden from the tag input. However I can’t work out how to get around $name getting garbled, variables added to a tag input not being interpreted unless the input is all English, and for some reason the email address won’t display only for the Japanese version (weird as there’s no kanji involved in the $email variable).

thanks in advance!

peace – boblet

Offline

#2 2005-11-20 14:50:57

fpradignac
Plugin Author
From: France near Cognac
Registered: 2005-01-29
Posts: 359
Website

Re: plugins and i18n/translations

Hello boblet,

Did you have some solution now with ZEM_CONTACT ?
I have the same problem in French with a lot of accents : a label “n° de téléphone :” can’t be sent so I use html code “n&deg; de t&eacute;l&eacute;phone :” but it’s not very clean in the receipt form.


françois

Offline

#3 2005-11-26 05:23:33

boblet
Member
Registered: 2005-08-08
Posts: 53

Re: plugins and i18n/translations

Hey Fpradignac,

nope :-( I guess I should have posted to a different forum as there have been no informative replies. Hrm…

Offline

#4 2005-11-30 10:09:25

Finnish
Member
From: Russia, Vladivostok
Registered: 2005-11-02
Posts: 13

Re: plugins and i18n/translations

hi, if you have a problem with non-english input being messed in zem_contact – look here:
http://forum.textpattern.com/viewtopic.php?pid=88436#p88436


have a nice day :)

Offline

#5 2005-11-30 16:29:20

boblet
Member
Registered: 2005-08-08
Posts: 53

Re: plugins and i18n/translations

Hey Finnish,

thanks for the message, but merely changing to htmlspecialchars isn’t enough for double-byte characters – it’s definitely the preg_replace that’s messing with me. I guess the language you’re using was safe because according to php.net’s pattern syntax

For example, in the “fr” (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w.

Unfortunately because PHP5 is still UTF8-naive it seems kanji are not matched. However, your message prompted me to start randomly deleting stuff, and replacing $name = (empty($atts['name']) ? preg_replace('/\W/', '', $label) : $atts['name']); with $name = (empty($atts['name']) ? $label : $atts['name']); in several places gives me kanji labels. I dunno if the stripping of non-word characters was essential to $name’s definition or not, but it seems to be working here.

peace – boblet

Offline

#6 2005-11-30 21:39:24

fpradignac
Plugin Author
From: France near Cognac
Registered: 2005-01-29
Posts: 359
Website

Re: plugins and i18n/translations

Salut boblet,

Well I see that you’re working “hard” on zem-contact.
I’ve tried to “simply” replaced strings like mentionned in your post : I can’t send my message. Did you changed something elsewhere ? I think so … and I hope you can help me ;-)

thanks,
françois.


françois

Offline

#7 2005-12-01 00:54:58

boblet
Member
Registered: 2005-08-08
Posts: 53

Re: plugins and i18n/translations

I’ll write up what I changed in the next day or so – it’s a little … involved ;-) However it is definitely working

peace – boblet

Offline

#8 2005-12-01 02:50:41

boblet
Member
Registered: 2005-08-08
Posts: 53

Re: plugins and i18n/translations

Internationalizing zem_contact

download link for my hack at the bottom

I wanted to use zem_contact for a bilingual site, but a lot of English is hard-coded into the plugin in the form of error messages. I changed these to variables with default English text (eg $errorlabel = (empty($atts['errorlabel']) ? "Required field <strong>$label</strong> is missing" : $atts['errorlabel']);) that can be replaced when calling the plugin. After these changes I also had to remove the preg_replace function from $name definitions that strips whitespace from label names, as it killed any double-byte characters (eg from $name = (empty($atts['name']) ? preg_replace('/\W/', '', $label) : $atts['name']); to $name = (empty($atts['name']) ? $label : $atts['name']);). It’s now up to me to make sure there’s no whitespace in these values.

Variables added (with default English text):

function zem_contact

  • $errorfailure Unable to send email
  • $errorheader Error!
  • $errorpara Please check and try again
  • $erroremailto No <strong>email address</strong> specified
  • $thankspage (if present the user will be redirected to this relative URL after the email is sent successfully)

function zem_contact_text

  • $errorlabel Required field <strong>$label</strong> is missing
  • $errorminlength <strong>$label</strong> must be at least $min long

function zem_contact_textarea

  • $errorlabel
  • $errorminlength

function zem_contact_email

  • $errorlabel
  • $errorvalidemail is not a vaild email address
  • $errorvalidhost is not a valid email host

function zem_contact_select

  • $errorlabel
  • $errornewlabel Unknown $label $v

function zem_contact_checkbox

  • $errorlabel*

function zem_contact_submit

  • $errorexpired Form expired, please try again

This allows us to specify the above variables when calling zem_contact, for example:

<txp:if_section name="contact.en">
<txp:zem_contact mailto="your@email.address" thankspage="contact/thank-you.en">
<txp:zem_contact_text label="Name" break="" required="1" /><br />
<txp:zem_contact_email break="" /><br />
<txp:zem_contact_text label="Subject" break="" /><br />
<txp:zem_contact_textarea label="Message" break="" required="1" /><br />
<txp:zem_contact_serverinfo name="REMOTE_ADDR" label="IP" />
<txp:zem_contact_serverinfo name="HTTP_USER_AGENT" label="Agent" />
<txp:zem_contact_submit />
</txp:zem_contact>
<txp:else />
<txp:zem_contact mailto="your@email.address" thankspage="contact/thank-you.ja" thanks="ありがとうございました!" errorfailure="メールを遅れませんでした。" errorheader="エラー!" errorpara="このことをチェックして、もう一度送って下さい。">
<txp:zem_contact_text label="名前" break="" required="1" errorlabel="のがいります" /><br />
<txp:zem_contact_email label="メールアドレス" break="" errorvalidemail="は正しいメールアドレスではない" errorvalidhost="は正しいメールホーストではない" errorlabel="のがいります" /><br />
<txp:zem_contact_text label="トピック" break="" /><br />
<txp:zem_contact_textarea label="メッセージ" break="" required="1" errorlabel="のがいります" /><br />
<txp:zem_contact_serverinfo name="REMOTE_ADDR" label="IP" />
<txp:zem_contact_serverinfo name="HTTP_USER_AGENT" label="Agent" />
<txp:zem_contact_submit errorexpired="フォームはもう期間経ので、もう一度して下さい。" />
</txp:zem_contact>
</txp:if_section>

(Although the Japanese is munged above by the Textile code tag, it is just standard Japanese, eg thanks=“ありがとうございました!”)

I don’t know how to allow html inside the variables when calling the plugin (eg to highlight a word), so have changed several of the error messages to add html formatting in the plugin rather than the variable, or have just not worried about it for the non-default language. $errorvaildemail is an example of the former:

$errorvalidemail = (empty($atts['errorvalidemail']) ? "is not a valid email address" : $atts['errorvalidemail']); // error text defined
$zem_contact_error[] = "'<strong>$email</strong>' $errorvalidemail"; // error text appended to variable which already has html formatting

I also haven’t added anything for form field default text. I’ll add a link to my version of the plugin once I add the changes from Destry’s docs to it.

I also added a combination of methods for thanks, including adding $thankspage variable. If this is present in the called plugin, it’ll redirect to the relative URL on success. If not it’ll fall back to displaying the $thanks form

if ($r) {
	$_POST = array();
	if ($thankspage) {
		header("HTTP/1.x 301 Moved Permanently");
		header("Status: 301");
		header("Location: $thankspage");
		header("Connection: close");
	} else {
		$out .= "<p id=\"form-attn\">$thanks</p>";
		$f = parse($form);
	}
}
else {
	$out .= "<div id=\"form-attn\">\n<h4>$errorheader</h4>\n<p>$errorfailure</p>\n</div>";
}

Here’s the CSS I’m using:

<code>
form {margin: 1em 0;}
fieldset {border: none;}
form p+p {margin: 0.7em 0; }
label {display: block; float: left; width: 8em; margin: 0 1em 0 0; text-align: right;}
#submit {display: block; float: right;}
div#form-attn {margin-top: 1em; padding: 1em 1em 1em; background: #FFFFA5;}
div#form-attn h4 {margin-top: 0; margin-bottom: 0.5em; color: #B81C25;}
div#form-attn li {color: red;}
p#form-attn {color: green; font-weight: bold;}
</code>

I’ve heavily changed this plugin eg semantic over b tags, errors are wrapped, errors appear before the form not after, different CSS hooks etc. If you use this hack please remember:

  • I know nothing about PHP, and this hackery (butchery) is probably making Zem cry/killing fluffy kittens
  • you will need to go through the code yourself to work out what is happening and what to style, plus probably change some headers I hard-coded in etc
  • it works for me, but it might not for you
  • I haven’t made an installable plugin so you’ll have to c&p over an already-installed copy using plugin > edit
  • all the bugs Destry mentioned are fixed, and the additions (checkbox memory, better thanks page redirection) added
  • did I mention I know nothing about PHP?

ok, have at it: zem_contact-0.6a (‘dead kitten’ release)

PS Destry or a forum mod – would it be possible to put a scroll bar on code blocks that are to wide? I know you can get to it via “Quote”, but a scroll bar would be nicer ;-)

Last edited by boblet (2005-12-02 12:32:08)

Offline

Board footer

Powered by FluxBB