| 1 |
<?php |
| 2 |
|
| 3 |
add_filter( 'bloginfo', 'bogo_bloginfo_filter', 10, 2 ); |
| 4 |
|
| 5 |
function bogo_bloginfo_filter( $output, $show ) { |
| 6 |
if ( ! Bogo_POMO::is_ready() ) { |
| 7 |
return $output; |
| 8 |
} |
| 9 |
|
| 10 |
if ( 'name' === $show ) { |
| 11 |
$output = bogo_translate( 'blogname', 'blogname', $output ); |
| 12 |
} elseif ( 'description' === $show ) { |
| 13 |
$output = bogo_translate( 'blogdescription', 'blogdescription', $output ); |
| 14 |
} |
| 15 |
|
| 16 |
return $output; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
add_filter( 'get_term', 'bogo_get_term_filter', 10, 2 ); |
| 21 |
|
| 22 |
function bogo_get_term_filter( $term, $taxonomy ) { |
| 23 |
if ( ! Bogo_POMO::is_ready() ) { |
| 24 |
return $term; |
| 25 |
} |
| 26 |
|
| 27 |
if ( $term instanceof WP_Term ) { |
| 28 |
$term = bogo_translate_term( $term ); |
| 29 |
} |
| 30 |
|
| 31 |
return $term; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
add_action( 'load-edit-tags.php', 'bogo_remove_get_term_filter', 10, 0 ); |
| 36 |
|
| 37 |
function bogo_remove_get_term_filter() { |
| 38 |
remove_filter( 'get_term', 'bogo_get_term_filter' ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
function bogo_translate_term( WP_Term $term ) { |
| 43 |
$term->name = bogo_translate( |
| 44 |
sprintf( '%s:%d', $term->taxonomy, $term->term_id ), |
| 45 |
$term->taxonomy, |
| 46 |
$term->name |
| 47 |
); |
| 48 |
|
| 49 |
return $term; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
function bogo_translate( $singular, $context = '', $default = '' ) { |
| 54 |
return Bogo_POMO::translate( $singular, $context, $default ); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
class Bogo_POMO { |
| 59 |
|
| 60 |
private static $mo; |
| 61 |
|
| 62 |
public static function translate( $singular, $context = '', $default = '' ) { |
| 63 |
if ( ! self::$mo ) { |
| 64 |
return '' !== $default ? $default : $singular; |
| 65 |
} |
| 66 |
|
| 67 |
$translated = self::$mo->translate( $singular, $context ); |
| 68 |
|
| 69 |
if ( $translated === $singular and '' !== $default ) { |
| 70 |
return $default; |
| 71 |
} else { |
| 72 |
return $translated; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public static function export( $locale, $entries = array() ) { |
| 77 |
if ( ! bogo_is_available_locale( $locale ) ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
$dir = self::dir(); |
| 82 |
|
| 83 |
$revision_date = new DateTimeImmutable(); |
| 84 |
|
| 85 |
$headers = array( |
| 86 |
'PO-Revision-Date' => $revision_date->format( 'Y-m-d H:i:s' ) . '+0000', |
| 87 |
'MIME-Version' => '1.0', |
| 88 |
'Content-Type' => 'text/plain; charset=UTF-8', |
| 89 |
'Content-Transfer-Encoding' => '8bit', |
| 90 |
'X-Generator' => sprintf( 'Bogo %s', BOGO_VERSION ), |
| 91 |
'Language' => $locale, |
| 92 |
'Project-Id-Version' => |
| 93 |
sprintf( 'WordPress %s', get_bloginfo( 'version' ) ), |
| 94 |
); |
| 95 |
|
| 96 |
require_once ABSPATH . WPINC . '/pomo/po.php'; |
| 97 |
$po = new PO(); |
| 98 |
$po->set_headers( $headers ); |
| 99 |
|
| 100 |
foreach ( (array) $entries as $entry ) { |
| 101 |
$entry = new Translation_Entry( $entry ); |
| 102 |
$po->add_entry( $entry ); |
| 103 |
} |
| 104 |
|
| 105 |
$po_file = is_multisite() |
| 106 |
? sprintf( '%d-%s.po', get_current_blog_id(), $locale ) |
| 107 |
: sprintf( '%s.po', $locale ); |
| 108 |
$po_file = path_join( $dir, $po_file ); |
| 109 |
$po->export_to_file( $po_file ); |
| 110 |
|
| 111 |
$mo = new MO(); |
| 112 |
$mo->set_headers( $headers ); |
| 113 |
|
| 114 |
foreach ( (array) $entries as $entry ) { |
| 115 |
$entry = new Translation_Entry( $entry ); |
| 116 |
$mo->add_entry( $entry ); |
| 117 |
} |
| 118 |
|
| 119 |
$mo_file = is_multisite() |
| 120 |
? sprintf( '%d-%s.mo', get_current_blog_id(), $locale ) |
| 121 |
: sprintf( '%s.mo', $locale ); |
| 122 |
$mo_file = path_join( $dir, $mo_file ); |
| 123 |
return $mo->export_to_file( $mo_file ); |
| 124 |
} |
| 125 |
|
| 126 |
public static function import( $locale ) { |
| 127 |
if ( ! bogo_is_available_locale( $locale ) ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
$dir = self::dir(); |
| 132 |
|
| 133 |
$mo_file = is_multisite() |
| 134 |
? sprintf( '%d-%s.mo', get_current_blog_id(), $locale ) |
| 135 |
: sprintf( '%s.mo', $locale ); |
| 136 |
$mo_file = path_join( $dir, $mo_file ); |
| 137 |
|
| 138 |
if ( ! is_readable( $mo_file ) ) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
$mo = new MO(); |
| 143 |
|
| 144 |
if ( ! $mo->import_from_file( $mo_file ) ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
self::$mo = $mo; |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
public static function reset() { |
| 153 |
self::$mo = null; |
| 154 |
} |
| 155 |
|
| 156 |
public static function is_ready() { |
| 157 |
return (bool) self::$mo; |
| 158 |
} |
| 159 |
|
| 160 |
private static function dir() { |
| 161 |
$dir = path_join( WP_LANG_DIR, 'bogo' ); |
| 162 |
$dir = apply_filters( 'bogo_pomo_dir', $dir ); |
| 163 |
wp_mkdir_p( $dir ); |
| 164 |
return $dir; |
| 165 |
} |
| 166 |
} |
| 167 |
|