laratext 0.1.0

Laratext for Laravel Rust: translation keys with their source text, and auto-translation with OpenAI, Claude, Google Translate or Translatorio.
Documentation
//! Plural rules, generated from `Illuminate\Translation\MessageSelector::getPluralIndex()` of Laravel 13.44.1.
//!
//! The same table as `resources/js/plural.js`, so the server and the browser pick the same form.

/// The index of the plural form a number takes in a locale.
///
/// A locale without a rule always takes the first form, as in Laravel. The number is read by its
/// absolute value.
pub fn plural_index(locale: &str, number: f64) -> usize {
    let n = number.abs();
    let t = n.trunc();

    match rule(&locale.replace('-', "_")) {
        Some(0) => 0,
        Some(1) => {
            if n == 1.0 {
                0
            } else {
                1
            }
        }
        Some(2) => {
            if n == 0.0 || n == 1.0 {
                0
            } else {
                1
            }
        }
        Some(3) => {
            if t % 10.0 == 1.0 && t % 100.0 != 11.0 {
                0
            } else if t % 10.0 >= 2.0 && t % 10.0 <= 4.0 && (t % 100.0 < 10.0 || t % 100.0 >= 20.0) {
                1
            } else {
                2
            }
        }
        Some(4) => {
            if n == 1.0 {
                0
            } else if (2.0..=4.0).contains(&n) {
                1
            } else {
                2
            }
        }
        Some(5) => {
            if n == 1.0 {
                0
            } else if n == 2.0 {
                1
            } else {
                2
            }
        }
        Some(6) => {
            if t % 10.0 == 1.0 && t % 100.0 != 11.0 {
                0
            } else if t % 10.0 >= 2.0 && (t % 100.0 < 10.0 || t % 100.0 >= 20.0) {
                1
            } else {
                2
            }
        }
        Some(7) => {
            if t % 100.0 == 1.0 {
                0
            } else if t % 100.0 == 2.0 {
                1
            } else if t % 100.0 == 3.0 || t % 100.0 == 4.0 {
                2
            } else {
                3
            }
        }
        Some(8) => {
            if t % 10.0 == 1.0 {
                0
            } else {
                1
            }
        }
        Some(9) => {
            if n == 1.0 {
                0
            } else if n == 0.0 || (t % 100.0 > 1.0 && t % 100.0 < 11.0) {
                1
            } else if t % 100.0 > 10.0 && t % 100.0 < 20.0 {
                2
            } else {
                3
            }
        }
        Some(10) => {
            if n == 0.0 {
                0
            } else if t % 10.0 == 1.0 && t % 100.0 != 11.0 {
                1
            } else {
                2
            }
        }
        Some(11) => {
            if n == 1.0 {
                0
            } else if t % 10.0 >= 2.0 && t % 10.0 <= 4.0 && (t % 100.0 < 12.0 || t % 100.0 > 14.0) {
                1
            } else {
                2
            }
        }
        Some(12) => {
            if n == 1.0 {
                0
            } else if n == 2.0 {
                1
            } else if n == 8.0 || n == 11.0 {
                2
            } else {
                3
            }
        }
        Some(13) => {
            if n == 1.0 {
                0
            } else if n == 0.0 || (t % 100.0 > 0.0 && t % 100.0 < 20.0) {
                1
            } else {
                2
            }
        }
        Some(14) => {
            if n == 0.0 {
                0
            } else if n == 1.0 {
                1
            } else if n == 2.0 {
                2
            } else if t % 100.0 >= 3.0 && t % 100.0 <= 10.0 {
                3
            } else if t % 100.0 >= 11.0 && t % 100.0 <= 99.0 {
                4
            } else {
                5
            }
        }
        _ => 0,
    }
}

fn rule(locale: &str) -> Option<u8> {
    Some(match locale {
        "az" | "az_AZ" | "bo" | "bo_CN" | "bo_IN" | "dz" | "dz_BT" | "id" | "id_ID" | "ja" | "ja_JP" | "jv" | "ka"
        | "ka_GE" | "km" | "km_KH" | "kn" | "kn_IN" | "ko" | "ko_KR" | "ms" | "ms_MY" | "th" | "th_TH" | "tr"
        | "tr_CY" | "tr_TR" | "vi" | "vi_VN" | "zh" | "zh_CN" | "zh_HK" | "zh_SG" | "zh_TW" => 0,
        "af" | "af_ZA" | "bn" | "bn_BD" | "bn_IN" | "bg" | "bg_BG" | "ca" | "ca_AD" | "ca_ES" | "ca_FR" | "ca_IT"
        | "da" | "da_DK" | "de" | "de_AT" | "de_BE" | "de_CH" | "de_DE" | "de_LI" | "de_LU" | "el" | "el_CY"
        | "el_GR" | "en" | "en_AG" | "en_AU" | "en_BW" | "en_CA" | "en_DK" | "en_GB" | "en_HK" | "en_IE" | "en_IN"
        | "en_NG" | "en_NZ" | "en_PH" | "en_SG" | "en_US" | "en_ZA" | "en_ZM" | "en_ZW" | "eo" | "eo_US" | "es"
        | "es_AR" | "es_BO" | "es_CL" | "es_CO" | "es_CR" | "es_CU" | "es_DO" | "es_EC" | "es_ES" | "es_GT"
        | "es_HN" | "es_MX" | "es_NI" | "es_PA" | "es_PE" | "es_PR" | "es_PY" | "es_SV" | "es_US" | "es_UY"
        | "es_VE" | "et" | "et_EE" | "eu" | "eu_ES" | "eu_FR" | "fa" | "fa_IR" | "fi" | "fi_FI" | "fo" | "fo_FO"
        | "fur" | "fur_IT" | "fy" | "fy_DE" | "fy_NL" | "gl" | "gl_ES" | "gu" | "gu_IN" | "ha" | "ha_NG" | "he"
        | "he_IL" | "hu" | "hu_HU" | "is" | "is_IS" | "it" | "it_CH" | "it_IT" | "ku" | "ku_TR" | "lb" | "lb_LU"
        | "ml" | "ml_IN" | "mn" | "mn_MN" | "mr" | "mr_IN" | "nah" | "nb" | "nb_NO" | "ne" | "ne_NP" | "nl"
        | "nl_AW" | "nl_BE" | "nl_NL" | "nn" | "nn_NO" | "no" | "om" | "om_ET" | "om_KE" | "or" | "or_IN" | "pa"
        | "pa_IN" | "pa_PK" | "pap" | "pap_AN" | "pap_AW" | "pap_CW" | "ps" | "ps_AF" | "pt" | "pt_BR" | "pt_PT"
        | "so" | "so_DJ" | "so_ET" | "so_KE" | "so_SO" | "sq" | "sq_AL" | "sq_MK" | "sv" | "sv_FI" | "sv_SE" | "sw"
        | "sw_KE" | "sw_TZ" | "ta" | "ta_IN" | "ta_LK" | "te" | "te_IN" | "tk" | "tk_TM" | "ur" | "ur_IN" | "ur_PK"
        | "zu" | "zu_ZA" => 1,
        "am" | "am_ET" | "bh" | "fil" | "fil_PH" | "fr" | "fr_BE" | "fr_CA" | "fr_CH" | "fr_FR" | "fr_LU" | "gun"
        | "hi" | "hi_IN" | "hy" | "hy_AM" | "ln" | "ln_CD" | "mg" | "mg_MG" | "nso" | "nso_ZA" | "ti" | "ti_ER"
        | "ti_ET" | "wa" | "wa_BE" | "xbr" => 2,
        "be" | "be_BY" | "bs" | "bs_BA" | "hr" | "hr_HR" | "ru" | "ru_RU" | "ru_UA" | "sr" | "sr_ME" | "sr_RS"
        | "uk" | "uk_UA" => 3,
        "cs" | "cs_CZ" | "sk" | "sk_SK" => 4,
        "ga" | "ga_IE" => 5,
        "lt" | "lt_LT" => 6,
        "sl" | "sl_SI" => 7,
        "mk" | "mk_MK" => 8,
        "mt" | "mt_MT" => 9,
        "lv" | "lv_LV" => 10,
        "pl" | "pl_PL" => 11,
        "cy" | "cy_GB" => 12,
        "ro" | "ro_RO" => 13,
        "ar" | "ar_AE" | "ar_BH" | "ar_DZ" | "ar_EG" | "ar_IN" | "ar_IQ" | "ar_JO" | "ar_KW" | "ar_LB" | "ar_LY"
        | "ar_MA" | "ar_OM" | "ar_QA" | "ar_SA" | "ar_SD" | "ar_SS" | "ar_SY" | "ar_TN" | "ar_YE" => 14,
        _ => return None,
    })
}