Sorry, we don't support your browser.  Install a modern browser

Remote language reset throigh API call#266

I would like to be able to do a remote language reset since my website is running out of translations very quickly (2-3 times a day) eventhough I have 5 million words credit.

I would like to be able to use code like this:

// Add a custom 10-minute interval to WordPress cron schedules
add_filter(‘cron_schedules’, function($schedules) $schedules[‘every_10_minutes’] = [
‘interval’ => 10 * MINUTE_IN_SECONDS, // 10 minutes in seconds
‘display’ => __(‘Every 10 Minutes’),
];
return $schedules;
});

// Schedule the Weglot word count check if not already scheduled
add_action(‘wp’, function() if (!wp_next_scheduled(‘weglot_word_count_check_event’)) wp_schedule_event(time(), ‘every_10_minutes’, ‘weglot_word_count_check_event’);
}
});

// Define the word count check and reset logic
add_action(‘weglot_word_count_check_event’, function() // Your Weglot API key
$api_key = ‘mykey’;

// Step 1: Check translated word count
$word_count_response = wp_remote_get(“https://api.weglot.com/usage?api_key=$api_key", [
‘headers’ => [
‘Content-Type’ => ‘application/json’,
],
]);

if (is_wp_error($word_count_response)) error_log(‘Weglot API Error (Word Count): ‘ . $word_count_response->get_error_message());
return;
}

$word_count_data = json_decode(wp_remote_retrieve_body($word_count_response), true);
$translated_words = isset($word_count_data[‘translated_words’]) ? $word_count_data[‘translated_words’] : 0;

// Step 2: If word count exceeds 4,990,000 (5 million - 10,000), delete translations
if ($translated_words >= 4990000) // Get the list of languages in your project
$languages_response = wp_remote_get(“https://api.weglot.com/languages?api_key=$api_key", [
‘headers’ => [
‘Content-Type’ => ‘application/json’,
],
]);

if (is_wp_error($languages_response)) {
  error_log('Weglot API Error (Languages): ' . $languages_response->get_error_message());
  return;
}

$languages_data = json_decode(wp_remote_retrieve_body($languages_response), true);
$languages = isset($languages_data['languages']) ? $languages_data['languages'] : [];

// Step 3: Delete translations for each language
foreach ($languages as $language) {
  $language_code = $language['language_to'];
  $delete_response = wp_remote_request("https://api.weglot.com/translations/$language_code?api_key=$api_key", [
    'method' => 'DELETE',
    'headers' => [
      'Content-Type' => 'application/json',
    ],
  ]);

  if (is_wp_error($delete_response)) {
    error_log("Weglot API Error (Delete $language_code): " . $delete_response->get_error_message());
  }
}

// Step 4: Remove and re-add specific languages (French, Spanish, Portuguese, German, Polish, English)
$languages_to_reset = ['fr', 'es', 'pt', 'de', 'pl', 'en'];

foreach ($languages_to_reset as $lang_code) {
  // Remove the language
  $remove_response = wp_remote_request("https://api.weglot.com/languages/$lang_code?api_key=$api_key", [
    'method' => 'DELETE',
    'headers' => [
      'Content-Type' => 'application/json',
    ],
  ]);

  if (is_wp_error($remove_response)) {
    error_log("Weglot API Error (Remove $lang_code): " . $remove_response->get_error_message());
    continue;
  }

  // Re-add the language
  $add_response = wp_remote_post("https://api.weglot.com/languages?api_key=$api_key", [
    'headers' => [
      'Content-Type' => 'application/json',
    ],
    'body' => json_encode([
      'language_to' => $lang_code,
    ]),
  ]);

  if (is_wp_error($add_response)) {
    error_log("Weglot API Error (Add $lang_code): " . $add_response->get_error_message());
  }
}

// Step 5: Refresh Weglot to apply changes
wp_remote_get("https://api.weglot.com/refresh?api_key=$api_key");

}
});

a year ago

Hi Martin, thanks for your message. Could you contact out support team for this request at support@weglot.com ?
Thanks.

a year ago