✨ Detect language in controller layer
This commit is contained in:
parent
96b36c1db4
commit
d4dfa810d1
@ -49,6 +49,7 @@ func createArticle(c *fiber.Ctx) error {
|
|||||||
item := models.Post{
|
item := models.Post{
|
||||||
Type: models.PostTypeArticle,
|
Type: models.PostTypeArticle,
|
||||||
Body: bodyMapping,
|
Body: bodyMapping,
|
||||||
|
Language: services.DetectLanguage(data.Content),
|
||||||
Tags: data.Tags,
|
Tags: data.Tags,
|
||||||
Categories: data.Categories,
|
Categories: data.Categories,
|
||||||
IsDraft: data.IsDraft,
|
IsDraft: data.IsDraft,
|
||||||
@ -117,6 +118,7 @@ func editArticle(c *fiber.Ctx) error {
|
|||||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||||
|
|
||||||
item.Body = bodyMapping
|
item.Body = bodyMapping
|
||||||
|
item.Language = services.DetectLanguage(data.Content)
|
||||||
item.Tags = data.Tags
|
item.Tags = data.Tags
|
||||||
item.Categories = data.Categories
|
item.Categories = data.Categories
|
||||||
item.IsDraft = data.IsDraft
|
item.IsDraft = data.IsDraft
|
||||||
|
@ -51,6 +51,7 @@ func createStory(c *fiber.Ctx) error {
|
|||||||
item := models.Post{
|
item := models.Post{
|
||||||
Type: models.PostTypeStory,
|
Type: models.PostTypeStory,
|
||||||
Body: bodyMapping,
|
Body: bodyMapping,
|
||||||
|
Language: services.DetectLanguage(data.Content),
|
||||||
Tags: data.Tags,
|
Tags: data.Tags,
|
||||||
Categories: data.Categories,
|
Categories: data.Categories,
|
||||||
PublishedAt: data.PublishedAt,
|
PublishedAt: data.PublishedAt,
|
||||||
@ -137,6 +138,7 @@ func editStory(c *fiber.Ctx) error {
|
|||||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||||
|
|
||||||
item.Body = bodyMapping
|
item.Body = bodyMapping
|
||||||
|
item.Language = services.DetectLanguage(data.Content)
|
||||||
item.Tags = data.Tags
|
item.Tags = data.Tags
|
||||||
item.Categories = data.Categories
|
item.Categories = data.Categories
|
||||||
item.PublishedAt = data.PublishedAt
|
item.PublishedAt = data.PublishedAt
|
||||||
|
@ -5,14 +5,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DetectLanguage(content *string) string {
|
func DetectLanguage(content string) string {
|
||||||
if content != nil {
|
|
||||||
detector := lingua.NewLanguageDetectorBuilder().
|
detector := lingua.NewLanguageDetectorBuilder().
|
||||||
FromLanguages(lingua.AllLanguages()...).
|
FromLanguages(lingua.AllLanguages()...).
|
||||||
Build()
|
Build()
|
||||||
if lang, ok := detector.DetectLanguageOf(*content); ok {
|
if lang, ok := detector.DetectLanguageOf(content); ok {
|
||||||
return strings.ToLower(lang.String())
|
return strings.ToLower(lang.String())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
|
@ -218,6 +218,10 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||||
|
if !item.IsDraft && item.PublishedAt == nil {
|
||||||
|
item.PublishedAt = lo.ToPtr(time.Now())
|
||||||
|
}
|
||||||
|
|
||||||
item, err := EnsurePostCategoriesAndTags(item)
|
item, err := EnsurePostCategoriesAndTags(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return item, err
|
return item, err
|
||||||
|
Loading…
Reference in New Issue
Block a user