# shellcheck shell=dash

xrc:mod:lib     lang    ls

___x_cmd_lang___main(){
    local op="$1";
    case "$op" in
        "")             ___x_cmd help -m lang >&2;  return 64 ;;
        -h|--help)      ___x_cmd help -m lang;      return ;;
        use|unuse|try|untry|ls|get)
                        shift; ___x_cmd_lang_"$op" "$@" ;;
        *)              N=lang M="Unknown subcmd -> $op" log:ret:64 ;;
    esac
}

___x_cmd_lang_get(){
    printf "%s\n" "$___X_CMD_LANG"
}

___x_cmd_lang_use(){
    ___x_cmd_lang___var_trim_ "$1"

    local fp="$___X_CMD_ROOT_DATA/lang/custom.txt"
    ___x_cmd ensurefp "$fp"
    ___x_cmd log :lang info "Set language to \"$___X_CMD_LANG\""
    printf "%s\n" "$___X_CMD_LANG" > "$fp"
}

___x_cmd_lang_unuse(){
    ___x_cmd_lang___var_trim_ ""
    ___x_cmd rmrf "$___X_CMD_ROOT_DATA/lang/custom.txt"
}

___x_cmd_lang_try(){
    ___x_cmd_lang___var_trim_ "$1"
}

___x_cmd_lang_untry(){
    ___x_cmd_lang___var_trim_ ""
}


___x_cmd_lang___var_trim_(){
    local lang="$1"
    case "$lang" in
        zh|cn|zh_*) ___X_CMD_LANG=zh ;;
        *)          ___X_CMD_LANG="$lang" ;;
    esac
}