Skip to contents

Implements the "Lookup" scheme from RFC 4647 Section 3.4: given an ordered list of language tag preferences and a set of available tags, returns the best match. Matching proceeds by progressively stripping the rightmost subtag from each preference until a match is found or no subtags remain. Single-character subtags (including the private-use prefix "x") are always stripped as a unit together with any preceding single-character subtags.

Usage

bcp_match_language(preferences, available, default = NULL)

Arguments

preferences

A character vector of BCP 47 language tags representing the caller's ordered preferences, from most to least preferred.

available

A character vector of BCP 47 language tags that are available to choose from.

default

The value to return when no preference matches any available tag. Defaults to NULL.

Value

The best-matching tag from available, or default if no match is found. Matching is case-insensitive; the returned value preserves the original casing from available.

Examples

bcp_match_language(c('en-US', 'fr'), c('en', 'fr-FR', 'de'))
#> [1] "en"
bcp_match_language('zh-Hans-CN', c('zh-TW', 'zh-Hans', 'en'))
#> [1] "zh-Hans"
bcp_match_language('pt-BR', c('fr', 'de'), default = 'en')
#> [1] "en"