| 466 | /** |
| 467 | * Register a translation textdomain. |
| 468 | * |
| 469 | * @since 5.0.0 |
| 470 | * |
| 471 | * @param string $handle Name of the script to register a translation domain to. |
| 472 | * @param string $domain The textdomain. |
| 473 | * @param string $path Optional. The full file path to the directory containing translation files. |
| 474 | * |
| 475 | * @return bool True if the textdomain was registered, false if not. |
| 476 | */ |
| 477 | public function set_translations( $handle, $domain, $path = null ) { |
| 478 | if ( ! isset( $this->registered[ $handle ] ) ) { |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | /** @var \_WP_Dependency $obj */ |
| 483 | $obj = $this->registered[ $handle ]; |
| 484 | |
| 485 | /** This filter is documented in wp-includes/class.wp-scripts.php */ |
| 486 | $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) ); |
| 487 | |
| 488 | $relative = false; |
| 489 | $languages_path = WP_LANG_DIR; |
| 490 | $json_translations = false; |
| 491 | |
| 492 | // First see if the script is from the content directory. |
| 493 | $content_url = content_url(); |
| 494 | if ( strpos( $content_url, $src ) === 0 ) { |
| 495 | // Make the src relative the specific plugin or theme. |
| 496 | $relative = trim( substr( $src, strlen( $content_url ) ), '/' ); |
| 497 | $relative = explode( '/', $relative ); |
| 498 | |
| 499 | $languages_path = WP_LANG_DIR . '/' . $relative[0]; |
| 500 | |
| 501 | $relative = array_slice( $relative, 2 ); |
| 502 | $relative = implode( '/', $relative ); |
| 503 | } else { |
| 504 | // Otherwise check if it's from another WP directory. |
| 505 | $site_url = site_url(); |
| 506 | if ( strpos( $site_url, $src ) === 0 ) { |
| 507 | // Make the src relative to the WP root. |
| 508 | $relative = substr( $src, strlen( $site_url ) ); |
| 509 | $relative = trim( $relative, '/' ); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // If the source is not from WP. |
| 514 | if ( false === $relative ) { |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | $locale = is_admin() ? get_locale() : get_user_locale(); |
| 519 | |
| 520 | $filename = $domain . '-' . $locale . '-' . md5( $relative ) . '.json'; |
| 521 | |
| 522 | // If a registered path was given check if a translation exists. |
| 523 | if ( $path && file_exists( $path . '/' . $filename ) ) { |
| 524 | $json_translations = file_get_contents( $path . '/' . $filename ); |
| 525 | } |
| 526 | |
| 527 | // Otherwise check if the translation exists in the languages path. |
| 528 | if ( ! $json_translations && file_exists( $languages_path . '/' . $filename ) ) { |
| 529 | $json_translations = file_get_contents( $languages_path . '/' . $filename ); |
| 530 | } |
| 531 | |
| 532 | if ( ! $json_translations ) { |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | $obj->deps[] = 'wp-i18n'; |
| 537 | |
| 538 | return $this->add_inline_script( $handle, 'wp.i18n.setLocaleData( ' . $json_translations . ', ' . $domain . ' );', 'before' ); |
| 539 | } |
| 540 | |