Localerepository - Source Excerpt 02
Summary
This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/uaix-locale-router/src/LocaleRepository.php so readers can inspect the evidence without opening the full source file.
**Source path:** Antichrist.net/wp-content/plugins/uaix-locale-router/src/LocaleRepository.php
/**
* Convert a URL tag to a WordPress locale.
*
* @param string $url_tag URL tag.
* @return string
*/
public static function url_tag_to_wp_locale( $url_tag ) {
$record = self::get_enabled_locale_by_url_tag( $url_tag );
if ( empty( $record['tag'] ) ) {
return '';
}
return str_replace( '-', '_', $record['tag'] );
}
/**
* Convert a WordPress locale to a canonical locale tag.
*
* @param string $wp_locale WordPress locale.
* @return string
*/
public static function wp_locale_to_tag( $wp_locale ) {
return LocaleValidator::canonicalize_tag( str_replace( '_', '-', (string) $wp_locale ) );
}
/**
* Determine whether a URL tag is enabled.
*
* @param string $url_tag URL tag.
* @return bool
*/
public static function is_enabled_url_tag( $url_tag ) {
return ! empty( self::get_enabled_locale_by_url_tag( $url_tag ) );
}
/**
* Return an enabled locale record by URL tag.
*
* @param string $url_tag URL tag.
* @return array
*/
public static function get_enabled_locale_by_url_tag( $url_tag ) {
$normalized = self::normalize_url_tag( $url_tag );
foreach ( self::get_supported_locales() as $record ) {
if ( ! empty( $record['enabled'] ) && self::locale_to_url_tag( $record['tag'] ) === $normalized ) {
return $record;
}
}
return array();
}
/**
* Return a supported locale record by tag.
*
* @param string $tag Locale tag.
* @return array
*/
public static function get_supported_locale( $tag ) {
$tag = LocaleValidator::canonicalize_tag( $tag );
foreach ( self::get_supported_locales() as $record ) {
if ( $record['tag'] === $tag ) {
return $record;
}
}
return array();
}
/**
* Return the standards-backed locale catalog.
*
* @return array
*/
public static function get_catalog() {
if ( null !== self::$catalog ) {
return self::$catalog;
}
$data = Json::decode_file( Plugin::locale_catalog_path(), array() );
$entries = isset( $data['translations'] ) && is_array( $data['translations'] ) ? $data['translations'] : $data;
$catalog = array();
foreach ( $entries as $entry ) {
if ( ! is_array( $entry ) || empty( $entry['language'] ) ) {
continue;
}
$tag = LocaleValidator::canonicalize_tag( str_replace( '_', '-', $entry['language'] ) );
if ( '' === $tag ) {
continue;
}
$catalog[ $tag ] = LocaleValidator::normalize_supported_record(
array(
'tag' => $tag,
'language' => isset( $entry['english_name'] ) ? $entry['english_name'] : $tag,
'nativeName' => isset( $entry['native_name'] ) ? $entry['native_name'] : $tag,
'region' => LocaleValidator::extract_region( $tag ),
'script' => LocaleValidator::extract_script( $tag ),
'isRtl' => LocaleValidator::is_rtl_locale( $tag ),
'enabled' => false,
'isDefault' => false,
'fallback' => $tag,
'catalogHit' => true,
)
);
}
self::$catalog = $catalog;
return self::$catalog;
}
/**
* Search the locale catalog.
*
* @param string $search Search term.
* @param int $limit Result limit.
* @return array
*/
public static function search_catalog( $search = '', $limit = 50 ) {
$search = strtolower( trim( (string) $search ) );
$results = array();
foreach ( self::get_catalog() as $record ) {
if ( '' !== $search ) {
$haystack = strtolower(
implode(
' ',
array(
$record['tag'],
$record['urlTag'],
$record['language'],
$record['nativeName'],
)
)
);
if ( false === strpos( $haystack, $search ) ) {
continue;
}
}
$results[] = $record;
if ( count( $results ) >= $limit ) {
break;
}
}
return $results;
}
/**
* Build a locale record from catalog data or a valid custom tag.
*
* @param string $tag Locale tag.
* @param array $overrides Overrides.
* @return array
*/
public static function build_record_for_tag( $tag, array $overrides = array() ) {
$tag = LocaleValidator::canonicalize_tag( $tag );
if ( '' === $tag ) {
return array();
}
$catalog = self::get_catalog();
if ( isset( $catalog[ $tag ] ) ) {
return LocaleValidator::normalize_supported_record( array_merge( $catalog[ $tag ], $overrides ) );
}
$language = strtok( $tag, '-' );
$derived = self::derive_catalog_metadata( $tag );
return LocaleValidator::normalize_supported_record(
array_merge(
array(
'tag' => $tag,
'language' => ! empty( $derived['language'] ) ? $derived['language'] : strtoupper( (string) $language ),
'nativeName' => ! empty( $derived['nativeName'] ) ? $derived['nativeName'] : $tag,
'region' => LocaleValidator::extract_region( $tag ),
'script' => ! empty( $derived['script'] ) ? $derived['script'] : LocaleValidator::extract_script( $tag ),
'isRtl' => LocaleValidator::is_rtl_locale( $tag ),
'enabled' => false,
'isDefault' => false,
'fallback' => self::get_default_locale(),
'catalogHit' => false,
),
$overrides
)
);
}
/**
* Fill in human-friendly locale names when saved records only contain tags.
*
* @param array $record Locale record.
* @return array
*/
private static function hydrate_display_metadata( array $record ) {
$default = self::default_display_metadata( $record['tag'] );
$derived = array_merge( self::derive_catalog_metadata( $record['tag'] ), $default );
if ( self::should_replace_language_label( $record ) && ! empty( $derived['language'] ) ) {
$record['language'] = $derived['language'];
}
if ( self::should_replace_native_name( $record, ! empty( $derived['nativeName'] ) ? $derived['nativeName'] : '' ) && ! empty( $derived['nativeName'] ) ) {
$record['nativeName'] = $derived['nativeName'];
}
if ( '' === $record['script'] && ! empty( $derived['script'] ) ) {
$record['script'] = $derived['script'];
}
return LocaleValidator::normalize_supported_record( $record );
}
/**
* Return display metadata from the bundled default locale file.
*
* @param string $tag Locale tag.
* @return array
*/
private static function default_display_metadata( $tag ) {
$tag = LocaleValidator::canonicalize_tag( $tag );
$defaults = Json::decode_file( Plugin::default_supported_locales_path(), array() );
if ( '' === $tag || ! is_array( $defaults ) ) {
return array();
}
foreach ( $defaults as $record ) {
if ( ! is_array( $record ) || empty( $record['tag'] ) || LocaleValidator::canonicalize_tag( $record['tag'] ) !== $tag ) {
continue;
}
$metadata = array();
foreach ( array( 'language', 'nativeName', 'region', 'script' ) as $field ) {
if ( isset( $record[ $field ] ) && '' !== trim( (string) $record[ $field ] ) ) {
$metadata[ $field ] = (string) $record[ $field ];
}
}
if ( isset( $record['isRtl'] ) ) {
$metadata['isRtl'] = (bool) $record['isRtl'];
}
return $metadata;
}