✨ 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{
|
||||
Type: models.PostTypeArticle,
|
||||
Body: bodyMapping,
|
||||
Language: services.DetectLanguage(data.Content),
|
||||
Tags: data.Tags,
|
||||
Categories: data.Categories,
|
||||
IsDraft: data.IsDraft,
|
||||
@ -117,6 +118,7 @@ func editArticle(c *fiber.Ctx) error {
|
||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||
|
||||
item.Body = bodyMapping
|
||||
item.Language = services.DetectLanguage(data.Content)
|
||||
item.Tags = data.Tags
|
||||
item.Categories = data.Categories
|
||||
item.IsDraft = data.IsDraft
|
||||
|
@ -51,6 +51,7 @@ func createStory(c *fiber.Ctx) error {
|
||||
item := models.Post{
|
||||
Type: models.PostTypeStory,
|
||||
Body: bodyMapping,
|
||||
Language: services.DetectLanguage(data.Content),
|
||||
Tags: data.Tags,
|
||||
Categories: data.Categories,
|
||||
PublishedAt: data.PublishedAt,
|
||||
@ -137,6 +138,7 @@ func editStory(c *fiber.Ctx) error {
|
||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||
|
||||
item.Body = bodyMapping
|
||||
item.Language = services.DetectLanguage(data.Content)
|
||||
item.Tags = data.Tags
|
||||
item.Categories = data.Categories
|
||||
item.PublishedAt = data.PublishedAt
|
||||
|
@ -5,14 +5,12 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func DetectLanguage(content *string) string {
|
||||
if content != nil {
|
||||
func DetectLanguage(content string) string {
|
||||
detector := lingua.NewLanguageDetectorBuilder().
|
||||
FromLanguages(lingua.AllLanguages()...).
|
||||
Build()
|
||||
if lang, ok := detector.DetectLanguageOf(*content); ok {
|
||||
if lang, ok := detector.DetectLanguageOf(content); ok {
|
||||
return strings.ToLower(lang.String())
|
||||
}
|
||||
}
|
||||
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) {
|
||||
if !item.IsDraft && item.PublishedAt == nil {
|
||||
item.PublishedAt = lo.ToPtr(time.Now())
|
||||
}
|
||||
|
||||
item, err := EnsurePostCategoriesAndTags(item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
|
Loading…
Reference in New Issue
Block a user