Search by

mscode-pl / vocative

mscodepl

Address people by first name in their language: vocative case, gender and salutation. For Polish, Ukrainian, Czech, Serbian and Croatian.

Package info

github.com/mscode-pl/vocative

pkg:composer/mscode-pl/vocative

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.0 2026-11-29 00:21 UTC

This package is auto-updated.

Last update: 2026-11-29 01:14:04 UTC


README

Vocative: address people by first name in their language

npm version npm downloads Packagist version Packagist downloads Tests PHP 8.2 to 8.5 License

In Polish you greet Anna with "Cześć Anno" and Mateusz with "Cześć Mateuszu". Most apps print "Cześć Anna" because the name comes straight from a form. Vocative turns a first name into the form you address someone with, tells you whether it is a man's or a woman's name, and builds the formal greeting. It covers Polish, Ukrainian, Czech, Serbian and Croatian. The same rules ship for JavaScript and PHP, and a Tolgee plugin does it inside your translations.

Install · JavaScript · PHP · Tolgee · How it works · Languages

Install

The same rules ship as an npm package and a Composer package. Install the one for your stack, not both.

JavaScript or TypeScript (browser, Node, React Native):

npm install @mscode-pl/vocative

PHP 8.2+ (Laravel or plain PHP):

composer require mscode-pl/vocative

No dependencies. The Polish data adds about 2.4 kB gzipped to a bundle, each other language about 1 kB.

JavaScript

import { vocative, gender, salutation } from '@mscode-pl/vocative/pl';

vocative('Anna');                      // 'Anno'
vocative('Paweł');                     // 'Pawle'
vocative('Maciek');                    // 'Maćku'
vocative('Anna Maria');                // 'Anno Mario'
vocative('Jan K.');                    // 'Janie K.'

gender('Kuba');                        // 'm'
gender('Andrea');                      // null, used by both men and women

salutation('Anna');                    // 'Anno'
salutation('Anna', { formal: true });  // 'Pani Anno'
salutation('Jan', { formal: true });   // 'Panie Janie'
  • Each language has its own entry point, so a bundle only carries the data for the languages you import.
  • Casing follows the input: ANNA gives ANNO, anna gives anno.
  • Account names are cleaned first, so a Google or Apple sign-in name works as it comes: Kasia9632 and KasiaDy12653 give Kasiu, ania.k gives aniu. Digits, symbols and a camelCase tail are dropped; when nothing usable is left (123), the result is an empty string, so you can fall back to a generic greeting.
  • Unknown names never throw. They go through the rules and come back inflected as well as the rules allow.
  • gender returns null when a name is common for both sexes. A formal salutation then falls back to the plain vocative instead of guessing "Pan" or "Pani".

The language object is there when the language is chosen at runtime:

import { polish } from '@mscode-pl/vocative/pl';
import { ukrainian } from '@mscode-pl/vocative/uk';
import type { Language } from '@mscode-pl/vocative';

const languages: Record<string, Language> = { pl: polish, uk: ukrainian };
languages[user.locale]?.vocative(user.firstName) ?? user.firstName;

PHP

use MSCode\Vocative\Vocative;

Vocative::pl()->vocative('Anna');                   // 'Anno'
Vocative::pl()->gender('Kuba');                     // 'm'
Vocative::pl()->salutation('Anna', formal: true);   // 'Pani Anno'

Vocative::uk()->vocative('Олександр');             // 'Олександре'
Vocative::for($user->locale)->vocative($user->first_name);

Vocative::for() accepts regional codes (pl-PL, sr_Latn) and throws an InvalidArgumentException for a language without rules, and Vocative::languages() lists the ones there are. In a Blade template or a laravel-react-email mailable:

Cześć {{ \MSCode\Vocative\Vocative::pl()->vocative($user->first_name) }}!

PHP 8.2 or newer with mbstring. The JavaScript and PHP versions are tested against the same fixtures and give the same result for every one of the 3,649 Polish first names in the data.

Tolgee

Translators write {name, vocative} in Tolgee, and the plugin fills in the vocative for the language being shown:

import { Tolgee } from '@tolgee/web';
import { FormatIcu } from '@tolgee/format-icu';
import { VocativePlugin } from '@mscode-pl/vocative/tolgee';
import { polish } from '@mscode-pl/vocative/pl';

const tolgee = Tolgee()
  .use(FormatIcu())
  .use(VocativePlugin({ languages: [polish] }))
  .init({ language: 'pl' /* ... */ });
{ "hello": "Cześć {name, vocative}!" }
t('hello', { name: 'Mateusz' }); // 'Cześć Mateuszu!'
  • pl-PL and other regional codes fall back to pl.
  • In a language without rules, {name, vocative} prints the name unchanged.
  • Words that depend on gender use a normal ICU select, with the gender passed as a parameter:
{ "dear": "{gender, select, f {Droga} m {Drogi} other {Witaj}} {name, vocative}" }
t('dear', { name, gender: gender(name) ?? 'other' }); // 'Droga Anno'

The plugin registers a formatter on Tolgee's core, which @tolgee/web, @tolgee/react and the other integrations share. It is tested with @tolgee/core and @tolgee/format-icu 7.3, and it does not import Tolgee itself.

How it works

Polish vocatives follow the end of the name: -a becomes -o (Anna, Anno), a fleeting -ek becomes -ku (Marek, Marku), a hard consonant takes -e with a sound change (Piotr, Piotrze; Robert, Robercie), and a soft one takes -u (Tomasz, Tomaszu). Diminutives such as Kasia and Ola take -u.

The rules were checked against PoliMorf, the morphological dictionary of the Polish Academy of Sciences, on every first name carried by at least 100 people in the PESEL register:

First names checked 3,649 (dane.gov.pl, January 2026)
With a vocative in the dictionary 1,281
Rules alone give the dictionary form 1,249 (97.5%)
Stored exceptions 32 vocatives, 512 genders

The 32 names where the rules miss, such as Lew (Lwie) or Wawrzyniec (Wawrzyńcu), are stored as exceptions. Gender comes from the PESEL lists: a name counts as male or female when at least 90% of the people who carry it are one sex, and as null otherwise.

What it does not do:

  • Surnames in Polish. In practice Poles say "Panie Kowalski", so the surname stays in the nominative.
  • Nicknames that are not registered first names. They go through the rules, which handle the common patterns (Kasia, Bartek, Zosia) but can miss rarer ones.

Other languages

Language Example Formal
Ukrainian Олександр, Олександре; Марія, Маріє; Наталя, Наталю Пане / Пані
Czech Petr, Petře; Zdeněk, Zdeňku; Jana, Jano Pane / Paní
Serbian Petar, Petre; Miloš, Milošu; Milica, Milice (Cyrillic and Latin) Gospodine / Gospođo
Croatian Ivan, Ivane; Josip, Josipe; Ana, Ana Gospodine / Gospođo

These rules are newer than the Polish ones. They are checked against a hand-made list of about 50 common names per language (tests/fixtures/<code>.json), not yet against a full dictionary, so rarer names can come out wrong. Ukrainian works on Cyrillic and returns Latin spellings such as Oleksandr unchanged, with null gender. Serbian answers in the script it was given. Serbian and Croatian leave most female names unchanged, since their vocative there depends on stress that the spelling does not show; only -ica takes -ice.

Languages

Language Code JavaScript PHP Tolgee
Polish pl @mscode-pl/vocative/pl Vocative::pl() yes
Ukrainian uk @mscode-pl/vocative/uk Vocative::uk() yes
Czech cs @mscode-pl/vocative/cs Vocative::cs() yes
Serbian sr @mscode-pl/vocative/sr Vocative::sr() yes
Croatian hr @mscode-pl/vocative/hr Vocative::hr() yes

Greek, Lithuanian and Romanian also have a vocative. Each language is a folder of rules and a fixture file of real names, so adding one does not touch the others. CONTRIBUTING.md walks through it.

Development

pnpm install && composer install
pnpm test && vendor/bin/pest
pnpm data:pl    # rebuild the Polish exceptions and fixtures from PESEL and PoliMorf

License

MIT. The Polish data is built from the PESEL first-name lists (CC0 1.0) and PoliMorf (2-clause BSD), see data/pl/NOTICE.md.

MSCode

Built and maintained by MSCode, a Polish software house that makes websites and web and mobile apps.

mscode.pl · More packages · Report an issue