Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate: Add additional custom errors #320

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ class WPorg_GP_Custom_Translation_Errors {
*/
const WORPRESS_CORE_PROJECT_ID = 1;

/**
* The parent WordPress core project ID.
*
* @var int
*/
const CONTEXT_ENUM_CHECK_LIST = [
'text direction' => [ 'ltr', 'rtl' ],
'Open Sans font: on or off' => [ 'on', 'off' ],
'Open Sans font: add new subset (greek, cyrillic, vietnamese)' => [ 'no-subset', 'greek', 'cyrillic', 'vietnamese' ],
'Word count type. Do not translate!' => [ 'characters_excluding_spaces', 'characters_including_spaces', 'words' ],
'decline months names: on or off' => [ 'on', 'off' ],
'Comment number declension: on or off' => [ 'on', 'off' ],
];
Comment on lines +23 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These in general follow a specific format, I wonder if we could add something more generic.

  • Word: ... (option1, option2) = must be option1 or option2
  • Word: option1 or option2 = Must be option1 or option2
  • Do not translate in the comment, and neither of the above = Triple check please?

Wouldn't cover 100% of cases, but would help with such strings in the future.


/**
* Registers all methods starting with error_ with GlotPress.
*/
Expand All @@ -39,8 +53,11 @@ function ( $key ) {
*/
public function is_core_project( GP_Original $gp_original ): bool {
$project = GP::$project->get( $gp_original->project_id );
$project = GP::$project->get( $project->parent_project_id );
if ( self::WORPRESS_CORE_PROJECT_ID == $project->parent_project_id ) {
return true;
}

$project = GP::$project->get( $project->parent_project_id );
if ( self::WORPRESS_CORE_PROJECT_ID == $project->parent_project_id ) {
return true;
} else {
Expand Down Expand Up @@ -169,10 +186,10 @@ public function error_timezone_date_format( $original, $translation, $gp_origina
if ( ! $this->is_core_project( $gp_original ) ) {
return true;
}
if ( is_null( $gp_original->context ) ) {
if ( is_null( $gp_original->comment ) ) {
return true;
}
if ( ! str_contains( $gp_original->context, 'timezone date format' ) ) {
if ( strpos( $gp_original->comment, 'https://www.php.net/manual/datetime.format.php' ) === false ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can keep using str_contains() as WordPress has a polyfill for it. Unless you want to do a case-insensitive check then this should be a stripos.

return true;
}

Expand All @@ -186,7 +203,81 @@ public function error_timezone_date_format( $original, $translation, $gp_origina
return esc_html__( 'The translation has empty spaces, new lines or another similar elements.', 'glotpress' );
}

return esc_html__( 'Must be a valid timezone date format.', 'glotpress' );
return esc_html__( 'Must be a valid date format.', 'glotpress' );
}

/**
* Adds an error for text direction.
*
* @param string $original The original string.
* @param string $translation The translated string.
* @param GP_Original $gp_original The GP_original object.
* @param GP_Locale $locale The locale.
* @return string|true The error message or true if no error.
*/
public function error_check_enum_list( $original, $translation, $gp_original, $locale ) {
if ( ! $this->is_core_project( $gp_original ) ) {
return true;
}
if ( is_null( $gp_original->context ) ) {
return true;
}
if ( !isset(self::CONTEXT_ENUM_CHECK_LIST[$gp_original->context])) {
return true;
}
if ( in_array( $translation, self::CONTEXT_ENUM_CHECK_LIST[$gp_original->context]) ) {
return true;
}

return esc_html__(
'Must be ' . $this->array_to_string( self::CONTEXT_ENUM_CHECK_LIST[$gp_original->context] ),
'glotpress'
);
Comment on lines +232 to +235
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a literal string, given it's being translated:

Suggested change
return esc_html__(
'Must be ' . $this->array_to_string( self::CONTEXT_ENUM_CHECK_LIST[$gp_original->context] ),
'glotpress'
);
return sptintnt(
esc_html__(
'Must be one of %s',
'glotpress'
),
$this->array_to_string( self::CONTEXT_ENUM_CHECK_LIST[$gp_original->context] )
);

}

/**
* Adds an error for text direction.
*
* @param string $original The original string.
* @param string $translation The translated string.
* @param GP_Original $gp_original The GP_original object.
* @param GP_Locale $locale The locale.
* @return string|true The error message or true if no error.
*/
public function error_check_integer( $original, $translation, $gp_original, $locale ) {
if ( ! $this->is_core_project( $gp_original ) ) {
return true;
}
if ( is_null( $gp_original->context ) ) {
return true;
}
if (
! in_array($gp_original->context, [
'draft_length',
'minimum input length for searching post links',
'comment_excerpt_length',
'excerpt_length',
])
) {
return true;
}

if ( absint( $translation ) == $translation ) {
return true;
}

return esc_html__( 'The translation has to be a number.', 'glotpress' );
}

private function array_to_string( $array ) {
$str = implode('’, ’', $array);
$lastCommaPos = strrpos($str, ',');

if ($lastCommaPos !== false) {
$str = substr_replace($str, ' or', $lastCommaPos, 1);
}

return '’' . $str . '’';
}
}

Expand All @@ -196,7 +287,7 @@ public function error_timezone_date_format( $original, $translation, $gp_origina
* @return WPorg_GP_Custom_Translation_Errors
*/
function wporg_gp_custom_translation_errors() {
global $wporg_gp_custom_translation_errors;
global $wporg_gp_custom_translation_errors;

if ( ! isset( $wporg_gp_custom_translation_errors ) ) {
$wporg_gp_custom_translation_errors = new WPorg_GP_Custom_Translation_Errors();
Expand Down