Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多言語対応 #40

Merged
merged 4 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Config/Migration/1484283232_update_enable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* 多言語化対応
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('NetCommonsMigration', 'NetCommons.Config/Migration');

/**
* 多言語化対応
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\M17n\Config\Migration
*/
class UpdateEnable extends NetCommonsMigration {

/**
* Migration description
*
* @var string
*/
public $description = 'update_enable';

/**
* Actions to be performed
*
* @var array $migration
*/
public $migration = array(
'up' => array(
),
'down' => array(
),
);

/**
* Before migration callback
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function before($direction) {
return true;
}

/**
* After migration callback
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function after($direction) {
$Language = $this->generateModel('Language');

if ($direction === 'up') {
$update = array(
'is_active' => false
);
$conditions = array(
'id' => '1'
);
if (! $Language->updateAll($update, $conditions)) {
return false;
}

$update = array(
'is_active' => true
);
$conditions = array(
'id' => '2'
);
if (! $Language->updateAll($update, $conditions)) {
return false;
}
}

return true;
}
}
53 changes: 53 additions & 0 deletions Config/Migration/M17nAddFieldsMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* MailsMigration
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('NetCommonsMigration', 'NetCommons.Config/Migration');

/**
* MailsMigration
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\M17n\Config\Migration
*/
class M17nAddFieldsMigration extends NetCommonsMigration {

/**
* After migration callback
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function after($direction) {
if ($direction === 'up') {
foreach ($this->models as $modelName => $addConditions) {
$model = $this->generateModel($modelName);

$update = array('is_translation' => true);
if ($model->hasField('key')) {
$conditions = array('LENGTH(' . $model->alias . '.`key`) != 32');
} else {
$conditions = array();
}
$conditions = Hash::merge($conditions, $addConditions);
if (! $model->updateAll($update, $conditions)) {
return false;
}

$update = array('is_origin' => false);
$conditions = array($model->alias . '.language_id !=' => '2');
$conditions = Hash::merge($conditions, $addConditions);
if (! $model->updateAll($update, $conditions)) {
return false;
}
}
}
return true;
}
}
52 changes: 52 additions & 0 deletions Model/Behavior/AddM17nBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* M17nBehavior
*
* @property OriginalKey $OriginalKey
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('ModelBehavior', 'Model');

/**
* AddM17nBehavior
*
* 登録するコンテンツデータに対して、対応している言語分登録します。<br>
*
* コンテンツデータのテーブルに以下のフィールドを保持してください。
* * language_id
* 言語コードに対応するidが登録されます。
* * is_origin
* オリジナルデータとします。
* * is_translation
* 翻訳したかどうか。
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\M17n\Model\Befavior
*/
class AddM17nBehavior extends ModelBehavior {

/**
* is_translationの更新
*
* @param Model $model 呼び出し元Model
* @return bool
* @throws InternalErrorException
*/
public function saveTranslation(Model $model) {
//$dataSource = ConnectionManager::getDataSource($model->useDbConfig);
//$tables = $dataSource->listSources();

//if (! in_array($model->tablePrefix . $model->useTable, $tables)) {
// return false;
//}

return true;
}

}
23 changes: 23 additions & 0 deletions Model/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

App::uses('M17nAppModel', 'M17n.Model');
App::uses('M17n.M17nHelper', 'M17n.Controller');

/**
* Language Model
Expand Down Expand Up @@ -117,6 +118,28 @@ public function getLanguages() {
return self::$languages;
}

/**
* 言語データを取得
*
* @return array
*/
public function getLanguagesWithName() {
$languages = $this->find('list', array(
'fields' => array('code', 'is_active'),
'recursive' => -1,
));
$activeLangs = array();
$enableLangs = array_intersect_key(M17nHelper::$languages, $languages);
foreach ($enableLangs as $code => $value) {
if ($languages[$code]) {
$activeLangs[] = $code;
}
$enableLangs[$code] = __d('m17n', $value);
}

return array($activeLangs, $enableLangs);
}

/**
* 利用可能な言語登録
*
Expand Down