Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-06-10 14:30:20

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

textpacks, gtxt's and placeholders

Thanks to Stef’s beta of plugin_composer, I’m in the act of gtxt’ing a customized rss_admin_db_manager (beta will be released after testing) and have enriched some JS verification messages with several variables.

Example:

REALLY DELETE BACKUPTXP_4.5.4-bot_admin_tooltips.sql“?

Created on 2013-06-09, 00:29:17
Filename on server:
1370730557-TXP_4.5.4-bot_admin_tooltips.sql

Where the coloured parts depict what’s created by variables. I saw in Adi’s textpack that he used the {name} placeholder, so it’s possible to have at least one variable, but how? Textpacks haven’t arrived the wiki and I couldn’t discover which plugin Adi used it in, so can’t see for myself how he’s done it.
And: Is it possible at all to have several placeholders, or would I have to split my gtxt-strings anyway?


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#2 2013-06-10 15:08:09

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: textpacks, gtxt's and placeholders

You can use any placeholder text you like, and as many as you like, it’s just a convention that we’ve adopted to wrap them in curly braces. That being said, the actual call to the gTxt() function with any particular string needs to supply the replaced content. So, given a string called abc_plugin_database_error that is defined in English as Database error in DB {reference}. User "{name}" not found. then this line:

gTxt('abc_plugin_database_error', array('{reference}' => 'localhost', '{name}' => 'Uli'));

would render: Database error in DB localhost. User "Uli" not found.

You can of course pass in variables to placeholder which do the same job. So if you had previously assigned the user to the $user_name variable:

gTxt('abc_plugin_database_error', array('{reference}' => 'localhost', '{name}' => $user_name));

The upshot is that if you’re going through a plugin that has hard-coded strings at present, you may need to add or alter the gTxt() calls to include an array of replacements as its second argument.

Note also that if you are returning error messages that Textpattern is going to display in its message area, you can actually return an array instead of a string. The first array value is the gTxt() function call or its returned string (as above), and the second is the type of error:

return array(gTxt('abc_plugin_database_error', array('{reference}' => 'localhost', '{name}' => $user_name)), E_ERROR);

If you pass that array back, you’ll get a red error box instead of a green ‘ok’ box.. Similarly if you supply E_WARNING you’ll get an orange warning box.

Hope that helps.

EDIT: stupid typos.

Last edited by Bloke (2013-06-10 15:11:19)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2013-06-10 15:16:59

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: textpacks, gtxt's and placeholders

Bloke wrote:

If you pass that array back, you’ll get a red error box instead of a green ‘ok’ box.

Ah, that was another question I’d planned to post.
Thanks for all the detailled tips! :) Will read really carefully when I’m back from rehearsal.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#4 2013-06-10 22:00:08

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: textpacks, gtxt's and placeholders

Now I understand why there’s always the laconic standard request “Are you sure?” ;)
But I’ve it working, yay!

Thanks again, Stef, also for your ready-to-copy code!


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#5 2013-06-14 13:40:31

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: textpacks, gtxt's and placeholders

Is there a convention as to when to use E_WARNING instead of E_ERROR?


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#6 2013-06-14 15:43:06

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: textpacks, gtxt's and placeholders

uli wrote:

Is there a convention as to when to use E_WARNING instead of E_ERROR?

It’s pretty much up to you. I think the broad separation is something like:

  • E_ERROR is some (possibly non-recoverable) failure that directly impacts the action the user took. e.g. save failed; backup failed; missing required input; server gone BANG; etc.
  • E_WARNING indicates some action that the user needs to be aware of which doesn’t necessarily halt proceedings. These may lead to E_ERRORs in future if not fixed.

Examples of E_WARNINGs might be if the system reverted to using a default setting because the user didn’t specify an input value, or there’s some naming collision which doesn’t prevent the program from working, but might lead to problems later. Stuff like that.

Last edited by Bloke (2013-06-14 15:44:25)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#7 2016-05-11 15:40:53

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: textpacks, gtxt's and placeholders

I’m currently making stm_article_order textpack-ready (Yay!) and have a knotty case there:

I’m offering a string for translation that contains an anchor tag (URL built from $prefs["siteurl"] and a string, fiddling prohibited) that ideally wraps an existing gTxt string. I thought I might dissect the anchor into an array of three {parts} (opening a tag, gTxt, closing a tag) (booh!) but my brain hurts even in such a workaround from “when am I in PHP, when in HTML, which apostrophes, dots or not” etc. Is there an easier, more elegant, way? How might such a nesting look like?

Edit: Forgot to mention the variable.

Last edited by uli (2016-05-11 15:50:55)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

Board footer

Powered by FluxBB