Skip to content

Commit

Permalink
Use gettext for plugin translation
Browse files Browse the repository at this point in the history
  • Loading branch information
rimas-kudelis committed Dec 21, 2024
1 parent ef2c647 commit d04e9aa
Show file tree
Hide file tree
Showing 63 changed files with 4,839 additions and 310 deletions.
44 changes: 10 additions & 34 deletions altcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,12 @@
define('ALTCHA_VERSION', '1.14.1');
define('ALTCHA_WEBSITE', 'https://altcha.org/');
define('ALTCHA_WIDGET_VERSION', '1.0.0');
define('ALTCHA_LANGUAGES', [
"auto" => "Auto",
"bg" => "Bulgarian",
"ca" => "Catalan",
"cs" => "Czech",
"da" => "Danish",
"de" => "German",
"el" => "Greek",
"en" => "English",
"es" => "Spanish",
"et" => "Estonian",
"fi" => "Finnish",
"fr" => "French",
"hr" => "Croatian",
"hu" => "Hungarian",
"it" => "Italian",
"ja" => "Japanese",
"lt" => "Lithuanian",
"lv" => "Latvian",
"nl" => "Dutch",
"no" => "Norwegian",
"pl" => "Polish",
"pt" => "Portuguese",
"ro" => "Romanian",
"ru" => "Russian",
"sk" => "Slovak",
"sr" => "Serbian",
"sv" => "Swedish",
"tr" => "Turkish",
"uk" => "Ukrainian",
"zh-CN" => "Chinese (simplified)",
]);

// required for is_plugin_active
require_once ABSPATH . 'wp-admin/includes/plugin.php';

require plugin_dir_path(__FILE__) . 'includes/helpers.php';
require plugin_dir_path(__FILE__) . 'includes/core.php';
require plugin_dir_path(__FILE__) . 'includes/translations.php';
require plugin_dir_path( __FILE__ ) . './public/widget.php';

require plugin_dir_path( __FILE__ ) . './integrations/contact-form-7.php';
Expand All @@ -82,6 +49,8 @@
register_activation_hook(__FILE__, 'altcha_activate');
register_deactivation_hook(__FILE__, 'altcha_deactivate');

add_action('init', 'altcha_init');

add_action('wp_enqueue_scripts', 'altcha_enqueue_widget_scripts');

add_shortcode(
Expand All @@ -97,13 +66,20 @@ function ($attrs) {
}
);

function altcha_init() {
load_plugin_textdomain(
'altcha-spam-protection',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
}

function altcha_activate()
{
update_option(AltchaPlugin::$option_api, 'selfhosted');
update_option(AltchaPlugin::$option_api_key, '');
update_option(AltchaPlugin::$option_expires, '3600');
update_option(AltchaPlugin::$option_secret, AltchaPlugin::$instance->random_secret());
update_option(AltchaPlugin::$option_language, 'auto');
update_option(AltchaPlugin::$option_hidefooter, true);
update_option(AltchaPlugin::$option_send_ip, true);
update_option(AltchaPlugin::$option_integration_custom, 'captcha');
Expand Down
41 changes: 24 additions & 17 deletions includes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class AltchaPlugin

public static $option_delay = "altcha_delay";

public static $option_language = "altcha_language";

public static $option_hidefooter = "altcha_hidefooter";

public static $option_hidelogo = "altcha_hidelogo";
Expand Down Expand Up @@ -143,11 +141,6 @@ public function get_expires()
return get_option(AltchaPlugin::$option_expires);
}

public function get_language()
{
return trim(get_option(AltchaPlugin::$option_language));
}

public function get_secret()
{
return trim(get_option(AltchaPlugin::$option_secret));
Expand Down Expand Up @@ -286,18 +279,32 @@ public function get_challengeurl()

public function get_translations($language = null)
{
if ($language === null) {
$language = $this->get_language();
$originalLanguage = null;

if ($language !== null) {
$originalLanguage = get_locale();
switch_to_locale($language);
}
if ($language === "auto") {
// Get the current locale
$language = get_locale();
if (!isset(ALTCHA_TRANSLATIONS[$language])) {
// Wordpress uses full codes such as `fr_FR`; if no translation is found, try short version `fr`
$language = substr($language, 0, 2);
}

$ALTCHA_WEBSITE = constant('ALTCHA_WEBSITE');
$translations = array(
"error" => __("Verification failed. Try again later.", "altcha-spam-protection"),
"footer" => sprintf(
__("Protected by %sALTCHA%s", "altcha-spam-protection"),
"<a href=\"$ALTCHA_WEBSITE\" target=\"_blank\">",
"</a>",
),
"label" => __("I'm not a robot", "altcha-spam-protection"),
"verified" => __("Verified", "altcha-spam-protection"),
"verifying" => __("Verifying...", "altcha-spam-protection"),
"waitAlert" => __("Verifying... please wait.", "altcha-spam-protection"),
);

if ($originalLanguage !== null) {
switch_to_locale($originalLanguage);
}
return ALTCHA_TRANSLATIONS[$language] ?: ALTCHA_TRANSLATIONS["en"];

return $translations;
}


Expand Down
18 changes: 0 additions & 18 deletions includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ function altcha_settings_init()
AltchaPlugin::$option_expires
);

register_setting(
'altcha_options',
AltchaPlugin::$option_language
);

register_setting(
'altcha_options',
AltchaPlugin::$option_hidefooter
Expand Down Expand Up @@ -279,19 +274,6 @@ function altcha_settings_init()
'altcha_admin'
);

add_settings_field(
'altcha_settings_language_field',
__('Language', 'altcha-spam-protection'),
'altcha_settings_select_callback',
'altcha_admin',
'altcha_widget_settings_section',
array(
"name" => AltchaPlugin::$option_language,
"hint" => "Select the language of the verification widget.",
"options" => ALTCHA_LANGUAGES
)
);

add_settings_field(
'altcha_settings_auto_field',
__('Auto verification', 'altcha-spam-protection'),
Expand Down
Loading

0 comments on commit d04e9aa

Please sign in to comment.