-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: Add new page for i18n filters #28553
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
docs/designers-developers/developers/filters/i18n-filters.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# i18n Filters | ||
|
||
The i18n functions (`__()`, `_x()`, `_n()` and `_nx()`) provide translations of strings for use in your code. The values returned by these functions are filterable if you need to override them, using the following filters: | ||
|
||
- `i18n.gettext` | ||
- `i18n.gettext_with_context` | ||
- `i18n.ngettext` | ||
- `i18n.ngettext_with_context` | ||
|
||
## Filter Arguments | ||
|
||
The filters are passed the following arguments, in line with their PHP equivalents. | ||
|
||
### i18n.gettext | ||
|
||
```jsx | ||
function i18nGettextCallback( translation, text, domain ) { | ||
return translation; | ||
} | ||
``` | ||
|
||
### i18n.gettext_with_context | ||
|
||
```jsx | ||
function i18nGettextWithContextCallback( translation, text, context, domain ) { | ||
return translation; | ||
} | ||
``` | ||
|
||
### i18n.ngettext | ||
|
||
```jsx | ||
function i18nNgettextCallback( translation, single, plural, number, domain ) { | ||
return translation; | ||
} | ||
```` | ||
|
||
### i18n.ngettext_with_context | ||
|
||
```jsx | ||
function i18nNgettextWithContextCallback( | ||
translation, | ||
single, | ||
plural, | ||
number, | ||
context, | ||
domain | ||
) { | ||
return translation; | ||
} | ||
```` | ||
|
||
## Basic Example | ||
|
||
Here is a simple example, using the `i18n.gettext` filter to override a specific translation. | ||
|
||
```jsx | ||
// Define our filter callback. | ||
function myPluginGettextFilter( translation, text, domain ) { | ||
if ( text === 'Add to Reusable blocks' ) { | ||
return 'Save to MyOrg block library'; | ||
} | ||
return translation; | ||
} | ||
|
||
// Adding the filter | ||
wp.hooks.addFilter( | ||
'i18n.gettext', | ||
'my-plugin', | ||
leewillis77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
myPluginGettextFilter | ||
); | ||
``` | ||
|
||
## Using 'text domain'-specific filters | ||
|
||
Filters that are specific to the text domain you're operating on are generally preferred for performance reasons (since your callback will only be run for strings in the relevant text domain). | ||
|
||
To attach to a text domain-specific filter append an underscore and the text-domain to the standard filter name. For example, if filtering a string where the text domain is "woocommerce", you would use one of the following filters: | ||
|
||
- `i18n.gettext_woocommerce` | ||
- `i18n.gettext_with_context_woocommerce` | ||
- `i18n.ngettext_woocommerce` | ||
- `i18n.ngettext_with_context_woocommerce` | ||
|
||
For example: | ||
|
||
```jsx | ||
// Define our filter callback. | ||
function myPluginGettextFilter( translation, text, domain ) { | ||
if ( text === "You’ve fulfilled all your orders" ) { | ||
return 'All packed up and ready to go. Good job!'; | ||
} | ||
return translation; | ||
} | ||
|
||
// Adding the filter | ||
wp.hooks.addFilter( | ||
'i18n.gettext_woocommerce', | ||
'my-plugin', | ||
leewillis77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
myPluginGettextFilter | ||
); | ||
``` | ||
|
||
*Note*: To apply a filter where the text-domain is `undefined` (for example WordPress core strings), then use the name "default" to construct the filter name. | ||
|
||
- `i18n.gettext_default` | ||
- `i18n.gettext_with_context_default` | ||
- `i18n.ngettext_default` | ||
- `i18n.ngettext_with_context_default` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to link with the corresponding PHP documentation page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not aware of a single place we could link to that covers all of the relevant PHP filters (they each have their own page), so decided not to add links.