示例#1
0
<?php

require_once dirname(__FILE__) . 'Localizer.php';
$localizerLanguage = new LocalizerLanguage('locals', 'xx');
$localizerLanguage->setMode('xml');
echo "Add some strings...<br>";
$localizerLanguage->addString("foo", "foo");
$localizerLanguage->addString("bar", "bar");
$localizerLanguage->addString("high", "high");
$localizerLanguage->addString("low", "low");
$localizerLanguage->dumpToHtml();
echo "Add a string in between...<br>";
$localizerLanguage->addString("test", "test", 1);
$localizerLanguage->dumpToHtml();
echo "Update the value of 'test'...<br>";
$success = $localizerLanguage->updateString("test", "test", "***");
if (!$success) {
    echo "ERROR UPDATING VALUE<br>";
}
$localizerLanguage->dumpToHtml();
echo "Update the key for 'test'...<br>";
$success = $localizerLanguage->updateString("test", "test_new");
if (!$success) {
    echo "ERROR UPDATING KEY<br>";
}
$localizerLanguage->dumpToHtml();
echo "Update the key and value for 'test'...<br>";
$success = $localizerLanguage->updateString("test_new", "boo", "ghost");
if (!$success) {
    echo "ERROR UPDATING STRING<br>";
}
示例#2
0
    /**
     * Update a set of strings in a language file.
     * @param string $p_prefix
     * @param string $p_languageCode
     * @param array $p_data
     *
     * @return mixed
     * 		Return TRUE on success, or an array of PEAR_Errors on failure.
     */
    public static function ModifyStrings($p_prefix, $p_languageId, $p_data)
    {
        global $g_localizerConfig;
      	// If we change a string in the default language,
      	// then all the language files must be updated with the new key.
        if ($p_languageId == $g_localizerConfig['DEFAULT_LANGUAGE']) {
	        $languages = Localizer::GetLanguages();
	        $saveResults = array();
	        foreach ($languages as $language) {

	        	// Load the language file
	        	$source = new LocalizerLanguage($p_prefix, $language->getLanguageId());
	        	$tmpResult = $source->loadFile(Localizer::GetMode());

	        	// If we cant load the file, record the error and move on to the next file.
				if (PEAR::isError($tmpResult)) {
					$saveResults[] = $tmpResult;
					continue;
				}

	        	// For the default language, we set the key & value to be the same.
	        	if ($p_languageId == $language->getLanguageId()) {
	        		foreach ($p_data as $pair) {
	        			$source->updateString($pair['key'], $pair['value'], $pair['value']);
	        		}
	        	}
	        	// For all other languages, we just change the key and keep the old value.
	        	else {
	        		foreach ($p_data as $pair) {
	        			$source->updateString($pair['key'], $pair['value']);
	        		}
	        	}

	        	// Save the file
				$tmpResult = $source->saveFile(Localizer::GetMode());
				if (PEAR::isError($tmpResult)) {
					$saveResults[] = $tmpResult;
				}
	        }
	        return count($saveResults) == 0 ? true : $saveResults;
        }
      	// We only need to change the values in one file.
        else {
        	// Load the language file
        	$source = new LocalizerLanguage($p_prefix, $p_languageId);
        	$result = $source->loadFile(Localizer::GetMode());
        	if (PEAR::isError($result)) {
        		return array($result);
        	}
    		foreach ($p_data as $pair) {
    			$source->updateString($pair['key'], $pair['key'], $pair['value']);
    		}
        	// Save the file
			$result = $source->saveFile(Localizer::GetMode());
			if (PEAR::isError($result)) {
				return array($result);
			} else {
				return true;
			}
        }
    } // fn ModifyStrings