function setUp() {
		parent::setUp();
		
		$this->alternateBasePath = $this->getCurrentAbsolutePath() . "/_fakewebroot";
		$this->alternateBaseSavePath = TEMP_FOLDER . '/i18nTextCollectorTest_webroot';
		FileSystem::makeFolder($this->alternateBaseSavePath);
		Director::setBaseFolder($this->alternateBasePath);

		// Push a template loader running from the fake webroot onto the stack.
		$templateManifest = new SS_TemplateManifest($this->alternateBasePath, false, true);
		$templateManifest->regenerate(false);
		SS_TemplateLoader::instance()->pushManifest($templateManifest);
		$this->_oldTheme = SSViewer::current_theme();
		SSViewer::set_theme('testtheme1');
		
		$classManifest = new SS_ClassManifest($this->alternateBasePath, true, true, false);
		SS_ClassLoader::instance()->pushManifest($classManifest);

		$this->originalLocale = i18n::get_locale();
		
		// Override default adapter to avoid cached translations between tests.
		// Emulates behaviour in i18n::get_translators()
		$this->origAdapter = i18n::get_translator('core');
		$adapter = new Zend_Translate(array(
			'adapter' => 'i18nSSLegacyAdapter',
			'locale' => i18n::default_locale(),
			'disableNotices' => true,
		));
		i18n::register_translator($adapter, 'core');
		$adapter->removeCache();
		i18n::include_by_locale('en');
	}
 protected static function enable_custom_translations()
 {
     $locale = i18n::get_locale();
     $lang = i18n::get_lang_from_locale($locale);
     $profileDir = self::getProfileDir();
     $translators = array_reverse(i18n::get_translators(), true);
     // Make sure to include base translations
     i18n::include_by_locale($lang);
     foreach ($translators as $priority => $translators) {
         foreach ($translators as $name => $translator) {
             /* @var $adapter Zend_Translate_Adapter */
             $adapter = $translator->getAdapter();
             // Load translations from profile
             $filename = $adapter->getFilenameForLocale($lang);
             $filepath = Director::baseFolder() . "/mysite/lang/" . $profileDir . '/' . $filename;
             if ($filename && !file_exists($filepath)) {
                 continue;
             }
             $adapter->addTranslation(array('content' => $filepath, 'locale' => $lang));
         }
     }
 }
 public function setUp()
 {
     parent::setUp();
     $this->alternateBasePath = $this->getCurrentAbsolutePath() . "/_fakewebroot";
     $this->alternateBaseSavePath = TEMP_FOLDER . '/i18nTextCollectorTest_webroot';
     Filesystem::makeFolder($this->alternateBaseSavePath);
     Config::inst()->update('Director', 'alternate_base_folder', $this->alternateBasePath);
     // Replace old template loader with new one with alternate base path
     $this->_oldLoader = ThemeResourceLoader::instance();
     ThemeResourceLoader::set_instance(new ThemeResourceLoader($this->alternateBasePath));
     $this->_oldTheme = Config::inst()->get('SSViewer', 'theme');
     Config::inst()->update('SSViewer', 'theme', 'testtheme1');
     $classManifest = new SS_ClassManifest($this->alternateBasePath, false, true, false);
     SS_ClassLoader::instance()->pushManifest($classManifest);
     $this->originalLocale = i18n::get_locale();
     // Override default adapter to avoid cached translations between tests.
     // Emulates behaviour in i18n::get_translators()
     $this->origAdapter = i18n::get_translator('core');
     $adapter = new Zend_Translate(array('adapter' => 'i18nSSLegacyAdapter', 'locale' => i18n::default_locale(), 'disableNotices' => true));
     i18n::register_translator($adapter, 'core');
     $adapter->removeCache();
     i18n::include_by_locale('en');
 }
 /**
  * @param Zend_Translate Needs to implement {@link i18nTranslateAdapterInterface}
  * @param String If left blank will override the default translator.
  * @param Int
  */
 public static function register_translator($translator, $name, $priority = 10)
 {
     if (!is_int($priority)) {
         throw new InvalidArgumentException("register_translator expects an int priority");
     }
     // Ensure it's not there. If it is, we're replacing it. It may exist in a different priority.
     self::unregister_translator($name);
     // Add our new translator
     if (!isset(self::$translators[$priority])) {
         self::$translators[$priority] = array();
     }
     self::$translators[$priority][$name] = $translator;
     // Resort array, ensuring highest priority comes first
     krsort(self::$translators);
     i18n::include_by_locale('en_US');
     i18n::include_by_locale('en');
 }
Example #5
0
	/**
	 * This is the main translator function. Returns the string defined by $class and $entity according to the currently set locale.
	 *
	 * @param string $entity Entity that identifies the string. It must be in the form "Namespace.Entity" where Namespace will be usually
	 * 						 the class name where this string is used and Entity identifies the string inside the namespace.
	 * @param string $string The original string itself. In a usual call this is a mandatory parameter, but if you are reusing a string which
	 *				 has already been "declared" (using another call to this function, with the same class and entity), you can omit it.
	 * @param string $priority Optional parameter to set a translation priority. If a string is widely used, should have a high priority (PR_HIGH),
	 * 				    in this way translators will be able to prioritise this strings. If a string is rarely shown, you should use PR_LOW.
	 *				    You can use PR_MEDIUM as well. Leaving this field blank will be interpretated as a "normal" priority (less than PR_MEDIUM).
	 * @param string $context If the string can be difficult to translate by any reason, you can help translators with some more info using this param
	 *
	 * @return string The translated string, according to the currently set locale {@link i18n::set_locale()}
	 */
	static function _t($entity, $string = "", $priority = 40, $context = "") {
		global $lang;
		
		// get current locale (either default or user preference)
		$locale = i18n::get_locale();
		
		// parse $entity into its parts
		$entityParts = explode('.',$entity);
		$realEntity = array_pop($entityParts);
		$class = implode('.',$entityParts);
		
		// if language table isn't loaded for this locale, get it for each of the modules
		if(!isset($lang[$locale])) i18n::include_by_locale($locale);
		
		// fallback to the passed $string if no translation is present
		$transEntity = isset($lang[$locale][$class][$realEntity]) ? $lang[$locale][$class][$realEntity] : $string;
		
		// entities can be stored in both array and literal values in the language tables
		return (is_array($transEntity) ? $transEntity[0] : $transEntity);
	}
 public function testIncludeByLocaleWithoutFallbackLanguage()
 {
     $classManifest = new SS_ClassManifest($this->alternateBasePath, true, true, false);
     SS_ClassLoader::instance()->pushManifest($classManifest);
     $adapter = i18n::get_translator('core')->getAdapter();
     $this->assertTrue($adapter->isAvailable('en'));
     $this->assertFalse($adapter->isAvailable('mi'));
     // not defined at all
     $this->assertFalse($adapter->isAvailable('mi_NZ'));
     // defined, but not loaded yet
     $this->assertFalse($adapter->isTranslated('i18nTestModule.ENTITY', 'mi'), 'Existing unloaded entity not available before call');
     $this->assertFalse($adapter->isTranslated('i18nTestModule.ENTITY', 'mi_NZ'), 'Non-existing unloaded entity not available before call');
     i18n::include_by_locale('mi_NZ');
     $this->assertFalse($adapter->isAvailable('mi'));
     $this->assertTrue($adapter->isAvailable('mi_NZ'));
     $this->assertTrue($adapter->isTranslated('i18nTestModule.ENTITY', null, 'mi_NZ'), 'Includes module files');
     SS_ClassLoader::instance()->popManifest();
 }
Example #7
0
	function testIncludeByLocale() {
		// Looping through modules, so we can test the translation autoloading
		// Load non-exclusive to retain core class autoloading
		$classManifest = new SS_ClassManifest($this->alternateBasePath, true, true, false);
		SS_ClassLoader::instance()->pushManifest($classManifest);
		
		$adapter = i18n::get_translator('core')->getAdapter();
		$this->assertTrue($adapter->isAvailable('en'));
		$this->assertFalse($adapter->isAvailable('de'));
		$this->assertFalse($adapter->isTranslated('i18nTestModule.ENTITY', 'de'), 
			'Existing unloaded entity not available before call'
		);
		$this->assertFalse($adapter->isTranslated('i18nTestModule.ENTITY', 'af'), 
			'Non-existing unloaded entity not available before call'
		);

		i18n::include_by_locale('de');
		
		$this->assertTrue($adapter->isAvailable('en'));
		$this->assertTrue($adapter->isAvailable('de'));
		$this->assertTrue($adapter->isTranslated('i18nTestModule.ENTITY', null, 'de'), 'Includes module files');
		$this->assertTrue($adapter->isTranslated('i18nTestTheme1.LAYOUTTEMPLATE', null, 'de'), 'Includes theme files');
		$this->assertTrue($adapter->isTranslated('i18nTestModule.OTHERENTITY', null, 'de'), 'Includes submodule files');
		
		SS_ClassLoader::instance()->popManifest();
	}
 /**
  * Determine if an i18n entry is already present in the project
  *
  * @param string The namespace of the translation
  * @param string The entity of the translation
  * @return boolean
  */
 public static function i18n_entry_exists($namespace, $entity)
 {
     global $lang;
     $loc = i18n::get_locale();
     if (!isset($lang[$loc])) {
         i18n::include_by_locale($loc);
     }
     $arr = $lang[$loc];
     if (isset($arr[$namespace]) && is_array($arr[$namespace])) {
         return isset($arr[$namespace][$entity]);
     }
     return false;
 }
 public function testCollectMergesWithExisting()
 {
     $defaultlocal = i18n::default_locale();
     $local = i18n::get_locale();
     i18n::set_locale('en_US');
     i18n::set_default_locale('en_US');
     i18n::include_by_locale('en');
     i18n::include_by_locale('en_US');
     $c = new i18nTextCollector();
     $c->setWriter(new i18nTextCollector_Writer_Php());
     $c->basePath = $this->alternateBasePath;
     $c->baseSavePath = $this->alternateBaseSavePath;
     $entitiesByModule = $c->collect(null, true);
     $this->assertArrayHasKey('i18nTestModule.ENTITY', $entitiesByModule['i18ntestmodule'], 'Retains existing entities');
     $this->assertArrayHasKey('i18nTestModule.NEWENTITY', $entitiesByModule['i18ntestmodule'], 'Adds new entities');
 }