Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating Horde_Translation_Autodetect which uses late static bindings…
… to detect the locale directory.
- Loading branch information
Showing
11 changed files
with
146 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
framework/Translation/lib/Horde/Translation/Autodetect.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/** | ||
* Copyright 2010-2014 Horde LLC (http://www.horde.org/) | ||
* | ||
* See the enclosed file COPYING for license information (LGPL). If you | ||
* did not receive this file, see http://www.horde.org/licenses/lgpl21. | ||
* | ||
* @category Horde | ||
* @copyright 2010-2014 Horde LLC | ||
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 | ||
* @package Translation | ||
* @since 2.2.0 | ||
*/ | ||
|
||
/** | ||
* The Horde_Translation_Autodetect auto detects the locale directory location | ||
* for the class implementing it. | ||
* | ||
* @author Jan Schneider <jan@horde.org> | ||
* @category Horde | ||
* @copyright 2010-2014 Horde LLC | ||
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 | ||
* @package Translation | ||
* @since 2.2.0 | ||
*/ | ||
abstract class Horde_Translation_Autodetect extends Horde_Translation | ||
{ | ||
/** | ||
* The absolute PEAR path to the translations for the default gettext handler. | ||
* | ||
* This value is automatically set by PEAR Replace Tasks. | ||
* | ||
* @var string | ||
*/ | ||
protected static $_pearDirectory; | ||
|
||
/** | ||
* Auto detects the locale directory location. | ||
* | ||
* @param string $handlerClass The name of a class implementing the | ||
* Horde_Translation_Handler interface. | ||
*/ | ||
public static function loadHandler($handlerClass) | ||
{ | ||
if (!static::$_domain) { | ||
throw new Horde_Translation_Exception('The domain property must be set by the class that extends Horde_Translation_Autodetect.'); | ||
} | ||
|
||
$directory = static::_searchLocaleDirectory(); | ||
if (!$directory) { | ||
throw new Horde_Translation_Exception(sprintf('Could not found find any locale directory for %s domain.', static::$_domain)); | ||
} | ||
|
||
static::$_directory = $directory; | ||
parent::loadHandler($handlerClass); | ||
} | ||
|
||
/** | ||
* Search for the locale directory for different installations methods (eg: PEAR, Composer). | ||
* | ||
* @var boolean|string The directory if found, or false when no valid directory is found | ||
*/ | ||
protected static function _searchLocaleDirectory() | ||
{ | ||
if (static::$_pearDirectory !== '@data_dir@') { | ||
$directory = static::$_pearDirectory . '/' . static::$_domain . '/locale'; | ||
if (file_exists($directory)) { | ||
return $directory; | ||
} | ||
} | ||
|
||
$className = get_called_class(); | ||
$class = new ReflectionClass($className); | ||
$basedir = dirname($class->getFilename()); | ||
$depth = substr_count($className, '_'); | ||
|
||
$scan = array( | ||
/* Composer */ | ||
$basedir . str_repeat('/..', $depth) . '/data/locale', | ||
/* Source */ | ||
$basedir . str_repeat('/..', $depth + 1) . '/locale' | ||
); | ||
foreach ($scan as $directory) { | ||
if (file_exists($directory)) { | ||
return $directory; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
framework/Translation/test/WrapperA/lib/Horde/WrapperA/Translation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
class Horde_WrapperA_Translation extends Horde_Translation | ||
{ | ||
static protected $_domain = 'Horde_WrapperA'; | ||
} |
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
framework/Translation/test/WrapperB/lib/Horde/WrapperB/Translation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
class Horde_WrapperB_Translation extends Horde_Translation | ||
{ | ||
static protected $_domain = 'Horde_WrapperB'; | ||
} |
File renamed without changes.
File renamed without changes.