Blocks
4 months ago
Contracts
9 months ago
Errors
1 month ago
Scripts
5 days ago
Arrays.php
3 years ago
ColorService.php
3 years ago
Currency.php
1 year ago
Encryption.php
3 years ago
GitHubInstaller.php
5 days ago
Interval.php
7 months ago
Server.php
2 years ago
TimeDate.php
3 months ago
Translations.php
3 years ago
URL.php
2 years ago
UtilityService.php
3 years ago
UtilityServiceProvider.php
3 years ago
kses.json
5 days ago
Interval.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Support; |
| 4 | |
| 5 | /** |
| 6 | * Handles interval translations |
| 7 | */ |
| 8 | class Interval { |
| 9 | /** |
| 10 | * Translate the interval. |
| 11 | * |
| 12 | * @param int $amount The interval amount. |
| 13 | * @param string $interval The interval type (e.g., "day", "week"). |
| 14 | * @param string $prefix (optional) The prefix to use before the interval. Default: "every". |
| 15 | * @param string $fallback (optional) The fallback text for non-standard intervals. Default: "once". |
| 16 | * @param bool $showSingle (optional) Whether to use singular forms for amounts (e.g., "1 day" instead of "1 days"). Default: false. |
| 17 | * @return string The translated interval string. |
| 18 | */ |
| 19 | public static function translate( |
| 20 | int $amount, |
| 21 | string $interval, |
| 22 | string $prefix = 'every', |
| 23 | string $fallback = 'once', |
| 24 | bool $showSingle = false |
| 25 | ) { |
| 26 | switch ( $interval ) { |
| 27 | case 'day': |
| 28 | $text = sprintf( |
| 29 | // translators: %d is the number of days. |
| 30 | $showSingle ? _n( '%d day', '%d days', $amount, 'surecart' ) : _n( '%d day', '%d days', $amount, 'surecart' ), |
| 31 | $amount |
| 32 | ); |
| 33 | return "$prefix $text"; |
| 34 | case 'week': |
| 35 | $text = sprintf( |
| 36 | // translators: %d is the number of weeks. |
| 37 | $showSingle ? _n( '%d week', '%d weeks', $amount, 'surecart' ) : _n( '%d week', '%d weeks', $amount, 'surecart' ), |
| 38 | $amount |
| 39 | ); |
| 40 | return "$prefix $text"; |
| 41 | case 'month': |
| 42 | $text = sprintf( |
| 43 | // translators: %d is the number of months. |
| 44 | $showSingle ? _n( '%d month', '%d months', $amount, 'surecart' ) : _n( '%d month', '%d months', $amount, 'surecart' ), |
| 45 | $amount |
| 46 | ); |
| 47 | return "$prefix $text"; |
| 48 | case 'year': |
| 49 | $text = sprintf( |
| 50 | // translators: %d is the number of years. |
| 51 | $showSingle ? _n( '%d year', '%d years', $amount, 'surecart' ) : _n( '%d year', '%d years', $amount, 'surecart' ), |
| 52 | $amount |
| 53 | ); |
| 54 | return "$prefix $text"; |
| 55 | default: |
| 56 | return $fallback; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Translate abbreviated interval. |
| 62 | * |
| 63 | * @param int $amount The interval amount. |
| 64 | * @param string $interval The interval type (e.g., "day", "week"). |
| 65 | * @param string $fallback (optional) The fallback text for non-standard intervals. Default: "once". |
| 66 | * @param bool $showSingle (optional) Whether to use singular forms for amounts (e.g., "1 day" instead of "1 days"). Default: false. |
| 67 | * @return string The translated and abbreviated interval string. |
| 68 | */ |
| 69 | function translateAbbreviatedInterval( |
| 70 | int $amount, |
| 71 | string $interval, |
| 72 | string $fallback = 'once', |
| 73 | bool $showSingle = false |
| 74 | ) { |
| 75 | switch ( $interval ) { |
| 76 | case 'day': |
| 77 | $text = sprintf( |
| 78 | // translators: %d is the number of days (abbreviated). |
| 79 | $showSingle ? _n( '%d d', '%d d', $amount, 'surecart' ) : _n( '%d d', '%d d', $amount, 'surecart' ), |
| 80 | $amount |
| 81 | ); |
| 82 | return " / $text"; |
| 83 | case 'week': |
| 84 | $text = sprintf( |
| 85 | // translators: %d is the number of weeks (abbreviated). |
| 86 | $showSingle ? _n( '%d wk', '%d wks', $amount, 'surecart' ) : _n( '%d wk', '%d wks', $amount, 'surecart' ), |
| 87 | $amount |
| 88 | ); |
| 89 | return " / $text"; |
| 90 | case 'month': |
| 91 | $text = sprintf( |
| 92 | // translators: %d is the number of months (abbreviated). |
| 93 | $showSingle ? _n( '%d mo', '%d mos', $amount, 'surecart' ) : _n( '%d mo', '%d mos', $amount, 'surecart' ), |
| 94 | $amount |
| 95 | ); |
| 96 | return " / $text"; |
| 97 | case 'year': |
| 98 | $text = sprintf( |
| 99 | // translators: %d is the number of years (abbreviated). |
| 100 | $showSingle ? _n( '%d yr', '%d yrs', $amount, 'surecart' ) : _n( '%d yr', '%d yrs', $amount, 'surecart' ), |
| 101 | $amount |
| 102 | ); |
| 103 | return " / $text"; |
| 104 | default: |
| 105 | return $fallback; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 |