<?php
/**
 * Plugin locale for Core Version Switcher by Volade.
 *
 * @package CVS
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class CVS_I18n {

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

	public function load_textdomain() {
		load_plugin_textdomain(
			'core-version-switcher-by-volade',
			false,
			dirname( plugin_basename( CVS_FILE ) ) . '/languages'
		);
	}

	/**
	 * @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 ( 'core-version-switcher-by-volade' !== $domain || ! is_admin() || ! get_current_user_id() ) {
			return $locale;
		}
		return self::get_user_locale();
	}
}
