Skip to content

Instantly share code, notes, and snippets.

@grappler
Last active December 1, 2015 09:37
Show Gist options
  • Save grappler/d3c82d2aab49cb25b4cf to your computer and use it in GitHub Desktop.
Save grappler/d3c82d2aab49cb25b4cf to your computer and use it in GitHub Desktop.

Adding context to strings

A word or phrase may be ambiguous to a translator who will likely only see the string in the interface of their translation tool and not necessarily on the front-end of your theme. Ambiguity can arise when a term is used in more than one context.

The functions _x() or _ex() are used to add the context to a string. They are similar to __() and _e(), but have a comment as an additional argument.

The function can be used in differnt ways

  • Explain the use for words with double meaings
  • Give extra information about the string

examples from WordPress core

_x( 'Format', 'post format' )

This strings is used in the title of the post format metabox. If there was no context given then it would be unclear if it meant for formating a text or as a title to the post format metbox.

image

_x( 'Comment', 'column name' )

In this example the string is the comment coloumn title on the comments page and not the verb to comment to a post.

image

_x( 'Mark as Spam', 'site' )

This string is to mark a site as spam in the multisite interface. The context allows it to differenciate to marking a comment as spam.

_x( ',', 'tag delimiter' )

In this case a symbol is internationlized. The context here explains that the comma is being used as a seperator between tags.

_x( 'j M @ H:i', 'revision date short format' )

Languages have differnt date and time formats. So these can be translated too. The context is not only given to help the translator to decifer the letters but to also differentiate between the standard date format and the short date format.

Reasoning

You may wonder if it would not be easier in some cases to make the string clearer by including more information. For exmaple instead of "Format" use "Post Format". In some places of the UI there may be space constraint which does not allow the string to be longer. Other times it may not make sense to repeat the same word multiple time. For example the comment management actions. It would look strange if coment was mentioned in each action e.g. "Approve Comment", "Reply to comment, "Edit Comment"

image

Translation software

By defining the context the same word can be translated differently.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment