| 327 | | $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}"; |
| | 327 | $translations = ''; |
| | 328 | if ( $obj->text_domain ) { |
| | 329 | $translation_script = $this->print_inline_translations( $src, $obj->text_domain, get_locale(), $obj->translations_path, false ); |
| | 330 | |
| | 331 | if ( $translation_script ) { |
| | 332 | $translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translation_script ); |
| | 333 | } |
| | 334 | } |
| | 335 | |
| | 336 | $tag = "{$cond_before}{$translations}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}"; |
| | 412 | /** |
| | 413 | * Prints inline translations/ |
| | 414 | * |
| | 415 | * @param string $src The script source. |
| | 416 | * @param string $domain The translation domain. |
| | 417 | * @param string $locale The locale. |
| | 418 | * @param string $registered_path Optional. The registered path for the translations, if any. |
| | 419 | * @param bool $echo Optional. Whether to echo the script instead of just returning it. |
| | 420 | * Default true. |
| | 421 | * |
| | 422 | * @return false|string False if no translations could be found. The script otherwise. |
| | 423 | */ |
| | 424 | public function print_inline_translations( $src, $domain, $locale, $registered_path = null, $echo = true ) { |
| | 425 | $relative = false; |
| | 426 | $languages_path = WP_LANG_DIR; |
| | 427 | $json_translations = false; |
| | 428 | |
| | 429 | // First see if the script is from the content directory. |
| | 430 | $content_url = content_url(); |
| | 431 | if ( strpos( $content_url, $src ) === 0 ) { |
| | 432 | // Make the src relative the specific plugin or theme. |
| | 433 | $relative = trim( substr( $src, strlen( $content_url ) ), '/' ); |
| | 434 | $relative = explode( '/', $relative ); |
| | 435 | |
| | 436 | $languages_path = WP_LANG_DIR . '/' . $relative[0]; |
| | 437 | |
| | 438 | $relative = array_slice( $relative, 2 ); |
| | 439 | $relative = implode( '/', $relative ); |
| | 440 | } else { |
| | 441 | // Otherwise check if it's from another WP directory. |
| | 442 | $site_url = site_url(); |
| | 443 | if ( strpos( $site_url, $src ) === 0 ) { |
| | 444 | // Make the src relative to the WP root. |
| | 445 | $relative = substr( $src, strlen( $site_url ) ); |
| | 446 | $relative = trim( $relative, '/' ); |
| | 447 | } |
| | 448 | } |
| | 449 | |
| | 450 | // If the source is not from WP. |
| | 451 | if ( $relative === false ) { |
| | 452 | return false; |
| | 453 | } |
| | 454 | |
| | 455 | $filename = $domain . '-' . $locale . '-' . md5( $relative ) . '.json'; |
| | 456 | |
| | 457 | // If a registered path was given check if a translation exists. |
| | 458 | if ( $registered_path && file_exists( $registered_path . '/' . $filename ) ) { |
| | 459 | $json_translations = file_get_contents( $registered_path . '/' . $filename ); |
| | 460 | } |
| | 461 | |
| | 462 | // Otherwise check if the translation exists in the languages path. |
| | 463 | if ( ! $json_translations && file_exists( $languages_path . '/' . $filename ) ) { |
| | 464 | $json_translations = file_get_contents( $languages_path . '/' . $filename ); |
| | 465 | } |
| | 466 | |
| | 467 | if ( ! $json_translations ) { |
| | 468 | return false; |
| | 469 | } |
| | 470 | |
| | 471 | $output = 'wp.i18n.setLocaleData( ' . $json_translations . ', ' . $domain . ' );'; |
| | 472 | |
| | 473 | if ( $echo ) { |
| | 474 | printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); |
| | 475 | } |
| | 476 | |
| | 477 | return $output; |
| | 478 | } |
| | 479 | |
| | 543 | /** |
| | 544 | * Register a translation textdomain. |
| | 545 | * |
| | 546 | * @since 5.0.0 |
| | 547 | * |
| | 548 | * @param string $handle Name of the script to register a translation domain to. |
| | 549 | * @param string $domain The textdomain. |
| | 550 | * @param string $path Optional. The full file path to the directory containing translation files. |
| | 551 | * |
| | 552 | * @return bool True if the textdomain was registered, false if not. |
| | 553 | */ |
| | 554 | public function set_translations( $handle, $domain, $path = null ) { |
| | 555 | if ( ! isset( $this->registered[ $handle ] ) ) { |
| | 556 | return false; |
| | 557 | } |
| | 558 | |
| | 559 | return $this->registered[ $handle ]->set_translations( $domain, $path ); |
| | 560 | } |
| | 561 | |