summaryrefslogtreecommitdiffstats
path: root/includes/class-cepis-woo-messages.php
blob: 93d327959d28c39e2a288ba493e76a9dd00971bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
 * The strings the shopper sees while a CEP is looked up.
 *
 * @package CepisWooCommerce
 */

declare(strict_types=1);

defined( 'ABSPATH' ) || exit;

/**
 * The widget ships its own Portuguese messages; these replace them so that a
 * store running in another locale gets the checkout in that locale, and so a
 * store owner can reword them with the usual translation tooling.
 *
 * Keys are the widget's status kinds and error codes. Anything not listed
 * keeps the widget's own wording.
 *
 * `not_found_range` and `not_found_city` are the exception: they are not
 * status kinds the widget knows, but templates the plugin's own JavaScript
 * fills in when the API says which municipality a missing CEP would have
 * belonged to. They live in the same table so that one filter, one text domain
 * and one `.po` file cover everything the shopper is told.
 */
final class Cepis_Woo_Messages {

	/**
	 * Build the message table.
	 *
	 * Must be called at or after `init`: it is the first thing in the plugin
	 * that translates.
	 *
	 * @return array<string, string> Message per status kind or error code.
	 */
	public static function all(): array {
		$messages = array(
			'idle'             => '',
			'loading'          => __( 'Looking the address up…', 'cepis-woocommerce' ),
			'found'            => __( 'Address found.', 'cepis-woocommerce' ),
			'not_found'        => __( 'CEP not found. Check the number or fill the address in by hand.', 'cepis-woocommerce' ),
			/* translators: 1: municipality, e.g. Areal. 2: state code, e.g. RJ. 3: lowest CEP that exists there, e.g. 25845-001. 4: highest CEP that exists there, e.g. 25849-899. */
			'not_found_range'  => __( 'CEP not found. This number would be in %1$s/%2$s, which has CEPs from %3$s to %4$s. Check the number.', 'cepis-woocommerce' ),
			/* translators: 1: municipality, e.g. Areal. 2: state code, e.g. RJ. */
			'not_found_city'   => __( 'CEP not found. This number would be in %1$s/%2$s. Check the number.', 'cepis-woocommerce' ),
			'invalid'          => __( 'Invalid CEP. Type all 8 digits.', 'cepis-woocommerce' ),
			'rate_limited'     => __( 'Too many lookups in a short time. Wait a few seconds and try again.', 'cepis-woocommerce' ),
			'server'           => __( 'The CEP service is unavailable. Fill the address in by hand.', 'cepis-woocommerce' ),
			'network'          => __( 'Could not look the CEP up. Check your connection and try again.', 'cepis-woocommerce' ),
			'timeout'          => __( 'The CEP lookup took too long. Try again or fill the address in by hand.', 'cepis-woocommerce' ),
			'invalid_response' => __( 'Unexpected answer from the CEP service. Fill the address in by hand.', 'cepis-woocommerce' ),
		);

		/**
		 * Filters the messages shown at the checkout while a CEP is resolved.
		 *
		 * @param array<string, string> $messages Message per status kind or error code.
		 */
		$messages = (array) apply_filters( 'cepis_woocommerce_messages', $messages );

		$clean = array();
		foreach ( $messages as $key => $message ) {
			if ( is_string( $key ) && is_scalar( $message ) ) {
				$clean[ sanitize_key( $key ) ] = wp_strip_all_tags( (string) $message );
			}
		}

		return $clean;
	}
}