Skip to content

Commit

Permalink
TranslateBehavior's method TranslationField returns standard table al…
Browse files Browse the repository at this point in the history
…ias for default locale
  • Loading branch information
roberto-maldonado committed Mar 17, 2017
1 parent b99a7b2 commit d0dab44
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/ORM/Behavior/TranslateBehavior.php
Expand Up @@ -436,6 +436,9 @@ public function locale($locale = null)
public function translationField($field)
{
$table = $this->_table;
if($this->locale() === $this->getConfig('defaultLocale')) {
return $table->aliasField($field);
}
$associationName = $table->getAlias() . '_' . $field . '_translation';

if ($table->associations()->has($associationName)) {
Expand Down
67 changes: 64 additions & 3 deletions tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Expand Up @@ -328,11 +328,47 @@ public function testFindSingleLocaleWithConditions()
public function testTranslationFieldForTranslatedFields()
{
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$table->addBehavior('Translate', [
'fields' => ['title', 'body'],
'defaultLocale' => 'en_US'
]);

$expectedSameLocale = 'Articles.title';
$expectedOtherLocale = 'Articles_title_translation.content';

$expected = 'Articles_title_translation.content';
$field = $table->translationField('title');
$this->assertSame($expected, $field);
$this->assertSame($expectedSameLocale, $field);

I18n::locale('es_ES');
$field = $table->translationField('title');
$this->assertSame($expectedOtherLocale, $field);

I18n::locale('en');
$field = $table->translationField('title');
$this->assertSame($expectedOtherLocale, $field);

$table->removeBehavior('Translate');

$table->addBehavior('Translate', [
'fields' => ['title', 'body'],
'defaultLocale' => 'de_DE'
]);

I18n::locale('de_DE');
$field = $table->translationField('title');
$this->assertSame($expectedSameLocale, $field);

I18n::locale('en_US');
$field = $table->translationField('title');
$this->assertSame($expectedOtherLocale, $field);

$table->locale('de_DE');
$field = $table->translationField('title');
$this->assertSame($expectedSameLocale, $field);

$table->locale('es');
$field = $table->translationField('title');
$this->assertSame($expectedOtherLocale, $field);
}

/**
Expand Down Expand Up @@ -1700,4 +1736,29 @@ public function testBuildMarshalMapUpdateEntitiesValidationErrors()
];
$this->assertEquals($expected, $entity->errors('es'));
}

// /**
// * Test that translationField method returns the standard column name
// * while using the default locale, and the i18n table alias when
// * using other language
// *
// * @return void
// */
// public function testTranslationFieldErrors()
// {
// $table = TableRegistry::get('Articles');
// $table->addBehavior('Translate', [
// 'fields' => ['title'],
// 'defaultLocale' => 'en_US'
// ]);
//
//// I18n::locale('en_US');
//// $this->assertEquals('Articles.title', $table->translationField('title'));
//
//// I18n::locale('de_DE');
//// $this->assertEquals($table->translationField('title'), 'Articles_title_translation.content');
//
//// I18n::locale('en');
//// $this->assertEquals($table->translationField('title'), 'Articles_title_translation.content');
// }
}

0 comments on commit d0dab44

Please sign in to comment.