localized-greeting
v1.0.0
Published
Time-of-day greetings in the user's locale. Zero dependencies.
Downloads
14
Maintainers
Readme
localized-greeting
Time-of-day greetings ("Good morning", "Guten Tag", "Bonsoir") in the user's locale. Zero runtime dependencies. Works in Node 18+ and modern browsers.
Install
npm install localized-greetingBasic usage
import { getGreeting, getTimeOfDay } from 'localized-greeting';
// Uses the runtime locale and current time automatically
console.log(getGreeting()); // e.g. "Good morning"
// Explicit locale
console.log(getGreeting({ locale: 'de' })); // "Guten Morgen"
console.log(getGreeting({ locale: 'fr' })); // "Bonjour"
// Explicit date
const d = new Date('2024-01-01T20:00:00');
console.log(getGreeting({ locale: 'es', date: d })); // "Buenas noches"
// Just the time bucket
console.log(getTimeOfDay()); // "morning" | "afternoon" | "evening"Custom locale registration
import { registerLocale, getGreeting, getSupportedLocales } from 'localized-greeting';
registerLocale('ja', {
morning: 'おはようございます',
afternoon: 'こんにちは',
evening: 'こんばんは',
});
console.log(getGreeting({ locale: 'ja' })); // "おはようございます" (in the morning)
console.log(getSupportedLocales()); // [..., 'ja']registerLocale can also override a built-in locale at runtime.
Locale resolution
- Try the full BCP 47 tag (e.g.
de-AT). - Try the base language (
de). - Fall back to
en.
Underscores are normalised to hyphens (de_AT → de-AT) and matching is case-insensitive.
When no locale option is passed, the library reads Intl.DateTimeFormat().resolvedOptions().locale (the system/browser locale) and falls back to "en" if unavailable.
Note: Time-of-day detection uses the local system clock of the environment running the code (Node.js process or browser tab). The
dateoption lets you override this with anyDateobject.
Time buckets
| Hour range | Bucket |
| ---------- | ----------- |
| 05 – 11 | morning |
| 12 – 17 | afternoon |
| 18 – 04 | evening |
Built-in locales
en, de, es, fr, it, pt, nl, sv, da, pl
API
type TimeOfDay = 'morning' | 'afternoon' | 'evening';
type GreetingTranslations = Record<TimeOfDay, string>;
interface GreetingOptions {
locale?: string; // BCP 47, defaults to runtime locale then "en"
date?: Date; // defaults to new Date()
}
function getGreeting(options?: GreetingOptions): string;
function getTimeOfDay(date?: Date): TimeOfDay;
function registerLocale(locale: string, translations: GreetingTranslations): void;
function getSupportedLocales(): string[];Development
npm install
npm run build # tsup → dist/
npm test # vitest
npm run typecheck # tsc --noEmit
npm run lint # eslint
npm run format # prettierLicense
MIT
