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"
|
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
|
// English falls back to phonetic "a"/"an" heuristics. Locale grammar data
|
||||||
// can override this with language-specific article forms.
|
// can override this with language-specific article forms.
|
||||||
//
|
//
|
||||||
|
// Use ArticlePhrase when you want the noun phrase prefixed with the article.
|
||||||
|
//
|
||||||
// Article("file") // "a"
|
// Article("file") // "a"
|
||||||
// Article("error") // "an"
|
// Article("error") // "an"
|
||||||
// Article("user") // "a" (sounds like "yoo-zer")
|
// Article("user") // "a" (sounds like "yoo-zer")
|
||||||
|
|
@ -558,6 +560,11 @@ func Article(word string) string {
|
||||||
return "a"
|
return "a"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ArticleToken is an explicit alias for Article.
|
||||||
|
func ArticleToken(word string) string {
|
||||||
|
return Article(word)
|
||||||
|
}
|
||||||
|
|
||||||
func articleForCurrentLanguage(lowerWord, originalWord string) (string, bool) {
|
func articleForCurrentLanguage(lowerWord, originalWord string) (string, bool) {
|
||||||
lang := currentLangForGrammar()
|
lang := currentLangForGrammar()
|
||||||
data := grammarDataForLang(lang)
|
data := grammarDataForLang(lang)
|
||||||
|
|
@ -849,7 +856,7 @@ func ArticlePhrase(word string) string {
|
||||||
return prefixWithArticle(article, word)
|
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,
|
// For languages such as French, this respects gendered articles, plural forms,
|
||||||
// and elision rules when grammar data is available.
|
// and elision rules when grammar data is available.
|
||||||
func DefiniteArticle(word string) string {
|
func DefiniteArticle(word string) string {
|
||||||
|
|
@ -869,6 +876,11 @@ func DefiniteArticle(word string) string {
|
||||||
return "the"
|
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.
|
// DefinitePhrase prefixes a noun phrase with the correct definite article.
|
||||||
func DefinitePhrase(word string) string {
|
func DefinitePhrase(word string) string {
|
||||||
word = core.Trim(word)
|
word = core.Trim(word)
|
||||||
|
|
@ -949,8 +961,10 @@ func TemplateFuncs() template.FuncMap {
|
||||||
"plural": Pluralize,
|
"plural": Pluralize,
|
||||||
"pluralForm": PluralForm,
|
"pluralForm": PluralForm,
|
||||||
"article": ArticlePhrase,
|
"article": ArticlePhrase,
|
||||||
|
"articleToken": ArticleToken,
|
||||||
"articlePhrase": ArticlePhrase,
|
"articlePhrase": ArticlePhrase,
|
||||||
"definiteArticle": DefiniteArticle,
|
"definiteArticle": DefiniteArticle,
|
||||||
|
"definiteToken": DefiniteToken,
|
||||||
"definite": DefinitePhrase,
|
"definite": DefinitePhrase,
|
||||||
"definitePhrase": DefinitePhrase,
|
"definitePhrase": DefinitePhrase,
|
||||||
"quote": Quote,
|
"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) {
|
func TestArticleFrenchLocale(t *testing.T) {
|
||||||
prev := Default()
|
prev := Default()
|
||||||
svc, err := New()
|
svc, err := New()
|
||||||
|
|
@ -1159,7 +1168,7 @@ func TestTemplateFuncs_Article(t *testing.T) {
|
||||||
t.Fatalf("template article = %q, want %q", got, want)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("Parse() alias helpers failed: %v", err)
|
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)
|
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)
|
t.Fatalf("template article aliases = %q, want %q", got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue