Skip to content

Instantly share code, notes, and snippets.

@stepanselyuk
Created September 1, 2014 13:17
Show Gist options
  • Save stepanselyuk/0809585a16fa68c15548 to your computer and use it in GitHub Desktop.
Save stepanselyuk/0809585a16fa68c15548 to your computer and use it in GitHub Desktop.
TwitterCldrAsset Yii2
<?php
/**
* Created by PhpStorm.
* Author: Stepan Seliuk <stepan@selyuk.com>
* Date: 31/08/14
* Time: 19:45
*/
namespace app\assets;
use app\components\AssetBundleMinifyBase;
/**
* Class TwitterCldrAsset
* @package app\assets
*/
class TwitterCldrAsset extends AssetBundleMinifyBase
{
public $sourcePath = '@resources';
public $css = [ ];
public $js = [ ];
public $depends = [ ];
public function init()
{
parent::init();
$file = $this->getFileName();
if ($file !== false) {
$this->js[ 'twitter-cldr.notminify' ] = [
$file
];
}
}
protected function getFileName()
{
// Список доступных файлов
$available =
'af,ar,be,bg,bn,ca,cs,cy,da,de-CH,de,el,en-150,en-AU,en-CA,en-GB,en-IE,en-SG,en-ZA,en,es-419,es-CO,es-MX,es-US,es,eu,fa,fi,fil,' //
. 'fr-BE,fr-CA,fr-CH,fr,ga,gl,he,hi,hr,hu,id,is,it-CH,it,ja,ko,lv,msa,nl,no,pl,pt,ro,ru,sk,sq,sr,sv,ta,th,tr,uk,ur,vi,zh-cn,zh-tw';
// Локаль системы
$language = \Yii::$app->language;
// Код языка из локали системы
$primaryLanguage = \Locale::getPrimaryLanguage( \Yii::$app->language );
// Базовый Url
$baseUrl = '@githubRaw/twitter/twitter-cldr-js/master/lib/assets/javascripts/twitter_cldr';
if (( $pos = stripos( $available, $language ) ) !== false) {
// Есть локаль среди доступных файлов
return $baseUrl . '/' . substr( $available, $pos, strlen( $language ) ) . '.js';
} elseif (preg_match( '/(' . $primaryLanguage . ')(?!\-)/i', $available, $m )) {
// Есть код языка среди доступных файлов
return $baseUrl . '/' . $m[ 1 ] . '.js';
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment