fix(i18n): add explicit article token aliases
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
495e977a6f
commit
cc66b3d049
2 changed files with 27 additions and 4 deletions
18
grammar.go
18
grammar.go
|
|
@ -519,10 +519,12 @@ func applyRegularPlural(noun string) string {
|
|||
return noun + "s"
|
||||
}
|
||||
|
||||
// Article returns the appropriate article for the current language.
|
||||
// Article returns the appropriate article token for the current language.
|
||||
// English falls back to phonetic "a"/"an" heuristics. Locale grammar data
|
||||
// can override this with language-specific article forms.
|
||||
//
|
||||
// Use ArticlePhrase when you want the noun phrase prefixed with the article.
|
||||
//
|
||||
// Article("file") // "a"
|
||||
// Article("error") // "an"
|
||||
// Article("user") // "a" (sounds like "yoo-zer")
|
||||
|
|
@ -558,6 +560,11 @@ func Article(word string) string {
|
|||
return "a"
|
||||
}
|
||||
|
||||
// ArticleToken is an explicit alias for Article.
|
||||
func ArticleToken(word string) string {
|
||||
return Article(word)
|
||||
}
|
||||
|
||||
func articleForCurrentLanguage(lowerWord, originalWord string) (string, bool) {
|
||||
lang := currentLangForGrammar()
|
||||
data := grammarDataForLang(lang)
|
||||
|
|
@ -849,7 +856,7 @@ func ArticlePhrase(word string) string {
|
|||
return prefixWithArticle(article, word)
|
||||
}
|
||||
|
||||
// DefiniteArticle returns the language-specific definite article for a word.
|
||||
// DefiniteArticle returns the language-specific definite article token for a word.
|
||||
// For languages such as French, this respects gendered articles, plural forms,
|
||||
// and elision rules when grammar data is available.
|
||||
func DefiniteArticle(word string) string {
|
||||
|
|
@ -869,6 +876,11 @@ func DefiniteArticle(word string) string {
|
|||
return "the"
|
||||
}
|
||||
|
||||
// DefiniteToken is an explicit alias for DefiniteArticle.
|
||||
func DefiniteToken(word string) string {
|
||||
return DefiniteArticle(word)
|
||||
}
|
||||
|
||||
// DefinitePhrase prefixes a noun phrase with the correct definite article.
|
||||
func DefinitePhrase(word string) string {
|
||||
word = core.Trim(word)
|
||||
|
|
@ -949,8 +961,10 @@ func TemplateFuncs() template.FuncMap {
|
|||
"plural": Pluralize,
|
||||
"pluralForm": PluralForm,
|
||||
"article": ArticlePhrase,
|
||||
"articleToken": ArticleToken,
|
||||
"articlePhrase": ArticlePhrase,
|
||||
"definiteArticle": DefiniteArticle,
|
||||
"definiteToken": DefiniteToken,
|
||||
"definite": DefinitePhrase,
|
||||
"definitePhrase": DefinitePhrase,
|
||||
"quote": Quote,
|
||||
|
|
|
|||
|
|
@ -354,6 +354,15 @@ func TestArticle(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestArticleTokenAliases(t *testing.T) {
|
||||
if got, want := ArticleToken("apple"), Article("apple"); got != want {
|
||||
t.Fatalf("ArticleToken(apple) = %q, want %q", got, want)
|
||||
}
|
||||
if got, want := DefiniteToken("apple"), DefiniteArticle("apple"); got != want {
|
||||
t.Fatalf("DefiniteToken(apple) = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArticleFrenchLocale(t *testing.T) {
|
||||
prev := Default()
|
||||
svc, err := New()
|
||||
|
|
@ -1159,7 +1168,7 @@ func TestTemplateFuncs_Article(t *testing.T) {
|
|||
t.Fatalf("template article = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
tmpl, err = template.New("").Funcs(TemplateFuncs()).Parse(`{{articlePhrase "apple"}}|{{definitePhrase "apple"}}`)
|
||||
tmpl, err = template.New("").Funcs(TemplateFuncs()).Parse(`{{articleToken "apple"}}|{{articlePhrase "apple"}}|{{definiteToken "apple"}}|{{definitePhrase "apple"}}`)
|
||||
if err != nil {
|
||||
t.Fatalf("Parse() alias helpers failed: %v", err)
|
||||
}
|
||||
|
|
@ -1169,7 +1178,7 @@ func TestTemplateFuncs_Article(t *testing.T) {
|
|||
t.Fatalf("Execute() alias helpers failed: %v", err)
|
||||
}
|
||||
|
||||
if got, want := buf.String(), "an apple|the apple"; got != want {
|
||||
if got, want := buf.String(), "an|an apple|the|the apple"; got != want {
|
||||
t.Fatalf("template article aliases = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue