Permalink
Browse files

[Opc_20] Simplifying the implementation of Opc_Translate without loos…

…ing the functionality.
  • Loading branch information...
1 parent 377f849 commit c92bd8581f4bf9fa6997b7d49b87650cce2186e7 @zyxist zyxist committed Sep 5, 2009
Showing with 89 additions and 118 deletions.
  1. +57 −63 lib/Translate.php
  2. +5 −11 lib/Translate/Adapter.php
  3. +27 −44 lib/Translate/Adapter/Ini.php
View
@@ -42,7 +42,7 @@ class Opc_Translate implements Opl_Translation_Interface
* Array contains groups and its languages when they are other than main.
* @var array
*/
- $_groupsLanguage = array(),
+ $_groupLanguages = array(),
/**
* Default language to use in case when there is no language selected
* or selected language has not specified required translation.
@@ -133,42 +133,19 @@ public function _($group, $id)
$adapter = $this->_defaultAdapter;
}
- // Check if there is set language.
- if($this->_currentLanguage === null)
+ // Select the language
+ $languages = array();
+ if(isset($this->_groupLanguages[$group]))
{
- if($groupAdapter)
- {
- $this->setGroupLanguage($group, $this->_defaultLanguage);
- }
- else
- {
- $this->setLanguage($this->_defaultLanguage);
- }
+ $languages[] = $this->_groupLanguages[$group];
}
+ $languages[] = $this->_currentLanguage;
+ $languages[] = $this->_defaultLanguage;
+
// Try to get translated message.
- if(($msg = $adapter->getMessage($this->_currentLanguage, $group, $id)) !== null)
- {
- return $msg;
- }
- // Try to load current language
- if($adapter->loadGroupLanguage($group, $this->_currentLanguage))
- {
- // Try to get translated message if language is loaded now
- if(($msg = $adapter->getMessage($this->_currentLanguage, $group, $id)) !== null)
- {
- return $msg;
- }
- }
- // Try to get default message.
- if(($msg = $adapter->getMessage($this->_defaultLanguage, $group, $id, 'default')) !== null)
- {
- return $msg;
- }
- // Try to load default language
- if($adapter->loadGroupLanguage($group, $this->_defaultLanguage))
+ foreach($languages as $lang)
{
- // Try to get default message if language is loaded now
- if(($msg = $adapter->getMessage($this->_defaultLanguage, $group, $id)) !== null)
+ if(($msg = $adapter->getMessage($lang, $group, $id)) !== null)
{
return $msg;
}
@@ -180,7 +157,16 @@ public function assign($group, $id)
{
$data = func_get_args();
$adapter = null;
- unset($data[0],$data[1]);
+ // Filter arrays
+ if(is_array($data[2]))
+ {
+ $data = $data[2];
+ }
+ else
+ {
+ unset($data[0], $data[1]);
+ }
+ // Load the adapter
if(isset($this->_groupAdapters[$group]))
{
$adapter = $this->_groupAdapters[$group];
@@ -189,7 +175,22 @@ public function assign($group, $id)
{
$adapter = $this->_defaultAdapter;
}
- $adapter->assign($group, $id, $data);
+ // Select the language
+ $languages = array();
+ if(isset($this->_groupLanguages[$group]))
+ {
+ $languages[] = $this->_groupLanguages[$group];
+ }
+ $languages[] = $this->_currentLanguage;
+ $languages[] = $this->_defaultLanguage;
+
+ foreach($languages as $lang)
+ {
+ if($adapter->assign($lang, $group, $id, $data))
+ {
+ break;
+ }
+ }
} // end assign();
/**
@@ -207,45 +208,38 @@ public function setLanguage($language)
} // end setLanguage();
/**
- * Sets language to specified group.
+ * Sets the group-specific language for the group. The
+ * null value removes the group language.
*
* @param string $group Group name
- * @param string $language New language
- * @return boolean
+ * @param string $language The group language
*/
public function setGroupLanguage($group, $language)
{
- if(isset($this->_groupAdapters[$group]))
+ if($language === null)
{
- if($this->_groupAdapters[$group]->loadGroupLanguage($group, $language))
- {
- $this->_groupsLanguage[$group] = $language;
- return true;
- }
- elseif($this->_groupAdapters[$group]->loadGroupLanguage($group, $this->_defaultLanguage, 'default'))
- {
- $this->_groupsLanguage[$group] = $this->_defaultLanguage;
- return false;
- }
- else
- {
- throw new Opc_TranslateFileNotFound_Exception($language, 'translation');
- }
+ unset($this->_groupLanguages[$group]);
}
else
{
- if($this->_defaultAdapter->loadGroupLanguage($group, $language))
- {
- $this->_groupsLanguage[$group] = $language;
- return true;
- }
- elseif($this->_defaultAdapter->loadGroupLanguage($group, $this->_defaultLanguage, 'default'))
- {
- $this->_groupsLanguage[$group] = $this->_defaultLanguage;
- }
- return false;
+ $this->_groupLanguages[$group] = (string)$language;
}
} // end setGroupLanguage();
+
+ /**
+ * If the group has a custom language defined, returns this
+ * language name. In any other case, it return null.
+ * @param string $group The group name
+ * @return string|null
+ */
+ public function getGroupLanguage($group)
+ {
+ if(isset($this->_groupLanguages[$group]))
+ {
+ return $this->_groupLanguages[$group];
+ }
+ return null;
+ } // end getGroupLanguage();
/**
* Gives access to control default language.
View
@@ -33,20 +33,14 @@
abstract public function getMessage($language, $group, $msg);
/**
- * Assings the data to the specified message.
+ * Assings the data to the specified message. The method must return
+ * true, if the assignment was successful and false otherwise.
*
+ * @param string $language The language
* @param string $group The message group
* @param string $id The message identifier
* @param array $data The data to assign
+ * @return boolean
*/
- abstract public function assign($group, $id, $data);
-
- /**
- * Loads new language for specified group.
- *
- * @param string $group Group name
- * @param string $language New language
- * @param string $type Type of translation
- */
- abstract public function loadGroupLanguage($group, $language, $type = 'translation');
+ abstract public function assign($language, $group, $id, Array $data);
} // end Opc_Translate_Adapter;
@@ -29,20 +29,15 @@ class Opc_Translate_Adapter_Ini extends Opc_Translate_Adapter
*/
$_directory = null,
/**
- * Loaded translation.
+ * Loaded messages
* @var array
*/
- $_translation = null,
- /**
- * Loaded default translation.
- * @var array
- */
- $_default = null,
+ $_messages = array(),
/**
* Assigned from template values.
* @var array
*/
- $_assigned = null,
+ $_assigned = array(),
/**
* File existence checking state.
* @var boolean
@@ -104,17 +99,21 @@ public function getDirectory()
*/
public function getMessage($language, $group, $id)
{
- if(isset($this->_assigned[$group][$id]))
+ if(isset($this->_assigned[$group]) && $this->_assigned[$group][$id])
{
return $this->_assigned[$group][$id];
}
- if(isset($this->_translation[$group][$id]))
+ if(!isset($this->_messages[$language]))
+ {
+ $this->_messages[$language] = array();
+ }
+ if(!isset($this->_messages[$language][$group]))
{
- return $this->_translation[$group][$id];
+ $this->loadGroup($language, $group);
}
- if(isset($this->_default[$group][$id]))
+ if(isset($this->_messages[$language][$group][$id]))
{
- return $this->_default[$group][$id];
+ return $this->_messages[$language][$group][$id];
}
return null;
} // end getMessage();
@@ -125,21 +124,17 @@ public function getMessage($language, $group, $id)
* @param string $group The message group
* @param string $id The message identifier
* @param array $data The data to assign.
+ * @return boolean
*/
- public function assign($group, $id, $data)
+ public function assign($language, $group, $id, Array $data)
{
- if(isset($this->_translation[$group][$id]))
- {
- $this->_assigned[$group][$id] = vsprintf($this->_translation[$group][$id], $data);
- }
- elseif(isset($this->_default[$group][$id]))
+ $message = $this->getMessage($language, $group, $id);
+ if($message === null)
{
- $this->_assigned[$group][$id] = vsprintf($this->_default[$group][$id], $data);
- }
- else
- {
- throw new Opc_TranslateCannotAssignData_Exception($group, $id);
+ return false;
}
+ $this->_assigned[$group][$id] = vsprintf($message, $data);
+ return true;
} // end assign();
/**
@@ -148,32 +143,20 @@ public function assign($group, $id, $data)
* @param string $group Group name
* @param string $language The language name.
* @param string $type Type of translation
- * @return boolean
+ * @throws Opc_TranslateGroupFileNotFound_Exception
*/
- public function loadGroupLanguage($group, $language, $type = 'translation')
+ public function loadGroup($language, $group)
{
- $data = @parse_ini_file($this->_directory.$language.DIRECTORY_SEPARATOR.$group.'.ini');
- if($data === false)
+ if(!isset($this->_messages[$language]))
{
- if($this->_fileCheck)
- {
- throw new Opc_TranslateGroupFileNotFound_Exception($group, $language);
- }
- else
- {
- return false;
- }
+ $this->_messages[$language] = array();
}
- switch($type)
+ $this->_messages[$language][$group] = @parse_ini_file($this->_directory.$language.DIRECTORY_SEPARATOR.$group.'.ini');
+ if($this->_messages[$language][$group] === false)
{
- case 'translation':
- $this->_translation[$group] = $data;
- break;
- case 'default':
- $this->_default[$group] = $data;
- break;
+ unset($this->_messages[$language][$group]);
+ throw new Opc_TranslateGroupFileNotFound_Exception($group, $language);
}
- return true;
} // end loadGroupLanguage();

0 comments on commit c92bd85

Please sign in to comment.