Skip to content

03. Basic use

Piotr B edited this page Jan 21, 2022 · 1 revision

Instance

Assigning

Before instantiating a new object, you have to pick the language you want the class to translate the values to. So you would have to chose one of these:

$slownie = new tei187\Slownie\DE(); // German
// or
$slownie = new tei187\Slownie\EN(); // English
// or
$slownie = new tei187\Slownie\PL(); // Polish

Constructor parameters

Each language's constructor has 4 parameters that can be assigned.

  1. value - (string), the value to translate.
  2. currency - (integer/string), the currency to use.
  3. exponent-as-fraction flag - (boolean), display exponent values as words or fraction (ex. "fifty cents" or "dollar 50/100").
  4. currency-as-code flag - (boolean), display currency as fully-translated words or ISO code (ex. "United States dollars" or "USD").

More information in documentation: tei187\Slownie\Slownie::_construct().

Translation

If you require multiple translations in the same process, you can translate already existing object into another language - refer to translateTo() method.

$pl = new tei187\Slownie\PL("1234.50", "pln");
$en = $pl->translateTo("en"); // returns a tei187\Slownie\EN class object.

$pl->output(); // jeden tysiąc dwieście trzydzieści cztery złote, pięćdziesiąt groszy
$en->output(); // one thousand two hundred thirty four Polish zlotys, fifty grosze

Setters

Value

Due to concerns and different methods of handling large integer or high-precision float values, the currency values have to be passed as or converted to the form of string. As such, handling integer and float input values has to be done separately and is not considered to be within the scope of this package. To set the value for translation:

$slownie->setValue("1234.56");

Currency

Currency can be passed as ISO 4217 string or digit code (reference). String code method is not case-sensitive. Digit code can be input as both string or integer (lack of preceding zeroes is handled by the Currency class check).

$slownie->setCurrency("usd");
// or
$slownie->setCurrency("840");

You can also define a Currency class object and pass it as argument value.

$currency = new \tei187\Slownie\Currency("usd");
$slownie->setCurrency($currency);

Formatting

For input strings that are not seemingly well-formed floats, it may be necessary to set formatting information, using 'setFormatting()' method. It accepts two parameters, first being a separator for thousands, second for decimals. After applying, the translator reparses input value.

$slownie->setValue("1.234,50");
$slownie->setFormatting(".", ","); // 1234.50

Flags

  • fractions use

If you want the minor values to be returned in fractional form rather than string, use the setFractions() method.

$slownie->setValue("1234.55")->setCurrency("usd")->setFractions(true);
$slownie->output();
// will return "one thousand two hundred thirty four United States dollars 55/100"
  • picker use

If you do not want to translated the currency names itself, use the setPickerUse() method.

$slownie->setValue("1234.55")->setCurrency("usd")->setPickerUse(true);
$slownie->output();
// will return "one thousand two hundred thirty four USD, fifty five cents"

Clone this wiki locally