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

Use gettext for plugin translation #34

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
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
91 changes: 75 additions & 16 deletions admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ function altcha_options_page_html()
</div>

<div style="flex-grow: 1;">
<div class="altcha-title">ALTCHA</div>
<div class="altcha-subtitle">A Privacy-Friendly Captcha Alternative.</div>
<div class="altcha-title"><?php echo esc_html__('ALTCHA', 'altcha-spam-protection'); ?></div>
<div class="altcha-subtitle"><?php echo esc_html__('A Privacy-Friendly Captcha Alternative.', 'altcha-spam-protection'); ?></div>
</div>

<div>
<div style="margin-bottom: 0.3rem;"><b>Do you like ALTCHA?</b></div>
<div style="margin-bottom: 0.3rem;"><b><?php echo esc_html__('Do you like ALTCHA?', 'altcha-spam-protection'); ?></b></div>
<div style="display:flex;gap: 0.5rem;">
<a href="https://wordpress.org/support/plugin/altcha-spam-protection/reviews/?filter=5#new-post" target="_blank" style="display: inline-flex; gap: 0.5rem;">
<span style="display: inline-flex; gap: 0.1rem;">
Expand All @@ -45,7 +45,7 @@ function altcha_options_page_html()
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#FFCC00" width="18" height="18"><path d="M12.0006 18.26L4.94715 22.2082L6.52248 14.2799L0.587891 8.7918L8.61493 7.84006L12.0006 0.5L15.3862 7.84006L23.4132 8.7918L17.4787 14.2799L19.054 22.2082L12.0006 18.26Z"></path></svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#FFCC00" width="18" height="18"><path d="M12.0006 18.26L4.94715 22.2082L6.52248 14.2799L0.587891 8.7918L8.61493 7.84006L12.0006 0.5L15.3862 7.84006L23.4132 8.7918L17.4787 14.2799L19.054 22.2082L12.0006 18.26Z"></path></svg>
</span>
<span>Review it!</span>
<span><?php echo esc_html__('Review it!', 'altcha-spam-protection'); ?></span>
</a>
</div>
</div>
Expand All @@ -65,13 +65,32 @@ function altcha_options_page_html()
</form>

<div style="opacity: 0.8;">
<p>ALTCHA Spam Protection for WordPress, plugin version <?php echo esc_html(AltchaPlugin::$version) ?>, widget version <?php echo esc_html(AltchaPlugin::$widget_version) ?></p>
<p><?php
echo esc_html(sprintf(
__(
/* translators: %1$s is the plugin version, and %2$s is the widget version */
'ALTCHA Spam Protection for WordPress, plugin version %1$s, widget version %2$s',
'altcha-spam-protection',
),
AltchaPlugin::$version,
AltchaPlugin::$widget_version,
));
?></p>
<p>
Please give ALTCHA a <a href="https://wordpress.org/support/plugin/altcha-spam-protection/reviews/?filter=5#new-post" target="_blank">★★★★★ rating</a> on WordPress.org to help us get the word out.
<?php
echo esc_html(sprintf(
__(
/* translators: the placeholders are opening and closing tags for a link (<a> tag) */
'Please give ALTCHA a %s★★★★★ rating%s on WordPress.org to help us get the word out.',
'altcha-spam-protection',
),
'<a href="https://wordpress.org/support/plugin/altcha-spam-protection/reviews/?filter=5#new-post" target="_blank">',
'</a>',
)); ?>
</p>
<p>
<a href="https://github.com/altcha-org/altcha" target="_blank" style="display: inline-flex; gap: 0.3rem;">
<span>Star ALTCHA on GitHub!</span>
<span><?php echo esc_html__('Star ALTCHA on GitHub!', 'altcha-spam-protection'); ?></span>
</a>
</p>
</div>
Expand All @@ -82,23 +101,63 @@ function altcha_options_page_html()
function altcha_general_section_callback()
{
?>
<p>The <b>Self-hosted</b> mode does not require an API Key and runs fully within your WordPress installation, without any external services.</p>

<p>To access the ALTCHA's cloud API, you need an API Key. Visit <a href="https://altcha.org" target="_blank">altcha.org</a> for more information.</p>
<p><?php
echo sprintf(
esc_html__(
/* translators: the placeholders are opening and closing tags for bold */
'The %sSelf-hosted%s mode does not require an API Key and runs fully within your WordPress installation, without any external services.',
'altcha-spam-protection',
),
'<b>',
'</b>',
);
?></p>

<p><?php
echo sprintf(
esc_html__(
/* translators: the placeholder is a clickable link to altcha.org */
'To access the ALTCHA\'s cloud API, you need an API Key. Visit %s for more information.',
'altcha-spam-protection',
),
'<a href="https://altcha.org" target="_blank">altcha.org</a>',
);
?></p>

<div>
<a href="https://altcha.org/docs/api/api_keys/" target="_blank" class="button button-primary">Create an API Key &rarr;</a>
<a href="https://altcha.org/docs/api/api_keys/" target="_blank" class="button button-primary"><?php echo esc_html__('Create an API Key →', 'altcha-spam-protection'); ?></a>
</div>

<p>Your domain name for the API Key: <b><?php echo esc_html(AltchaPlugin::$hostname); ?></b></p>
<p><?php
echo sprintf(
esc_html__(
/* translators: the placeholder will be replaced with the domain name */
'Your domain name for the API Key: %s',
'altcha_spam_protection',
),
'<b>' . esc_html(AltchaPlugin::$hostname) . '</b>',
);
?></p>
<?php
}

function altcha_spam_filter_section_callback()
{
?>

<p>The <a href="https://altcha.org/docs/api/spam-filter-api" target="_blank">Spam Filter</a> is <b>available only in the API mode</b> with a valid API Key.</p>
<p><?php
echo sprintf(
esc_html__(
/* translators: the first two placeholders will be replaced with opening and closing tags for a link (<a> tag), the other two with opening and closing tags for bold (<b> tag). The two pairs may be swapped with each other, but the two tags within pairs may not. */
'The %1$sSpam Filter%2$s is %3$savailable only in the API mode%4$s with a valid API Key.',
'altcha-spam-protection',
),
'<a href="https://altcha.org/docs/api/spam-filter-api" target="_blank">',
'</a>',
'<b>',
'</b>',
);
?></p>

<?php
}
Expand All @@ -107,7 +166,7 @@ function altcha_widget_section_callback()
{
?>

<p>Customize the widget to fit your needs.</p>
<p><?php echo esc_html__('Customize the widget to fit your needs.', 'altcha-spam-protection'); ?></p>

<?php
}
Expand All @@ -116,7 +175,7 @@ function altcha_integrations_section_callback()
{
?>

<p>Activate ALTCHA for these integrations:</p>
<p><?php echo esc_html__('Activate ALTCHA for these integrations:', 'altcha-spam-protection'); ?></p>

<?php
}
Expand All @@ -125,7 +184,7 @@ function altcha_wordpress_section_callback()
{
?>

<p>Activate ALTCHA for the core Wordpress functionality:</p>
<p><?php echo esc_html__('Activate ALTCHA for the core Wordpress functionality:', 'altcha-spam-protection'); ?></p>

<?php
}
Expand Down
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
4 changes: 2 additions & 2 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
function altcha_options_page()
{
add_options_page(
'ALTCHA Spam Protection',
'ALTCHA Anti-spam',
__('ALTCHA Spam Protection', 'altcha-spam-protection'),
__('ALTCHA Anti-spam', 'altcha-spam-protection'),
'manage_options',
'altcha_admin',
'altcha_options_page_html',
Expand Down
42 changes: 25 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,33 @@ 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(
/* translators: the placeholders contain opening and closing tags for a link (<a> tag) */
__('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
Loading