<commit_before><?php
/**
 * DCRM Localization System
 * Base On WordPress 4.1 Translation API
 *
 * Modify by Hintay
 * 
 * Keywords: __ , _e , _n:1,2 , _x:1,2c , _ex:1,2c , _nx:4c,1,2 , esc_attr__ , 
 * esc_attr_e , esc_attr_x:1,2c , esc_html__ , esc_html_e , esc_html_x:1,2c , 
 * _n_noop:1,2 , _nx_noop:3c,1,2 , __ngettext_noop:1,2
 *
 * More about gettext keywords: http://poedit.net/trac/wiki/Doc/Keywords
 */

 define('LANG_DIR', dirname(__FILE__));
/**
 * Get the current locale.
 *
 * If the locale is set, then it will filter the locale in the 'locale' filter
 * hook and return the value.
 *
 * If the locale is not set already, then the WPLANG constant is used if it is
 * defined. Then it is filtered through the 'locale' filter hook and the value
 * for the locale global set and the locale is returned.
 *
 * The process to get the locale should only be done once, but the locale will
 * always be filtered using the 'locale' hook.
 *
 * @since 1.5.0
 *
 * @return string The locale of the blog or from the 'locale' hook.
 */
 if ( !function_exists( 'get_locale' ) ):
function get_locale() {
	global $locale;

	if ( isset( $locale ) ) {
		return $locale;
	}

	if ( defined( 'DCRM_LANG' ) ) {
		$locale = DCRM_LANG;
		return $locale;
	}

	$langs = explode(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
	$locale = str_replace('-', '_', $langs[0]);

	if ( !empty( $locale ) ){
		if( !(strpos( $locale, '_' ) === FALSE) ) {
			$lang_coun = explode('_', $locale);
			// ISO 语言代码
			$lang = $lang_coun[0];
			// ISO 国家代码
			$coun = $lang_coun[1];

			// 特殊语言代码判断
			switch( $coun ) {
				case 'hans':
					$coun = 'Hans';
					break;
				case 'hant':
					$coun = 'Hant';
					break;
				default:
					$coun =  strtoupper($coun);
			}
			$fin_local = $lang . $coun;
		} else {
			$lang = $locale;
			$fin_local = $locale;
		}

		if( !file_exists( LANG_DIR.'/'.$fin_local.'.mo' ) ) {
			//检查2位语言代码
			if( file_exists( LANG_DIR.'/'.$lang.'.mo' ) ) {
				$locale = $lang;
				return $locale;
			} else {
				//检查近似语言
				$files = scandir( LANG_DIR . '/' );
				if ( $files ){
					foreach ( $files as $file ) {
						if ( '.' === $file[0] || is_dir( $file ) ) {
							continue;
						}
						if ( preg_match( '/(?:(.+)-)?' . $lang . '_([A-Za-z_]{1,4}).mo/', $file, $match ) ) {
							$similarlangs[] = $match;
						}
					}
					if ( isset( $similarlangs ) ) {
						$locale = $lang . '_' . $similarlangs[0];
						return $locale;
					}
				}
			}
		} else {
			$locale = $fin_local;
			return $locale;
		}
	}

	$locale = 'zh_CN';
	return $locale;
}
endif;

/**
 * Retrieve the translation of $text.
 *
 * If there is no translation, or the text domain isn't loaded, the original text is returned.
 *
 * *Note:* Don't use {@see translate()} directly, use `{@see __()} or related functions.
 *
 * @since 2.2.0
 *
 * @param string $text   Text to translate.
 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 * @return string Translated text
 */
function translate( $text, $domain = 'default' ) {
	$translations = get_translations_for_domain( $domain );
	$translations = $translations->translate( $text );

	return $translations;
}

/**<commit_msg>重写浏览器语言获取函数。 Rewrite Browser Language Detect Function.<commit_after><?php
/**
 * DCRM Localization System
 * Base On WordPress 4.1 Translation API
 *
 * Modify by Hintay
 * 
 * Keywords: __ , _e , _n:1,2 , _x:1,2c , _ex:1,2c , _nx:4c,1,2 , esc_attr__ , 
 * esc_attr_e , esc_attr_x:1,2c , esc_html__ , esc_html_e , esc_html_x:1,2c , 
 * _n_noop:1,2 , _nx_noop:3c,1,2 , __ngettext_noop:1,2
 *
 * More about gettext keywords: http://poedit.net/trac/wiki/Doc/Keywords
 */

define('LANG_DIR', dirname(__FILE__));
/**
 * Get the current locale.
 *
 * If the locale is set, then it will filter the locale in the 'locale' filter
 * hook and return the value.
 *
 * If the locale is not set already, then the WPLANG constant is used if it is
 * defined. Then it is filtered through the 'locale' filter hook and the value
 * for the locale global set and the locale is returned.
 *
 * The process to get the locale should only be done once, but the locale will
 * always be filtered using the 'locale' hook.
 *
 * @since 1.5.0
 *
 * @return string The locale of the blog or from the 'locale' hook.
 */
if ( !function_exists( 'get_locale' ) ):
function get_locale() {
	global $locale;

	if ( isset( $locale ) ) {
		return $locale;
	}

	if ( defined( 'DCRM_LANG' ) ) {
		$locale = DCRM_LANG;
		return $locale;
	}

	$localelist = get_browser_languages();

	if ( !empty( $localelist ) ){
		// 先按浏览器语言列表匹配文件
		foreach ( $localelist as $singlelocale ) {
			if( file_exists( LANG_DIR.'/'.$singlelocale.'.mo' ) ) {
				$locale = $singlelocale;
				return $locale;
			}
		}

		// 单独提取ISO 语言代码为 $langlist
		foreach ( $localelist as $key => $val ) {
			if( !(strpos( $val, '_' ) === FALSE) ) {
				$lang_coun = explode('_', $val);
				$lang = $lang_coun[0];
			} else {
				$lang = $val;
			}
			$langlist[$key] = $lang;
		}
		// 去掉重复项
		$langlist = array_unique( $langlist );

		// 按2位语言代码列表匹配文件
		foreach ( $langlist as $singlelang ) {
			if( file_exists( LANG_DIR.'/'.$singlelang.'.mo' ) ) {
				$locale = $singlelang;
				return $locale;
			}
		}

		// 检查近似语言，例如找不到 zh_TW 时寻找 zh_CN
		$files = scandir( LANG_DIR . '/' );
		foreach ( $files as $file ) {
			if ( '.' === $file[0] || is_dir( $file ) ) {
				continue;
			}
			foreach ( $langlist as $singlelang ) {
				if ( preg_match( '/(?:(.+)-)?' . $singlelang . '_([A-Za-z_]{1,4}).mo/', $file, $match ) ) {
					$similarlang = $match;
					$locale = $singlelang . '_' . $similarlang;
					return $locale;
				}
			}
		}
	}

	$locale = 'zh_CN';
	return $locale;
}
endif;

/**
 * Get the browser languages.
 * 获取浏览器设定的语言列表，并做预处理
 *
 */
function get_browser_languages() {
	if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
		// break up string into pieces (languages and q factors)
		preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
		$k = $lang_parse[1];

		foreach ($k as $key => $val) {
			$val = str_replace('-', '_', $val);
			if( !(strpos( $val, '_' ) === FALSE) ) {
				$lang_coun = explode('_', $val);
				$coun = $lang_coun[1];
				// 特殊语言代码处理
				switch( $coun ) {
					case 'hans':
						$coun = 'Hans';
						break;
					case 'hant':
						$coun = 'Hant';
						break;
					default:
						$coun =  strtoupper($coun);
				}
				$val = $lang_coun[0] . '_' . $coun;
			}
			$k[$key] = $val;
		}
		return $k;
	}
	return null;
}

/**
 * Retrieve the translation of $text.
 *
 * If there is no translation, or the text domain isn't loaded, the original text is returned.
 *
 * *Note:* Don't use {@see translate()} directly, use `{@see __()} or related functions.
 *
 * @since 2.2.0
 *
 * @param string $text   Text to translate.
 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 * @return string Translated text
 */
function translate( $text, $domain = 'default' ) {
	$translations = get_translations_for_domain( $domain );
	$translations = $translations->translate( $text );

	return $translations;
}

/**
{'fraction_non_alphanumeric': 0.16641093597433393, 'fraction_numerical': 0.011159157483609988, 'mean_word_length': 2.9767054908485857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2439914', 'n_tokens_mistral': 3090, 'n_tokens_neox': 2764, 'n_words': 708}