helper_plugin_data::makeTranslationReplacement PHP Method

makeTranslationReplacement() public method

Replace translation related placeholders in given string
public makeTranslationReplacement ( string $data ) : string
$data string
return string
    public function makeTranslationReplacement($data)
    {
        global $conf;
        global $ID;
        $patterns[] = '%lang%';
        if (isset($conf['lang_before_translation'])) {
            $values[] = $conf['lang_before_translation'];
        } else {
            $values[] = $conf['lang'];
        }
        // if translation plugin available, get current translation (empty for default lang)
        $patterns[] = '%trans%';
        /** @var helper_plugin_translation $trans */
        $trans = plugin_load('helper', 'translation');
        if ($trans) {
            $local = $trans->getLangPart($ID);
            if ($local === '') {
                $local = $conf['lang'];
            }
            $values[] = $local;
        } else {
            $values[] = '';
        }
        return str_replace($patterns, $values, $data);
    }

Usage Example

 public function testMakeTranslationReplacement()
 {
     $helper = new helper_plugin_data();
     $this->assertEquals('en', $helper->makeTranslationReplacement('%lang%'));
     $this->assertEquals('', $helper->makeTranslationReplacement('%trans%'));
     if (plugin_enable('translation')) {
         global $conf;
         global $ID;
         $conf['plugin']['translation']['translations'] = 'de';
         $ID = 'de:somepage';
         $this->assertEquals('en', $helper->makeTranslationReplacement('%lang%'));
         $this->assertEquals('de', $helper->makeTranslationReplacement('%trans%'));
     }
 }
All Usage Examples Of helper_plugin_data::makeTranslationReplacement