Skip to content

Commit

Permalink
Add missing DocBlocks in gp-includes/warnings.php.
Browse files Browse the repository at this point in the history
  • Loading branch information
ocean90 committed Oct 18, 2016
1 parent 071a42e commit 68e11be
Showing 1 changed file with 179 additions and 13 deletions.
192 changes: 179 additions & 13 deletions gp-includes/warnings.php
Expand Up @@ -12,37 +12,87 @@
* @since 1.0.0
*/
class GP_Translation_Warnings {
var $callbacks = array();

/**
* List of callbacks.
*
* @since 1.0.0
* @access public
*
* @var array
*/
public $callbacks = array();

/**
* Adds a callback for a new warning.
*
* @since 1.0.0
* @access public
*
* @param string $id Unique ID of the callback.
* @param callable $callback The callback.
*/
public function add( $id, $callback ) {
$this->callbacks[ $id ] = $callback;
}

/**
* Removes an existing callback for a warning.
*
* @since 1.0.0
* @access public
*
* @param string $id Unique ID of the callback.
*/
public function remove( $id ) {
unset( $this->callbacks[ $id ] );
}

/**
* Checks whether a callback exists for an ID.
*
* @since 1.0.0
* @access public
*
* @param string $id Unique ID of the callback.
* @return bool True if exists, false if not.
*/
public function has( $id ) {
return isset( $this->callbacks[ $id ] );
}

/**
* Checks translations for any issues/warnings.
*
* @since 1.0.0
* @access public
*
* @param string $singular The singular form of an original string.
* @param string $plural The plural form of an original string.
* @param array $translations An array of translations for an original.
* @param GP_Locale $locale The locale of the translations.
* @return array|null Null if no issues have been found, otherwise an array
* with warnings.
*/
public function check( $singular, $plural, $translations, $locale ) {
$problems = array();
foreach ( $translations as $translation_index => $translation ) {
if ( ! $translation ) {
continue;
}

$skip = array( 'singular' => false, 'plural' => false );
if ( ! is_null( $plural ) ) {
if ( null !== $plural ) {
$numbers_for_index = $locale->numbers_for_index( $translation_index );
if ( $locale->nplurals == 1 ) {
if ( 1 === $locale->nplurals ) {
$skip['singular'] = true;
} else if ( in_array( 1, $numbers_for_index ) ) {
} elseif ( in_array( 1, $numbers_for_index, true ) ) {
$skip['plural'] = true;
} else {
$skip['singular'] = true;
}
}

foreach ( $this->callbacks as $callback_id => $callback ) {
if ( ! $skip['singular'] ) {
$singular_test = call_user_func( $callback, $singular, $translation, $locale );
Expand All @@ -63,24 +113,63 @@ public function check( $singular, $plural, $translations, $locale ) {
}
}

/**
* Class used to register built-in translation warnings.
*
* @since 1.0.0
*/
class GP_Builtin_Translation_Warnings {

var $length_lower_bound = 0.2;
var $length_upper_bound = 5.0;
/**
* Lower bound for length checks.
*
* @since 1.0.0
* @access public
*
* @var float
*/
public $length_lower_bound = 0.2;

/**
* Upper bound for length checks.
*
* @since 1.0.0
* @access public
*
* @var float
*/
public $length_upper_bound = 5.0;

/**
* List of locales which are excluded from length checks.
*
* @since 1.0.0
* @access public
*
* @var array
*/
public $length_exclude_languages = array( 'art-xemoji', 'ja', 'zh', 'zh-hk', 'zh-cn', 'zh-sg', 'zh-tw' );

/**
* Checks whether lengths of source and translation differ too much.
*
* @since 1.0.0
* @access public
*
* @param string $original The source string.
* @param string $translation The translation.
* @param GP_Locale $locale The locale of the translation.
* @return string|true True if check is OK, otherwise warning message.
*/
public function warning_length( $original, $translation, $locale ) {
if ( in_array( $locale->slug, $this->length_exclude_languages ) ) {
if ( in_array( $locale->slug, $this->length_exclude_languages, true ) ) {
return true;
}

if ( gp_startswith( $original, 'number_format_' ) ) {
return true;
}

$len_src = gp_strlen( $original );
$len_trans = gp_strlen( $translation );
if (
Expand All @@ -98,11 +187,23 @@ public function warning_length( $original, $translation, $locale ) {
return true;
}

/**
* Checks whether HTML tags are missing or have been added.
*
* @since 1.0.0
* @access public
*
* @param string $original The source string.
* @param string $translation The translation.
* @param GP_Locale $locale The locale of the translation.
* @return string|true True if check is OK, otherwise warning message.
*/
public function warning_tags( $original, $translation, $locale ) {
$tag_pattern = '(<[^>]*>)';
$tag_re = "/$tag_pattern/Us";
$original_parts = preg_split( $tag_re, $original, - 1, PREG_SPLIT_DELIM_CAPTURE );
$translation_parts = preg_split( $tag_re, $translation, - 1, PREG_SPLIT_DELIM_CAPTURE );

if ( count( $original_parts ) > count( $translation_parts ) ) {
return __( 'Missing tags from translation.', 'glotpress' );
}
Expand All @@ -129,10 +230,10 @@ public function warning_tags( $original, $translation, $locale ) {
$original_is_tag = preg_match( "/^$tag_pattern$/", $original_tag );
$translation_is_tag = preg_match( "/^$tag_pattern$/", $translation_tag );

if ( $original_is_tag && $translation_is_tag && $original_tag != $translation_tag ) {
if ( $original_is_tag && $translation_is_tag && $original_tag !== $translation_tag ) {
$original_tag = preg_replace( $translatable_attr_regex, '', $original_tag );
$translation_tag = preg_replace( $translatable_attr_regex, '', $translation_tag );
if ( $original_tag != $translation_tag ) {
if ( $original_tag !== $translation_tag ) {
return $expected_error_msg;
}
}
Expand All @@ -142,7 +243,17 @@ public function warning_tags( $original, $translation, $locale ) {
return true;
}


/**
* Checks whether PHP placeholders are missing or have been added.
*
* @since 1.0.0
* @access public
*
* @param string $original The source string.
* @param string $translation The translation.
* @param GP_Locale $locale The locale of the translation.
* @return string|true True if check is OK, otherwise warning message.
*/
public function warning_placeholders( $original, $translation, $locale ) {
/**
* Filter the regular expression that is used to match placeholders in translations.
Expand Down Expand Up @@ -170,6 +281,16 @@ public function warning_placeholders( $original, $translation, $locale ) {
return true;
}

/**
* Counts the placeholders in a string.
*
* @since 1.0.0
* @access private
*
* @param string $string The string to search.
* @param string $re Regular expressions to match placeholders.
* @return array An array with counts per placeholder.
*/
private function _placeholders_counts( $string, $re ) {
$counts = array();
preg_match_all( "/$re/", $string, $matches );
Expand All @@ -180,6 +301,17 @@ private function _placeholders_counts( $string, $re ) {
return $counts;
}

/**
* Checks whether a translation does begin on newline.
*
* @since 1.0.0
* @access public
*
* @param string $original The source string.
* @param string $translation The translation.
* @param GP_Locale $locale The locale of the translation.
* @return string|true True if check is OK, otherwise warning message.
*/
public function warning_should_begin_on_newline( $original, $translation, $locale ) {
if ( gp_startswith( $original, "\n" ) && ! gp_startswith( $translation, "\n" ) ) {
return __( 'Original and translation should both begin on newline.', 'glotpress' );
Expand All @@ -188,6 +320,17 @@ public function warning_should_begin_on_newline( $original, $translation, $local
return true;
}

/**
* Checks whether a translation doesn't begin on newline.
*
* @since 1.0.0
* @access public
*
* @param string $original The source string.
* @param string $translation The translation.
* @param GP_Locale $locale The locale of the translation.
* @return string|true True if check is OK, otherwise warning message.
*/
public function warning_should_not_begin_on_newline( $original, $translation, $locale ) {
if ( ! gp_startswith( $original, "\n" ) && gp_startswith( $translation, "\n" ) ) {
return __( 'Translation should not begin on newline.', 'glotpress' );
Expand All @@ -196,6 +339,17 @@ public function warning_should_not_begin_on_newline( $original, $translation, $l
return true;
}

/**
* Checks whether a translation does end on newline.
*
* @since 1.0.0
* @access public
*
* @param string $original The source string.
* @param string $translation The translation.
* @param GP_Locale $locale The locale of the translation.
* @return string|true True if check is OK, otherwise warning message.
*/
public function warning_should_end_on_newline( $original, $translation, $locale ) {
if ( gp_endswith( $original, "\n" ) && ! gp_endswith( $translation, "\n" ) ) {
return __( 'Original and translation should both end on newline.', 'glotpress' );
Expand All @@ -204,6 +358,17 @@ public function warning_should_end_on_newline( $original, $translation, $locale
return true;
}

/**
* Checks whether a translation doesn't end on newline.
*
* @since 1.0.0
* @access public
*
* @param string $original The source string.
* @param string $translation The translation.
* @param GP_Locale $locale The locale of the translation.
* @return string|true True if check is OK, otherwise warning message.
*/
public function warning_should_not_end_on_newline( $original, $translation, $locale ) {
if ( ! gp_endswith( $original, "\n" ) && gp_endswith( $translation, "\n" ) ) {
return __( 'Translation should not end on newline.', 'glotpress' );
Expand All @@ -213,9 +378,11 @@ public function warning_should_not_end_on_newline( $original, $translation, $loc
}

/**
* Adds all methods starting with warning_ to $translation_warnings
* Registers all methods starting with `warning_` as built-in warnings.
*
* @param GP_Translation_Warnings $translation_warnings Instance of GP_Translation_Warnings.
*/
public function add_all( &$translation_warnings ) {
public function add_all( $translation_warnings ) {
$warnings = array_filter( get_class_methods( get_class( $this ) ), function ( $key ) {
return gp_startswith( $key, 'warning_' );
} );
Expand All @@ -224,5 +391,4 @@ public function add_all( &$translation_warnings ) {
$translation_warnings->add( str_replace( 'warning_', '', $warning ), array( $this, $warning ) );
}
}

}

0 comments on commit 68e11be

Please sign in to comment.