<?php
/**
 * Plugin locale for PHP Compatibility Checker by Volade admin strings.
 *
 * @package PCC
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class PCC_I18n {

	public function __construct() {
		add_filter( 'plugin_locale', array( $this, 'filter_plugin_locale' ), 10, 2 );
	}

	/**
	 * @return string[]
	 */
	public static function available_locales() {
		return array( 'en_US', 'fr_FR', 'es_ES', 'de_DE', 'pt_BR', 'it_IT' );
	}

	public static function is_valid_locale( $locale ) {
		return in_array( $locale, self::available_locales(), true );
	}

	public static function get_user_locale() {
		$site = determine_locale();
		if ( self::is_valid_locale( $site ) ) {
			return $site;
		}
		return 'en_US';
	}

	public function filter_plugin_locale( $locale, $domain ) {
		if ( 'php-compatibility-checker-by-volade' !== $domain || ! is_admin() || ! get_current_user_id() ) {
			return $locale;
		}
		return self::get_user_locale();
	}
}
