Skip to content

Commit

Permalink
fix for subdomain language mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Jackowski committed May 26, 2016
1 parent 7094077 commit f4ddddc
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions includes/lib/transifex-live-integration-javascript.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ class Transifex_Live_Integration_Javascript {
* @var array
*/
private $language_map;
private $url_options;

/**
* Public constructor, sets local settings
* @param array $settings Associative array of plugin settings.
*/
public function __construct( $settings, $live_settings ) {
Plugin_Debug::logTrace();
$this->live_settings = $live_settings; // set defaults
$this->live_settings = $live_settings; // set defaults
$this->live_settings['api_key'] = $settings['api_key']; // add api key
$this->lang = false;
$this->source_language = $settings['source_language'];
$this->language_map = $settings['language_map'];
$this->url_options = $settings['url_options'];
}

/**
Expand Down Expand Up @@ -98,18 +100,34 @@ function wp_head_hook() {
Plugin_Debug::logTrace();
$lang = $this->lang;
$live_settings = $this->live_settings;
if ( $lang ) {
$detectlang = <<<DETECTLANG
$snippet = '';

if ( $this->url_options === 2 ) {
$language_map = '';
foreach ($this->language_map as $key => $value) {
$language_map .= "case '$value': return '$key'; break; ";
}
$snippet .= <<<SUBDOMAIN
<script type="text/javascript">function subdomain_detect_lang() {var a = window.location.host.split( '.' )[0]; switch(a) { $language_map case 'www': return true; break;} return a;}</script>
SUBDOMAIN;
$detectlang = "subdomain_detect_lang";
} else {
if ( $lang ) {
$detectlang = <<<DETECTLANG
function() { return "$lang";}
DETECTLANG;
} else {
$live_settings_string = json_encode( $live_settings );
}
}
if ( !($live_settings_string ) ) {
$live_settings = array_merge( $live_settings, ['detectlang' => '%function%' ] );
$live_settings_json = json_encode( $live_settings );
$live_settings_string = str_replace( '"%function%"', $detectlang, $live_settings_json );
} else {
$live_settings_string = json_encode( $live_settings );
}

$snippet = <<<SNIPPET
$snippet .= <<<SNIPPET
<script type="text/javascript">window.liveSettings=$live_settings_string;</script>
<script type="text/javascript" src="//cdn.transifex.com/live.js"></script>\n
SNIPPET;
Expand Down

0 comments on commit f4ddddc

Please sign in to comment.