<commit_before><?php
/**
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         1.2.0
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\Test\TestCase\I18n;

use Aura\Intl\Package;
use Cake\Cache\Cache;
use Cake\Core\Plugin;
use Cake\I18n\I18n;
use Cake\TestSuite\TestCase;
use Locale;

/**
 * I18nTest class
 */
class I18nTest extends TestCase
{

    /**
     * Used to restore the internal locale after tests
     *
     * @var string
     */
    public $locale;

    /**
     * Set Up
     *
     * @return void
     */
    public function setUp()
    {
        parent::setUp();
        $this->locale = Locale::getDefault() ?: I18n::DEFAULT_LOCALE;
    }

    /**
     * Tear down method
     *
     * @return void
     */
    public function tearDown()
    {
        parent::tearDown();
        I18n::clear();
        I18n::setDefaultFormatter('default');
        I18n::setLocale($this->locale);
        Plugin::unload();
        Cache::clear(false, '_cake_core_');
    }

    /**
     * Tests that the default locale is set correctly
     *
     * @group deprecated
     * @return void
     */
    public function testDefaultLocale()
    {
        $this->deprecated(function () {
            $newLocale = 'de_DE';
            I18n::setLocale($newLocale);
            $this->assertEquals($newLocale, I18n::getLocale());
            $this->assertEquals($this->locale, I18n::getDefaultLocale());
        });
    }

    /**
     * Tests that a default translator is created and messages are parsed
     * correctly
     *
     * @return void
     */
    public function testGetDefaultTranslator()
    {
        $translator = I18n::getTranslator();
        $this->assertInstanceOf('Aura\Intl\TranslatorInterface', $translator);
        $this->assertEquals('%d is 1 (po translated)', $translator->translate('%d = 1'));
        $this->assertSame($translator, I18n::getTranslator(), 'backwards compat works');
    }

    /**
     * Tests that the translator can automatically load messages from a .mo file
     *
     * @return void
     */
    public function testGetTranslatorLoadMoFile()
    {
        $translator = I18n::getTranslator('default', 'es_ES');
        $this->assertEquals('Plural Rule 6 (translated)', $translator->translate('Plural Rule 1'));
    }

    /**
     * Tests that plural rules are correctly used for the English language
     * using the sprintf formatter
     *
     * @return void
     */
    public function testPluralSelection()
    {
        I18n::setDefaultFormatter('sprintf');
        $translator = I18n::getTranslator(); // en_US
        $result = $translator->translate('%d = 0 or > 1', ['_count' => 1]);
        $this->assertEquals('1 is 1 (po translated)', $result);

        $result = $translator->translate('%d = 0 or > 1', ['_count' => 2]);
        $this->assertEquals('2 is 2-4 (po translated)', $result);
    }

    /**
     * Tests that plural rules are correctly used for the English language
     * using the basic formatter
     *
     * @return void
     */
    public function testPluralSelectionBasicFormatter()
    {
        $translator = I18n::getTranslator('special');
        $result = $translator->translate('There are {0} things', ['_count' => 2, 'plenty']);
        $this->assertEquals('There are plenty things', $result);

        $result = $translator->translate('There are {0} things', ['_count' => 1]);
        $this->assertEquals('There is only one', $result);
    }

    /**
     * Test plural rules are used for non-english languages
     *
     * @return void
     */
    public function testPluralSelectionRussian()
    {
        $translator = I18n::getTranslator('default', 'ru');
        $result = $translator->translate('{0} months', ['_count' => 1, 1]);
        $this->assertEquals('1 months ends in 1, not 11', $result);

        $result = $translator->translate('{0} months', ['_count' => 2, 2]);
        $this->assertEquals('2 months ends in 2-4, not 12-14', $result);

        $result = $translator->translate('{0} months', ['_count' => 7, 7]);
        $this->assertEquals('7 months everything else', $result);
    }

    /**
     * Tests that custom translation packages can be created on the fly and used later on
     *
     * @return void
     */
    public function testCreateCustomTranslationPackage()
    {
        I18n::setTranslator('custom', function () {
            $package = new Package('default');
            $package->setMessages([
                'Cow' => 'Le moo'
            ]);

            return $package;
        }, 'fr_FR');

        $translator = I18n::getTranslator('custom', 'fr_FR');
        $this->assertEquals('Le moo', $translator->translate('Cow'));
    }

    /**
     * Tests that messages can also be loaded from plugins by using the
     * domain = plugin_name convention
     *
     * @return void
     */
    public function testPluginMesagesLoad()
    {
        Plugin::load([
            'TestPlugin',
            'Company/TestPluginThree'
        ]);

        $translator = I18n::getTranslator('test_plugin');
        $this->assertEquals(
            'Plural Rule 1 (from plugin)',
            $translator->translate('Plural Rule 1')
        );

        $translator = I18n::getTranslator('company/test_plugin_three');
        $this->assertEquals(
            'String 1 (from plugin three)',
            $translator->translate('String 1')
        );
    }

    /**
     * Tests that messages messages from a plugin can be automatically
     * overridden by messages in app
     *
     * @return void
     */
    public function testPluginOverride()
    {
        Plugin::load('TestTheme');
        $translator = I18n::getTranslator('test_theme');
        $this->assertEquals(
            'translated',
            $translator->translate('A Message')
        );
    }

    /**
     * Tests the locale method
     *
     * @return void
     */
    public function testGetDefaultLocale()
    {
        $this->assertEquals('en_US', I18n::getLocale());
        $this->assertEquals('en_US', ini_get('intl.default_locale'));
        I18n::setLocale('fr_FR');
        $this->assertEquals('fr_FR', I18n::getLocale());
        $this->assertEquals('fr_FR', ini_get('intl.default_locale'));
    }

    /**
     * Tests that changing the default locale also changes the way translators
     * are fetched
     *
     * @return void
     */
    public function testGetTranslatorByDefaultLocale()
    {
        I18n::setTranslator('custom', function () {
            $package = new Package('default');
            $package->setMessages([
                'Cow' => 'Le moo'
            ]);

            return $package;
        }, 'fr_FR');

        I18n::setLocale('fr_FR');
        $translator = I18n::getTranslator('custom');
        $this->assertEquals('Le moo', $translator->translate('Cow'));
    }

    /**
     * Tests the __() function
     *
     * @return void
     */
    public function testBasicTranslateFunction()
    {
        I18n::setDefaultFormatter('sprintf');
        $this->assertEquals('%d is 1 (po translated)', __('%d = 1'));
        $this->assertEquals('1 is 1 (po translated)', __('%d = 1', 1));
        $this->assertEquals('1 is 1 (po translated)', __('%d = 1', [1]));
        $this->assertEquals('The red dog, and blue cat', __('The %s dog, and %s cat', ['red', 'blue']));
        $this->assertEquals('The red dog, and blue cat', __('The %s dog, and %s cat', 'red', 'blue'));
    }

    /**
     * Tests the __() functions with explict null params
     *
     * @return void
     */
    public function testBasicTranslateFunctionsWithNullParam()
    {
        $this->assertEquals('text {0}', __('text {0}'));
        $this->assertEquals('text ', __('text {0}', null));

        $this->assertEquals('text {0}', __n('text {0}', 'texts {0}', 1));<commit_msg>Fixed "explict" is a misspelling of "explicit"<commit_after><?php
/**
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         1.2.0
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\Test\TestCase\I18n;

use Aura\Intl\Package;
use Cake\Cache\Cache;
use Cake\Core\Plugin;
use Cake\I18n\I18n;
use Cake\TestSuite\TestCase;
use Locale;

/**
 * I18nTest class
 */
class I18nTest extends TestCase
{

    /**
     * Used to restore the internal locale after tests
     *
     * @var string
     */
    public $locale;

    /**
     * Set Up
     *
     * @return void
     */
    public function setUp()
    {
        parent::setUp();
        $this->locale = Locale::getDefault() ?: I18n::DEFAULT_LOCALE;
    }

    /**
     * Tear down method
     *
     * @return void
     */
    public function tearDown()
    {
        parent::tearDown();
        I18n::clear();
        I18n::setDefaultFormatter('default');
        I18n::setLocale($this->locale);
        Plugin::unload();
        Cache::clear(false, '_cake_core_');
    }

    /**
     * Tests that the default locale is set correctly
     *
     * @group deprecated
     * @return void
     */
    public function testDefaultLocale()
    {
        $this->deprecated(function () {
            $newLocale = 'de_DE';
            I18n::setLocale($newLocale);
            $this->assertEquals($newLocale, I18n::getLocale());
            $this->assertEquals($this->locale, I18n::getDefaultLocale());
        });
    }

    /**
     * Tests that a default translator is created and messages are parsed
     * correctly
     *
     * @return void
     */
    public function testGetDefaultTranslator()
    {
        $translator = I18n::getTranslator();
        $this->assertInstanceOf('Aura\Intl\TranslatorInterface', $translator);
        $this->assertEquals('%d is 1 (po translated)', $translator->translate('%d = 1'));
        $this->assertSame($translator, I18n::getTranslator(), 'backwards compat works');
    }

    /**
     * Tests that the translator can automatically load messages from a .mo file
     *
     * @return void
     */
    public function testGetTranslatorLoadMoFile()
    {
        $translator = I18n::getTranslator('default', 'es_ES');
        $this->assertEquals('Plural Rule 6 (translated)', $translator->translate('Plural Rule 1'));
    }

    /**
     * Tests that plural rules are correctly used for the English language
     * using the sprintf formatter
     *
     * @return void
     */
    public function testPluralSelection()
    {
        I18n::setDefaultFormatter('sprintf');
        $translator = I18n::getTranslator(); // en_US
        $result = $translator->translate('%d = 0 or > 1', ['_count' => 1]);
        $this->assertEquals('1 is 1 (po translated)', $result);

        $result = $translator->translate('%d = 0 or > 1', ['_count' => 2]);
        $this->assertEquals('2 is 2-4 (po translated)', $result);
    }

    /**
     * Tests that plural rules are correctly used for the English language
     * using the basic formatter
     *
     * @return void
     */
    public function testPluralSelectionBasicFormatter()
    {
        $translator = I18n::getTranslator('special');
        $result = $translator->translate('There are {0} things', ['_count' => 2, 'plenty']);
        $this->assertEquals('There are plenty things', $result);

        $result = $translator->translate('There are {0} things', ['_count' => 1]);
        $this->assertEquals('There is only one', $result);
    }

    /**
     * Test plural rules are used for non-english languages
     *
     * @return void
     */
    public function testPluralSelectionRussian()
    {
        $translator = I18n::getTranslator('default', 'ru');
        $result = $translator->translate('{0} months', ['_count' => 1, 1]);
        $this->assertEquals('1 months ends in 1, not 11', $result);

        $result = $translator->translate('{0} months', ['_count' => 2, 2]);
        $this->assertEquals('2 months ends in 2-4, not 12-14', $result);

        $result = $translator->translate('{0} months', ['_count' => 7, 7]);
        $this->assertEquals('7 months everything else', $result);
    }

    /**
     * Tests that custom translation packages can be created on the fly and used later on
     *
     * @return void
     */
    public function testCreateCustomTranslationPackage()
    {
        I18n::setTranslator('custom', function () {
            $package = new Package('default');
            $package->setMessages([
                'Cow' => 'Le moo'
            ]);

            return $package;
        }, 'fr_FR');

        $translator = I18n::getTranslator('custom', 'fr_FR');
        $this->assertEquals('Le moo', $translator->translate('Cow'));
    }

    /**
     * Tests that messages can also be loaded from plugins by using the
     * domain = plugin_name convention
     *
     * @return void
     */
    public function testPluginMesagesLoad()
    {
        Plugin::load([
            'TestPlugin',
            'Company/TestPluginThree'
        ]);

        $translator = I18n::getTranslator('test_plugin');
        $this->assertEquals(
            'Plural Rule 1 (from plugin)',
            $translator->translate('Plural Rule 1')
        );

        $translator = I18n::getTranslator('company/test_plugin_three');
        $this->assertEquals(
            'String 1 (from plugin three)',
            $translator->translate('String 1')
        );
    }

    /**
     * Tests that messages messages from a plugin can be automatically
     * overridden by messages in app
     *
     * @return void
     */
    public function testPluginOverride()
    {
        Plugin::load('TestTheme');
        $translator = I18n::getTranslator('test_theme');
        $this->assertEquals(
            'translated',
            $translator->translate('A Message')
        );
    }

    /**
     * Tests the locale method
     *
     * @return void
     */
    public function testGetDefaultLocale()
    {
        $this->assertEquals('en_US', I18n::getLocale());
        $this->assertEquals('en_US', ini_get('intl.default_locale'));
        I18n::setLocale('fr_FR');
        $this->assertEquals('fr_FR', I18n::getLocale());
        $this->assertEquals('fr_FR', ini_get('intl.default_locale'));
    }

    /**
     * Tests that changing the default locale also changes the way translators
     * are fetched
     *
     * @return void
     */
    public function testGetTranslatorByDefaultLocale()
    {
        I18n::setTranslator('custom', function () {
            $package = new Package('default');
            $package->setMessages([
                'Cow' => 'Le moo'
            ]);

            return $package;
        }, 'fr_FR');

        I18n::setLocale('fr_FR');
        $translator = I18n::getTranslator('custom');
        $this->assertEquals('Le moo', $translator->translate('Cow'));
    }

    /**
     * Tests the __() function
     *
     * @return void
     */
    public function testBasicTranslateFunction()
    {
        I18n::setDefaultFormatter('sprintf');
        $this->assertEquals('%d is 1 (po translated)', __('%d = 1'));
        $this->assertEquals('1 is 1 (po translated)', __('%d = 1', 1));
        $this->assertEquals('1 is 1 (po translated)', __('%d = 1', [1]));
        $this->assertEquals('The red dog, and blue cat', __('The %s dog, and %s cat', ['red', 'blue']));
        $this->assertEquals('The red dog, and blue cat', __('The %s dog, and %s cat', 'red', 'blue'));
    }

    /**
     * Tests the __() functions with explicit null params
     *
     * @return void
     */
    public function testBasicTranslateFunctionsWithNullParam()
    {
        $this->assertEquals('text {0}', __('text {0}'));
        $this->assertEquals('text ', __('text {0}', null));

        $this->assertEquals('text {0}', __n('text {0}', 'texts {0}', 1));
{'fraction_non_alphanumeric': 0.14849692140528795, 'fraction_numerical': 0.015211879753712423, 'mean_word_length': 2.4194014447884418, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 125, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3724490', 'n_tokens_mistral': 4982, 'n_tokens_neox': 4751, 'n_words': 1201}