WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] [polylang] How can I set defaullt value to polylang translations ? (3 posts)

  1. ben.IT
    Member
    Posted 1 year ago #

    Hi,
    I am setting up a new multilingual website with Polylang.
    I wonder how can I set defaullt value to polylang translations.

    I am thinking about this in order to deliver translation from a development envionment to a production environement..

    add_action( 'after_setup_theme', 'setup' );
    function setup ()
    {
    	pll_register_string('my_domain','sentence to translate')	 ;
    	//how to set the default value for a given language ?
    }
    
    <?php pll_e('sentence to translate'); ?>

    Thanks,
    greetings,
    ben

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    You can do something close to:

    global $polylang;
    $language = $polylang->get_language('en'); // example here for English
    $mo = $polylang->mo_import($language); // all string translations in $language
    $mo->add_entry($mo->make_entry('string to translate', 'default translation')); // add the default translation
    $polylang->mo_export($mo, $language); // save translations in the database
  3. ben.IT
    Member
    Posted 1 year ago #

    Hi chouby . Thanks for your answer. I wrote this yesterday to manage translation import when setting up the site.
    I my case I need french and english translation :

    <?php
    require_once( ABSPATH.'wp-content/plugins/polylang/include/admin-base.php');
    require_once( ABSPATH.'wp-content/plugins/polylang/include/admin.php');
    
    /**
     * Manage translation object for initial import. Need the string reference, the french translation and the english translation
     *
     *
    */
    class Translation
    {
    	public $termReference ;
    	public $termTranslationFr ;
    	public $termTranslationEn ;
    
    	public function __construct($termReference, $termTranslationFr, $termTranslationEn)
    	{
    		$this->termReference=$termReference ;
    		$this->termTranslationFr=$termTranslationFr;
    		$this->termTranslationEn=$termTranslationEn ;
    	}
    }//end of class Translation
    
    /**
     * Manage translation import. Imports Translation objects.
     * This class allows to import translation to polylang backoffice. polylang sets options in wp3_options.
     * Query to see how polylang options : select * from wp3_options where option_name like 'polylang_mo%'
     * This import mechanism his largely inspired by the 2 followings methods of polylang located in wp-content/plugins/polylang/include/base.php :
     * function mo_export($mo, $lang)
     * function mo_import($lang)
     */
    class TranslationImporter
    {
    	private $tableTranslations ; //contain all Translation objects to import
    	private $moFr ; //MO object for french translation
    	private $moEn ; //MO object for english translation
    
    	private $polylang_mo_fr_code='polylang_mo251' ;
    	private $polylang_mo_en_code='polylang_mo253' ;
    
    	public function __construct()
    	{
    		$this->moEn = new MO();
    		$this->moFr = new MO();
    		$this->tableTranslations = array();
    	}
    
    	/**
    	 * adds a Translation object to
    	 * @param unknown $translation the array tableTranslations
    	 */
    	public function addTranslationToImport($translation)
    	{
    		array_push($this->tableTranslations,$translation);
    	}
    
    	/**
    	 * import translations from tableTranslations attribute to the french MO and english MO attributes.
    	 */
    	public function importTranslationToMo()
    	{
    		foreach ($this->tableTranslations as $row)
    		{
    			//make_entry('reference string ', 'translation')
    			$this->moFr->add_entry($this->moFr->make_entry($row->termReference, $row->termTranslationFr));
    			$this->moEn->add_entry($this->moEn->make_entry($row->termReference, $row->termTranslationEn));
    		}
    	}
    
    	/**
    	 * Update polylang options from the mo attributes which has been fill in with the importTranslationToMo method
    	 */
    	public function updateOptionsFromMO()
    	{
    		$stringsFr = array();
    		foreach ($this->moFr->entries as $entry)
    			$strings[] = array($entry->singular, $this->moFr->translate($entry->singular));
    		update_option('polylang_mo'.'251', $strings);
    
    		$stringsEn = array();
    		foreach ($this->moEn->entries as $entry)
    			$strings[] = array($entry->singular, $this->moEn->translate($entry->singular));
    		update_option('polylang_mo'.'253', $strings);
    
    	}
    }//end of class TranslationImporter
    /**
     * This main script imports translation into polylang.
    */
    require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php') ; //use $_SERVER['DOCUMENT_ROOT'] to refer to WP root while ABSPATH isnt yet set.
    require_once(ABSPATH.'wp-content/plugins/international-int/classes/InternationalIntTranslationImporter.class.php');
    $start_time = microtime(true); //calcul exec time for fun 
    
    $TranslationImporter = new TranslationImporter();
    //add the translations to the importer
    $TranslationImporter->addTranslationToImport(new Translation('annuaire', 'annuaire', 'adress book'));
    $TranslationImporter->addTranslationToImport(new Translation('vos commentaires', 'c fr ', 'c en'));
    $TranslationImporter->addTranslationToImport(new Translation('INTRANET INTERNATIONAL', 'fr ', 'en'));
    $TranslationImporter->addTranslationToImport(new Translation('Evénements clients', 'Evénements clients', 'customers event'));
    $TranslationImporter->addTranslationToImport(new Translation('ref', 'fr ', 'en'));
    
    $TranslationImporter->importTranslationToMo(); //generate MOs in the importer
    $TranslationImporter->updateOptionsFromMO(); //update WP options from MOs importers
    
    echo 'end of translation import. execution took : '.(microtime(true) - $start_time).' seconds.';

    greetings,
    ben

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic