Skip to content
GitHub

Guides

Use malagasy-intl with your framework

Show values, build a form with typing help and validation, and choose your settings once, in TypeScript, React, Vue, Angular, Svelte, Node, Bun and Deno, plain PHP, Laravel and Symfony.

The functions are plain, dependency-free calls, so they work the same in every framework: no plugin, provider or setup step. Each example below writes the same receipt: Ar 150 000, the amount in words, the payer’s number in international format and the date in Malagasy.

Every framework has the same three steps, so you can read only yours:

  1. Show values: the same receipt everywhere, Ar 150 000, the amount in words, the payer’s number in international format and the date in Malagasy.
  2. Build a form: group a phone or amount field while the user types, check the value, and show a message in French or English that the library already wrote.
  3. Choose your settings once: one object, or one config file, so no call repeats an option.

Install @mdgascar/malagasy-intl for every module, or only the packages you use (@mdgascar/ariary, @mdgascar/phone-number, @mdgascar/number-words, @mdgascar/date-time). They run in Node.js 22.18+, Bun, Deno and every modern browser, as ESM or CommonJS, with TypeScript types included.

For settings, create one object and import it everywhere:

mg.ts
import { createMalagasy } from '@mdgascar/malagasy-intl';
export const mg = createMalagasy({ phoneFormat: 'international' });

Then mg.money(order.total), mg.phone(order.phone) and mg.date(order.paidOn) need no options. See Choose your settings once.

Plain functions, typed options and results. satisfies checks the object without widening it.

receipt.ts
import { formatAriary, spellAriary } from '@mdgascar/ariary';
import { formatPhone } from '@mdgascar/phone-number';
import { formatDate } from '@mdgascar/date-time';
const receipt = {
amount: formatAriary(150000),
words: spellAriary(150000),
phone: formatPhone('0341234567', 'international'),
date: formatDate('2026-11-23', 'full'),
} satisfies Record<string, string>;

Call the functions while rendering: they are pure and fast, so there is nothing to memoize. Works the same in Next.js, Remix and React Native.

Receipt.tsx
import { formatAriary, spellAriary } from '@mdgascar/ariary';
import { formatPhone } from '@mdgascar/phone-number';
import { formatDate } from '@mdgascar/date-time';
export function Receipt({ order }: { order: Order }) {
return (
<dl>
<dd>{formatAriary(order.total)}</dd>
<dd>{spellAriary(order.total)}</dd>
<dd>{formatPhone(order.phone, 'international')}</dd>
<dd>{formatDate(order.paidOn, 'full')}</dd>
</dl>
);
}

For a form, group the field in the change handler and check it while rendering. safeParsePhone never throws, and describeError gives the message.

PhoneField.tsx
import { useState } from 'react';
import { describeError, formatPhoneInput, safeParsePhone } from '@mdgascar/malagasy-intl';
export function PhoneField() {
const [text, setText] = useState('');
const result = text === '' ? undefined : safeParsePhone(text);
return (
<label>
Phone
<input
value={text}
inputMode="tel"
onChange={(event) => setText(formatPhoneInput(event.target.value))}
/>
{result?.success === false && <p role="alert">{describeError(result.error, 'fr')}</p>}
</label>
);
}

Use them straight in the template, with <script setup lang="ts">. Works the same in Nuxt.

Receipt.vue
<script setup lang="ts">
import { formatAriary, spellAriary } from '@mdgascar/ariary';
import { formatPhone } from '@mdgascar/phone-number';
import { formatDate } from '@mdgascar/date-time';
defineProps<{ order: Order }>();
</script>
<template>
<p>{{ formatAriary(order.total) }}</p>
<p>{{ spellAriary(order.total) }}</p>
<p>{{ formatPhone(order.phone, 'international') }}</p>
<p>{{ formatDate(order.paidOn, 'full') }}</p>
</template>

In a form, a computed holds the message.

PhoneField.vue
<script setup lang="ts">
import { computed, ref } from 'vue';
import { describeError, formatPhoneInput, safeParsePhone } from '@mdgascar/malagasy-intl';
const text = ref('');
const message = computed(() => {
if (text.value === '') return undefined;
const result = safeParsePhone(text.value);
return result.success ? undefined : describeError(result.error, 'fr');
});
</script>
<template>
<label>
Phone
<input
:value="text"
inputmode="tel"
@input="text = formatPhoneInput(($event.target as HTMLInputElement).value)"
/>
</label>
<p v-if="message" role="alert">{{ message }}</p>
</template>

Wrap the calls in computed() signals on a standalone component, or in a pure pipe if you prefer templates.

receipt.component.ts
import { Component, computed, input } from '@angular/core';
import { formatAriary, spellAriary } from '@mdgascar/ariary';
import { formatPhone } from '@mdgascar/phone-number';
import { formatDate } from '@mdgascar/date-time';
@Component({ selector: 'app-receipt', template: '{{ amount() }} · {{ words() }}' })
export class ReceiptComponent {
order = input.required<Order>();
amount = computed(() => formatAriary(this.order().total));
words = computed(() => spellAriary(this.order().total));
phone = computed(() => formatPhone(this.order().phone, 'international'));
date = computed(() => formatDate(this.order().paidOn, 'full'));
}

In a form, a signal holds the grouped text and a computed() holds the message.

phone-field.component.ts
import { Component, computed, signal } from '@angular/core';
import { describeError, formatPhoneInput, safeParsePhone } from '@mdgascar/malagasy-intl';
@Component({
selector: 'app-phone-field',
template: `
<label>
Phone
<input [value]="text()" inputmode="tel" (input)="type($event)" />
</label>
@if (message()) {
<p role="alert">{{ message() }}</p>
}
`,
})
export class PhoneFieldComponent {
text = signal('');
message = computed(() => {
if (this.text() === '') return undefined;
const result = safeParsePhone(this.text());
return result.success ? undefined : describeError(result.error, 'fr');
});
type(event: Event): void {
this.text.set(formatPhoneInput((event.target as HTMLInputElement).value));
}
}

Call them in markup with Svelte 5 runes. Works the same in SvelteKit.

Receipt.svelte
<script lang="ts">
import { formatAriary, spellAriary } from '@mdgascar/ariary';
import { formatPhone } from '@mdgascar/phone-number';
import { formatDate } from '@mdgascar/date-time';
let { order }: { order: Order } = $props();
</script>
<p>{formatAriary(order.total)}</p>
<p>{spellAriary(order.total)}</p>
<p>{formatPhone(order.phone, 'international')}</p>
<p>{formatDate(order.paidOn, 'full')}</p>

In a form, $state holds the text and $derived holds the result.

PhoneField.svelte
<script lang="ts">
import { describeError, formatPhoneInput, safeParsePhone } from '@mdgascar/malagasy-intl';
let text = $state('');
const result = $derived(text === '' ? undefined : safeParsePhone(text));
</script>
<label>
Phone
<input
value={text}
inputmode="tel"
oninput={(event) => (text = formatPhoneInput(event.currentTarget.value))}
/>
</label>
{#if result?.success === false}
<p role="alert">{describeError(result.error, 'fr')}</p>
{/if}

On the server, return the values from any handler. This fetch export runs as is in Bun and Deno, and in Node behind any adapter (Hono, Express…).

server.ts
import { formatAriary, spellAriary } from '@mdgascar/ariary';
import { formatPhone } from '@mdgascar/phone-number';
import { formatDate } from '@mdgascar/date-time';
export default {
fetch: () =>
Response.json({
amount: formatAriary(150000),
words: spellAriary(150000),
phone: formatPhone('0341234567', 'international'),
date: formatDate('2026-11-23', 'full'),
}),
};

To check a request body, answer 422 with the shared error code and the message.

server.ts
import { describeError, formatPhone, safeParsePhone } from '@mdgascar/malagasy-intl';
export default {
async fetch(request: Request): Promise<Response> {
const { phone } = await request.json();
const result = safeParsePhone(phone);
if (!result.success) {
return Response.json(
{ code: result.error.code, message: describeError(result.error, 'fr') },
{ status: 422 },
);
}
return Response.json({ phone: formatPhone(result.data, 'e164') });
},
};

Any schema library works through its own refinement hook. The same two functions, safeParsePhone and describeError, are all you need.

schema.ts
import { z } from 'zod';
import * as v from 'valibot';
import { describeError, isValidPhone, safeParsePhone } from '@mdgascar/malagasy-intl';
export const ZodPhone = z.string().transform((value, context) => {
const result = safeParsePhone(value);
if (result.success) return result.data;
context.addIssue({ code: 'custom', message: describeError(result.error, 'fr') });
return z.NEVER;
});
export const ValibotPhone = v.pipe(
v.string(),
v.check(isValidPhone, describeError('INVALID_FORMAT', 'fr')),
);

PHP 8.3 or later. The core package works in any Composer project; Laravel and Symfony get dedicated packages for validation, templates and database types.

composer require mdgascar/malagasy-intl, then value objects and enums. Works the same in WordPress, Slim, Laminas or any script.

receipt.php
use Mdgascar\MalagasyIntl\Ariary\Ariary;
use Mdgascar\MalagasyIntl\PhoneNumber\{PhoneNumber, PhoneNumberFormat};
use Mdgascar\MalagasyIntl\DateTime\{DateStyle, MalagasyDate};
$receipt = [
'amount' => Ariary::fromInt(150000)->format(),
'words' => Ariary::fromInt(150000)->spell(),
'phone' => PhoneNumber::parse('0341234567')->format(PhoneNumberFormat::International),
'date' => MalagasyDate::fromString('2026-11-23')->format(DateStyle::Full),
];

To handle a posted form, group the field for display and read the result without exceptions.

order.php
use Mdgascar\MalagasyIntl\MessageLocale;
use Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumber;
$phone = $_POST['phone'] ?? '';
$value = $phone === '' ? '' : PhoneNumber::formatInput($phone);
$result = PhoneNumber::safeParse($phone);
if (!$result->success) {
echo $result->error?->errorCode->describe(MessageLocale::French);
}

For settings, create one Malagasy object and pass it to the code that needs it: new Malagasy(phoneFormat: PhoneNumberFormat::International).

composer require mdgascar/malagasy-intl-laravel adds Blade directives, Eloquent casts (AsAriary, AsPhoneNumber) and the validation rules mg_phone, mg_ariary, mg_words, mg_ordinal, mg_date, mg_time and mg_datetime. Laravel 12 and 13.

receipt.blade.php
{{-- Order casts: 'total' => AsAriary::class, 'phone' => AsPhoneNumber::class --}}
@use('Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumberFormat')
@use('Mdgascar\MalagasyIntl\DateTime\DateStyle')
<p>@ariary($order->total)</p>
<p>{{ $order->total->spell() }}</p>
<p>@mgPhone($order->phone, PhoneNumberFormat::International)</p>
<p>@mgDate($order->paid_on, DateStyle::Full)</p>

In a form request, use the rules. Messages come in English and French and follow the app locale.

StoreOrderRequest.php
use Illuminate\Foundation\Http\FormRequest;
final class StoreOrderRequest extends FormRequest
{
public function rules(): array
{
return [
'phone' => ['required', 'mg_phone:mobile'],
'total' => ['required', 'mg_ariary'],
'paid_on' => ['required', 'mg_date'],
];
}
}

In the view, @mgPhoneInput and @mgAmountInput keep the grouping after a failed submit.

create.blade.php
<input name="phone" inputmode="tel" value="@mgPhoneInput(old('phone'))">
<input name="total" inputmode="decimal" value="@mgAmountInput(old('total'))">
@error('phone') <p role="alert">{{ $message }}</p> @enderror
@error('paid_on') <p role="alert">{{ $message }}</p> @enderror

For settings, publish the config with php artisan vendor:publish --tag=malagasy-intl and set the ariary options (grouping, symbol_position, decimal, currency_display), or use the MALAGASY_INTL_ARIARY_* environment variables. The container also gives you a Malagasy object that uses them:

OrderController.php
use Mdgascar\MalagasyIntl\Malagasy;
final class OrderController
{
public function __construct(private readonly Malagasy $mg) {}
public function show(Order $order): array
{
return [
'total' => $this->mg->money($order->total),
'phone' => $this->mg->phone($order->phone),
'paid_on' => $this->mg->date($order->paid_on),
];
}
}

composer require mdgascar/malagasy-intl-symfony adds Twig filters, Doctrine types (mg_ariary, mg_phone) and the constraints MalagasyPhone, AriaryAmount and MalagasyText. Symfony 7.4 LTS and 8.

receipt.html.twig
{# Doctrine types: order.total is an Ariary, order.phone a PhoneNumber #}
<p>{{ order.total|ariary }}</p>
<p>{{ order.total.spell }}</p>
<p>{{ order.phone|mg_phone('international') }}</p>
<p>{{ order.paidOn|mg_date('full') }}</p>

On an input class, use the constraints. Messages come in English and French and follow the request locale.

OrderInput.php
use Mdgascar\MalagasyIntl\PhoneNumber\PhoneLineType;
use Mdgascar\MalagasyIntl\ReadableText;
use Mdgascar\MalagasyIntlBundle\Validator\AriaryAmount;
use Mdgascar\MalagasyIntlBundle\Validator\MalagasyPhone;
use Mdgascar\MalagasyIntlBundle\Validator\MalagasyText;
use Symfony\Component\Validator\Constraints as Assert;
final class OrderInput
{
#[Assert\NotBlank, MalagasyPhone(type: PhoneLineType::Mobile)]
public ?string $phone = null;
#[Assert\NotBlank, AriaryAmount]
public ?string $total = null;
#[Assert\NotBlank, MalagasyText(ReadableText::Date)]
public ?string $paidOn = null;
}

In the template, mg_phone_input and mg_amount_input keep the grouping after a failed submit.

new.html.twig
<input name="phone" inputmode="tel" value="{{ input.phone|mg_phone_input }}">
<input name="total" inputmode="decimal" value="{{ input.total|mg_amount_input }}">
{% for violation in violations %}
<p role="alert">{{ violation.message }}</p>
{% endfor %}

For settings, set the ariary options in config/packages/malagasy_intl.yaml (grouping, symbol_position, decimal, currency_display). The Malagasy service uses them and can be injected anywhere:

OrderController.php
use Mdgascar\MalagasyIntl\Malagasy;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
final class OrderController
{
public function __construct(private readonly Malagasy $mg) {}
#[Route('/orders/{id}')]
public function show(Order $order): JsonResponse
{
return new JsonResponse([
'total' => $this->mg->money($order->total),
'phone' => $this->mg->phone($order->phone),
'paid_on' => $this->mg->date($order->paidOn),
]);
}
}