Title says it all, how do I use the Context field? What constitutes context — a path, a node title? Not sure what to put into the field to enable context.

Saw this issue: http://drupal.org/node/1057894 perhaps the capability is not complete yet? Either way, looks promising!

Comments

RobLoach’s picture

Status: Active » Needs review

Context isn't really used in many situations. You probably don't have to worry about it... See the t() "context" parameter. If #1135950: Remove static caching in t() got in, it would allow us to do some very interesting things with it though.

AaronBauman’s picture

If it's not relevant until D8, why include it in the D7 version?
Do we ever really expect that D8 context will be backported into D7?

RobLoach’s picture

Would need to go into Drupal 8, and then backported to Drupal 7.

AaronBauman’s picture

Right - I understand that.
I'm thinking that if it's not currently useful, it should not be included in the interface.
What do you think about removing it until context becomes more widely useful / documented?
The documentation you linked to for t() provides a circular definition of context.
How about at least providing a configuration option or permission for hiding it if it's not going to be documented in this module?

JamesOakley’s picture

I'm using the context feature of this module. Here's my use case - in Ubercart, sometimes you'd want to translate "check" into "cheque" (for UK users). But only when Ubercart refers to a financial check; if the context was "check your order before pressing continue", you wouldn't want it to say "cheque your order"!

The Ubercart maintainers have given the t() calls a context of "cheque" when a financial kind of check is meant. I can then use String Overrides to override check to cheque, but only when the context is cheque. The great news is - it works.

What I've given is not documentation, but sometimes a real-world use-case helps sheds more light than formal documentation - I hope this helps someone.

mardale’s picture

This looks really interesting - I'm running a booking and membership site and would really like to be able to use different texts for buttons in context ("book now", "buy membership", "add to cart".

I'm already using stringsoverrides for other global issues, but if there is a way to use the context field to achieve my goal I'd be very happy.

JamesOakley, can you share how you got the context field to work?

JamesOakley’s picture

@mardale

The Ubercart "payment pack" module has a couple of calls to t() that use the third argument of t(). That argument, if supplied, is an associative array. If a value is set for the key "context", then the translation action is given the context specified.

For example, this line is in uc_payment_pack.module:

'#title' => t('Check payment policy', array(), array('context' => 'cheque'))

So the returned value will be "Check payment policy" as put through the translator, but it goes through the translator with a context of "cheque".

That's all in Ubercart - I modified nothing.

If I used String Overrides to change "Check" into "Cheque", I'd change every instance of the word "Check". So I'd get things like "Please cheque the address before proceeding", which I don't want.

The answer is to pick up that t() is called with a context that tells it when we're talking about a money check, called "cheque" in UK English. So I ask String Overrides to replace "Check" with "Cheque", but only when the context is "cheque". Then only the instances that should be changed get translated into English.

RobLoach’s picture

Issue summary: View changes
Status: Needs review » Fixed

Thanks for the help, James. If there's anyone opposed to it, I'll close this. Let's open up a new story if we need to add more documentaiton.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

drummondf’s picture

Not sure if I can open this back up or not, but I have a followup question on this (which is a Dcoumentation question)

I have a situation where I am using String Overrides to change the N/A option in a Comment field to "Just a Comment" (I then use Conditional Fields to provide additional options, but don't want users to have to select "Just a Comment" from Taxonomy because it will display that on all of the comments)

What would the appropriate Context be to "point" to Comments?

Thank you!

just.andy.shilton’s picture

Geric, I'm not sure you can I'm afraid. If you aren't sure what the context is, you can often use a good IDE (I use Netbeans) to search the codebase of a module for the t() function. I just did that for the core comments module and almost none of the strings wrapped in the t() function have a context attached. I wouldn't recommend hacking core so this is a case of "it's not possible" from what i can see sadly.

Hopefully D8 will have more as I know there is a larger amount of attention been given to translations in D8.

JamesOakley’s picture

Yes, that's correct.

If you want to use the Context setting in String Overrides to change a string only in a certain context, then you are relying on the module where that string appears.

If they printed the string you want to change (in this case, "n/a") using the t() function to specify a context, then that is the "context" you must specify in your String Overrides.

If they didn't (either because they didn't use t() at all - a big no-no - or more likely because they used t() but didn't specify a context), then you have two choices. 1. Open an issue in that module's queue, requesting that t() be used with a specified context, or 2. Override the string on your site, but do so wherever it's used, without regard for context.

In this case, the Comments module is part of core, so you'd be looking at the core Comments module for the relevant use of t(), and it would be the core issue queue to request the feature if it's not been done.