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 15, 2024
1 parent ef2c647 commit c4ba485
Show file tree
Hide file tree
Showing 62 changed files with 5,103 additions and 272 deletions.
51 changes: 30 additions & 21 deletions altcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@
define('ALTCHA_WIDGET_VERSION', '1.0.0');
define('ALTCHA_LANGUAGES', [
"auto" => "Auto",
"bg" => "Bulgarian",
"bg_BG" => "Bulgarian",
"ca" => "Catalan",
"cs" => "Czech",
"da" => "Danish",
"de" => "German",
"cs_CZ" => "Czech",
"da_DK" => "Danish",
"de_DE" => "German",
"el" => "Greek",
"en" => "English",
"es" => "Spanish",
"en_US" => "English",
"es_ES" => "Spanish",
"et" => "Estonian",
"fi" => "Finnish",
"fr" => "French",
"fr_FR" => "French",
"hr" => "Croatian",
"hu" => "Hungarian",
"it" => "Italian",
"hu_HU" => "Hungarian",
"it_IT" => "Italian",
"ja" => "Japanese",
"lt" => "Lithuanian",
"lt_LT" => "Lithuanian",
"lv" => "Latvian",
"nl" => "Dutch",
"no" => "Norwegian",
"pl" => "Polish",
"pt" => "Portuguese",
"ro" => "Romanian",
"ru" => "Russian",
"sk" => "Slovak",
"sr" => "Serbian",
"sv" => "Swedish",
"tr" => "Turkish",
"nl_NL" => "Dutch",
"nn_NO" => "Norwegian",
"pl_PL" => "Polish",
"pt_PT" => "Portuguese",
"ro_RO" => "Romanian",
"ru_RU" => "Russian",
"sk_SK" => "Slovak",
"sr_RS" => "Serbian",
"sv_SE" => "Swedish",
"tr_TR" => "Turkish",
"uk" => "Ukrainian",
"zh-CN" => "Chinese (simplified)",
]);
Expand All @@ -57,7 +57,6 @@

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 +81,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,6 +98,14 @@ 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');
Expand Down
38 changes: 28 additions & 10 deletions includes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,36 @@ public function get_challengeurl()

public function get_translations($language = null)
{
$originalLanguage = null;

if ($language === null) {
$language = $this->get_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);
}
$language = $this->get_language();
}
if ($language !== "auto") {
$originalLocale = get_locale();
switch_to_locale($language);
}
return ALTCHA_TRANSLATIONS[$language] ?: ALTCHA_TRANSLATIONS["en"];

$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"),
"cannot_submit" => __("Cannot submit.", "altcha-spam-protection"),
);

if ($originalLanguage !== null) {
switch_to_locale($originalLocale);
}

return $translations;
}


Expand Down
241 changes: 0 additions & 241 deletions includes/translations.php

This file was deleted.

Binary file added languages/altcha-spam-protection-bg_BG.mo
Binary file not shown.
Loading

0 comments on commit c4ba485

Please sign in to comment.