Russian without Robotism, Part 1

    I fully support the cleanliness and proper use of the Russian language. In life in general and in Runet in particular. For from inscriptions like "23 pieces" or the averaged versions of "23 pieces." mildly jarring.

    Therefore, when the task arose to overcome roboticism in the endings of nouns, the corresponding function was developed and implemented (or rather, the plugin for Smarty).

    The function is based on the rule - in total there are 3 options (sometimes fewer) noun endings for any Russian words used with numbers:

    • If the last digit of the number is 1 (but not 11)
    • If the last digit of the number is 2,3,4 (but not * 12, * 13, * 14)
    • All other cases.


    Naturally, having received this regularity, writing a code was not difficult.

    Copy Source | Copy HTML
    1.  /**
    2. * Smarty {number2word} function plugin
    3. *
    4. * Type: function* Name: number2word* Purpose: Get russian (or other lang) word equivalent to current number of items
    5. * @author Beresnev Sergey
    6. * @param array
    7. * @param Smarty
    8. */
    9.  function smarty_function_number2word($params, &$smarty)
    10.  {
    11.  if (!isset($params['number'])) {
    12.  $smarty->trigger_error("numbertoword: Отсутствует параметр 'number'");
    13.  return;
    14.  }
    15.  if (!isset($params['name1']) or !isset($params['name2']) or !isset($params['name3'])) {
    16.  $smarty->trigger_error("numbertoword: Отсутствуют параметры 'name1' или 'name2' или 'name3'");
    17.  return;
    18.  }
    19.  $temp = strval($params['number']);
    20.  $temp = $temp[utf8_strlen($temp)-1];
    21.  return (($temp>1 and $temp <5 and (intval($params['number']%100)>19 or intval($params['number']%100)<10))?$params['name2']:($temp==1?$params['name1']:$params['name3']));
    22.  }


    After that, in the template it is enough to indicate the design of the form
    {number2word number=$your_number name1=Строка name2=Строки name3=Строк}

    Z.Y. Thank you for the correction to the account * 10- * 20

    Also popular now: