Flag catalog and package
The flag catalog gives the site and its tools one shared set of country, regional, and international flags. It joins flag artwork with names, codes, locale hints, search terms, color data, and source records, then publishes the result as web assets and the @thesvg/flags package. The flag browser experience uses this catalog to help people find and inspect flags.
Catalog scope
The catalog represents current ISO 3166-1 entries plus a small set of user-assigned, regional, and organization flags. Each entry has one stable slug and code, plus enough identity data to support display, lookup, and links across the site.
The current set has 263 flags: 250 ISO entries including Kosovo under XK, and 13 additional entries for England, Scotland, Wales, Catalonia, the Basque Country, Galicia, the Valencian Community, the Balearic Islands, the Canary Islands, Corsica, Occitania, the European Union, and the United Nations. Each entry has 4x3, square, and circle variants. packages/…/flags.json• L54-128 packages/…/types.ts• L1-39
The catalog uses a flag as a country or region mark, not as a language mark. Locale hints are therefore conventional country choices and should appear beside the language name rather than replace it. packages/…/locale-core.ts• L6-39
References: packages/…/flags.json• L54-128 packages/…/types.ts• L1-39 packages/…/locale-core.ts• L6-39
Metadata model
Each flag record connects identity, geography, language, artwork, and discovery data. Generated TypeScript exposes the same record shape to the package and the website.
| Metadata group | Purpose |
|---|---|
| Identity | Stable slug, lowercase code, ISO alpha-2, alpha-3, numeric code, names, and emoji. |
| Classification | Flag kind, parent country for subdivisions, continent, and capital. |
| Language | Common language tags and locale tags for which the flag can act as a hint. |
| Discovery | Normalized aliases for names, codes, former names, locale tags, emoji, and colors. |
| Appearance | Palette entries with hex values and painted-area shares, color families, and available shapes. |
The JSON catalog is the source record for generated metadata. flags.generated.ts is its code form, while flag-names.generated.ts keeps only code-to-English-name data so locale lookup stays small. These generated files are outputs of the flag generator and are not hand-edited. scripts/…/generate-flags.mjs• L398-404 packages/…/flag-names.generated.ts• L1-8
References: packages/…/flags.json• L54-128 packages/…/types.ts• L6-39 scripts/…/generate-flags.mjs• L398-404
Generation and provenance
The generator turns vetted upstream artwork and reference data into one checked catalog. It validates coverage and SVG safety before it writes artwork, metadata, search data, registry entries, licenses, and documentation markers.
The generation flow moves from source files to normalized artwork and metadata, then to the web catalog and package inputs.
flowchart LR
A[Upstream flag artwork] --> B[Generator]
R[ISO and language reference data] --> B
B --> C[Flag SVG files]
B --> D[Catalog metadata]
B --> E[Search alias data]
B --> F[Source and license manifest]
C --> G[Website registry]
D --> G
C --> H[Published flag package]
D --> H
The generator keeps source commits and per-file provenance with the result. The manifest also records exclusions, so the catalog boundary remains visible when a flag is not included. scripts/…/generate-flags.mjs• L1-23 scripts/…/generate-flags.mjs• L81-126 scripts/…/generate-flags.mjs• L375-436
References: scripts/…/generate-flags.mjs• L1-23 scripts/…/generate-flags.mjs• L81-126 scripts/…/generate-flags.mjs• L375-436
Search and ranking
The search helper resolves a flag from a slug, code, or alias. It accepts common names, native names, ISO codes, numeric codes, emoji, former names, locale tags, and color terms. Exact terms rank before prefixes, and prefixes rank before substring matches; the helper does not try to correct spelling.
getFlag is for one known identity. findFlags is for a user query and returns all useful matches in relevance order. The same rank rules are shared with the browser so package users and the site agree on what a query means. packages/…/search.ts• L8-27 packages/…/rank.ts• L17-36
The website keeps the first-load tile data compact and loads the larger alias map only when a person searches. This keeps browsing light without changing the search terms available after the alias data loads. src/…/flags-tiles.ts• L15-34 scripts/…/generate-flags.mjs• L402-404
References: packages/…/search.ts• L8-27 packages/…/rank.ts• L17-36 src/…/flags-tiles.ts• L15-34 scripts/…/generate-flags.mjs• L402-404
Locale mapping
The locale helper maps a BCP 47 locale to one flag when a conventional country hint exists. It checks an exact language-region choice first, then a region subtag, then a script choice, and finally a bare-language choice; malformed or unknown input returns no result.
This order lets pt-BR select Brazil and fr-CA select Canada while still giving a useful default for en or zh-Hant. The result contains the stable slug, code, and English name, so callers can render the flag without loading the full search catalog. packages/…/locale-core.ts• L41-76 packages/…/locale.ts• L7-15
References: packages/…/locale-core.ts• L41-76 packages/…/locale.ts• L7-15
Color data and ranking
Color data turns each flag into searchable and sortable appearance data. The generator samples the default 4x3 artwork, groups nearby RGB colors, keeps the largest useful groups, and records their share of painted pixels.
Each palette is capped at six colors and ordered by share. Color families use an HSL rule with names such as red, gold, blue, black, and white. The dominant non-white color also becomes the catalog accent used by the wider icon registry. scripts/…/generate-flags.mjs• L157-185 scripts/…/colors.mjs• L14-29
These values support more than display swatches. Search can match family names and hex values, while the browser can filter by one or more families, find colors near a supplied hex value, and sort by dominant hue. packages/…/search.ts• L17-27 src/…/model.ts• L120-140
References: scripts/…/generate-flags.mjs• L157-185 scripts/…/colors.mjs• L14-29 packages/…/search.ts• L17-27 src/…/model.ts• L120-140
Published flag package
The @thesvg/flags package publishes the catalog as small, per-flag entry points. Consumers can use raw SVG strings, React components, locale helpers, or the full search and metadata surface according to their needs.
| Package surface | Role |
|---|---|
| Root entry | Small locale helper and shared types. |
@thesvg/flags/search | Full metadata, lookup, and ranked search. |
@thesvg/flags/locale | Locale-to-flag mapping. |
@thesvg/flags/react and per-flag React paths | React components for all flags. |
@thesvg/flags/flags/<code> | SVG strings for one flag. |
| @thesvg/…/<code>.svg | Raw 4x3, square, or circle files. |
| manifest.json | Published metadata and source information. |
The package is tree-shakeable and does not require React unless a React surface is used. Its build creates ESM, CommonJS, type declarations, raw SVG files, per-flag modules, and the manifest from the same generated catalog. packages/…/package.json• L28-114 packages/…/build.ts• L35-90
The React surface treats flags as decorative by default and gives them an accessible name only when a title is supplied. It also keeps each flag instance's internal SVG identifiers separate, which allows detailed flags to appear more than once on a page. packages/…/create-flag.ts• L14-23 packages/…/create-flag.ts• L30-80
References: packages/…/package.json• L28-114 packages/…/build.ts• L35-90 packages/…/create-flag.ts• L14-23 packages/…/create-flag.ts• L30-80
Catalog contracts
The catalog's checks protect the links between metadata, artwork, registry entries, licenses, and package exports. They require complete ISO coverage, the regional extras, unique identifiers, all three shapes, safe SVG content, matching aliases, and a license record for every published file. packages/…/data.test.ts• L20-118 packages/…/pack.test.ts• L14-33
Color checks keep palettes bounded and ordered, keep the family classifier stable, and keep the registry's dominant color in step with the metadata. Search and locale checks protect both lookup forms and fallback order. packages/…/colors.test.ts• L14-52 packages/…/locale-search.test.ts• L12-38
References: packages/…/data.test.ts• L20-118 packages/…/pack.test.ts• L14-33 packages/…/colors.test.ts• L14-52 packages/…/locale-search.test.ts• L12-38
Change checklist
When these files or sections are changed, remember to consider these:
- When a flag entry or alias changes, regenerate the catalog so flags.generated.ts, flag-names.generated.ts, flags-search.json, icons.json, and the published manifest stay aligned.
- When a source or SVG variant changes, update the per-file license manifest and third-party notices with the generated output.
- When locale rules or flag names change, check the small root locale surface and the browser's shared ranking rules together.
- When palette rules change, check search aliases, registry colors, browser color filters, and hue sorting as one contract.
- When package exports change, keep the generated ESM, CommonJS, type, raw SVG, and tarball paths available to consumers.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Metadata is edited without regeneration | Website entries, search aliases, and package metadata disagree. | Generator outputs are checked against the shared registry and generated-file tests. |
| A flag lacks one shape | A browser view or package path fails for that flag. | The data suite requires default, square, and circle SVG files for every entry. |
| An alias is removed or ranked differently | A known name, code, locale, or color query returns the wrong result or no result. | Search tests cover alternate names, exact identifiers, and shared rank logic. |
| A source file has no accepted license record | The artwork cannot be published with complete provenance. | The license test matches every on-disk flag file to an accepted source entry. |
| A package export points to a missing build file | Consumers fail at import time after install. | The pack test checks every export target in the tarball. |
Key files
| File | Why to read it |
|---|---|
| packages/…/flags.json | Canonical generated catalog records and source provenance. |
| packages/…/types.ts | Shared meaning of flag identity, locale, palette, and variants. |
| scripts/…/generate-flags.mjs | End-to-end catalog generation, validation, and output contract. |
| packages/…/search.ts | Public identity lookup and alias search behavior. |
| packages/…/rank.ts | Shared normalization and relevance ranking used by the package and browser. |
| packages/…/locale-core.ts | Locale fallback rules and conventional country hints. |
| scripts/…/colors.mjs | Palette extraction and color-family classification. |
| packages/…/build.ts | Published package assembly and generated entry points. |
| packages/…/package.json | Public package exports, build lifecycle, and optional React dependency contract. |
| src/…/flags-tiles.ts | Compact catalog view used by the browser. |
| src/…/model.ts | Browser filtering and sorting behavior built on catalog metadata. |
| packages/…/data.test.ts | Coverage, shape, registry, and provenance guarantees. |
| packages/…/locale-search.test.ts | Lookup and locale mapping guarantees. |