Skip to content
wiki.fftac.org

Localerepository - Source Excerpt 03

Back to Localerepository

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

return array();
	}

	/**
	 * Determine display metadata for a locale tag using the shipped locale catalog.
	 *
	 * @param string $tag Locale tag.
	 * @return array
	 */
	private static function derive_catalog_metadata( $tag ) {
		$tag     = LocaleValidator::canonicalize_tag( $tag );
		$catalog = self::get_catalog();

		if ( '' === $tag || isset( $catalog[ $tag ] ) ) {
			return isset( $catalog[ $tag ] ) ? $catalog[ $tag ] : array();
		}

		$candidate = self::find_catalog_language_match( $tag, $catalog );

		if ( empty( $candidate ) ) {
			return array();
		}

		return array(
			'language'   => self::simplify_locale_label( $candidate['language'] ),
			'nativeName' => self::simplify_locale_label( $candidate['nativeName'] ),
			'script'     => ! empty( $candidate['script'] ) ? $candidate['script'] : LocaleValidator::extract_script( $candidate['tag'] ),
		);
	}

	/**
	 * Find the closest catalog record that can supply a language self-name.
	 *
	 * @param string $tag Locale tag.
	 * @param array  $catalog Locale catalog.
	 * @return array
	 */
	private static function find_catalog_language_match( $tag, array $catalog ) {
		$language = strtolower( strtok( $tag, '-' ) );
		$region   = LocaleValidator::extract_region( $tag );

		if ( isset( $catalog[ $language ] ) ) {
			return $catalog[ $language ];
		}

		if ( 'zh' === $language ) {
			$preferred = array();

			if ( in_array( $region, array( 'CN', 'SG' ), true ) ) {
				$preferred = array( 'zh-CN' );
			} elseif ( in_array( $region, array( 'HK', 'MO' ), true ) ) {
				$preferred = array( 'zh-HK', 'zh-TW' );
			} elseif ( 'TW' === $region ) {
				$preferred = array( 'zh-TW', 'zh-HK' );
			}

			foreach ( $preferred as $preferred_tag ) {
				if ( isset( $catalog[ $preferred_tag ] ) ) {
					return $catalog[ $preferred_tag ];
				}
			}
		}

		$candidates = array();

		foreach ( $catalog as $candidate ) {
			if ( $language === strtolower( strtok( $candidate['tag'], '-' ) ) ) {
				$candidates[] = $candidate;
			}
		}

		if ( empty( $candidates ) ) {
			return array();
		}

		usort(
			$candidates,
			static function ( $left, $right ) {
				$left_name  = LocaleRepository::simplify_locale_label( $left['nativeName'] );
				$right_name = LocaleRepository::simplify_locale_label( $right['nativeName'] );
				$length_cmp = strlen( $left_name ) <=> strlen( $right_name );

				if ( 0 !== $length_cmp ) {
					return $length_cmp;
				}

				$tag_cmp = strlen( $left['tag'] ) <=> strlen( $right['tag'] );

				if ( 0 !== $tag_cmp ) {
					return $tag_cmp;
				}

				return strcmp( $left['tag'], $right['tag'] );
			}
		);

		return $candidates[0];
	}

	/**
	 * Collapse catalog names like "English (UK)" down to the language self-name.
	 *
	 * @param string $label Locale label.
	 * @return string
	 */
	private static function simplify_locale_label( $label ) {
		$label = trim( (string) $label );

		if ( '' === $label ) {
			return '';
		}

		foreach ( array( ' (', '(' ) as $separator ) {
			$position = strpos( $label, $separator );

			if ( false !== $position ) {
				return trim( substr( $label, 0, $position ) );
			}
		}

		return $label;
	}

	/**
	 * Determine whether a stored language label should be replaced.
	 *
	 * @param array $record Locale record.
	 * @return bool
	 */
	private static function should_replace_language_label( array $record ) {
		$language = trim( (string) $record['language'] );

		if ( '' === $language ) {
			return true;
		}

		$tag      = $record['tag'];
		$base_tag = strtoupper( (string) strtok( $tag, '-' ) );

		return $language === $tag || $language === strtolower( $tag ) || $language === $base_tag;
	}

	/**
	 * Determine whether a stored native label should be replaced.
	 *
	 * @param array $record Locale record.
	 * @return bool
	 */
	private static function should_replace_native_name( array $record, $preferred_native_name = '' ) {
		$native_name = trim( (string) $record['nativeName'] );

		if ( '' === $native_name ) {
			return true;
		}

		if ( $native_name === $record['tag'] || $native_name === strtolower( $record['tag'] ) || $native_name === $record['urlTag'] ) {
			return true;
		}

		if ( '' !== $preferred_native_name && self::simplify_locale_label( $native_name ) === self::simplify_locale_label( $preferred_native_name ) && $native_name !== $preferred_native_name ) {
			return true;
		}

		$normalized_native   = self::normalize_label_for_comparison( $native_name );
		$normalized_native_simple = self::normalize_label_for_comparison( self::simplify_locale_label( $native_name ) );
		$normalized_language = self::normalize_label_for_comparison( $record['language'] );
		$normalized_preferred = self::normalize_label_for_comparison( $preferred_native_name );
		$normalized_preferred_simple = self::normalize_label_for_comparison( self::simplify_locale_label( $preferred_native_name ) );

		if ( '' !== $normalized_language && $normalized_native === $normalized_language ) {
			return true;
		}

		return (
			'' !== $normalized_preferred &&
			(
				$normalized_native === $normalized_preferred ||
				$normalized_native === $normalized_preferred_simple ||
				$normalized_native_simple === $normalized_preferred ||
				$normalized_native_simple === $normalized_preferred_simple
			) &&
			$native_name !== $preferred_native_name
		);
	}

	/**
	 * Normalize a display label for stale-label comparisons.
	 *
	 * @param string $label Label text.
	 * @return string
	 */
	private static function normalize_label_for_comparison( $label ) {
		$label = strtolower( trim( (string) $label ) );

		if ( '' === $label ) {
			return '';
		}

		$label = str_replace( "\xEF\xBC\x88", ' (', $label );

		if ( function_exists( 'remove_accents' ) ) {
			$label = remove_accents( $label );
		}