Skip to content

Instantly share code, notes, and snippets.

@ivankravchenko
Created July 21, 2014 09:31
Show Gist options
  • Save ivankravchenko/43ef686145aeed9c39b5 to your computer and use it in GitHub Desktop.
Save ivankravchenko/43ef686145aeed9c39b5 to your computer and use it in GitHub Desktop.
wordpress translate helper
<?php
/*
* tr – translation helper
* examples:
* tr("Hello") -> "Привіт" // simply translates given string
* tr("%s products", $category) -> "Продукти категорії " // translates format and uses sprintf for substitutions
* tr(array("%d comment", "%d comments"), $commentCount) -> "1 comment" // pluralized format version
*/
function tr() {
$context = 'theme';
$args = func_get_args();
if (count($args) == 0) throw new ErrorException("Incorrect tr() usage.");
$phrase = array_shift($args);
if (is_string($phrase)) {
$phrase = __($phrase, $context);
} elseif (is_array($phrase) && count($phrase) == 2 && count($argv) > 0) {
$phrase = _n($phrase, $context, $args[0]);
}
if (count($args) > 0) {
return call_user_func_array('sprintf', array_merge(array($phrase), $args));
} else {
return $phrase;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment