mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
72 lines
2.3 KiB
Go
72 lines
2.3 KiB
Go
package localization
|
|
|
|
import (
|
|
"bugulma/backend/internal/domain"
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// GetFieldValue is a generic function to get a field value from an entity using its handler
|
|
func GetFieldValue[T any](entity T, field string, handler domain.EntityHandler[T]) string {
|
|
return handler.GetFieldValue(entity, field)
|
|
}
|
|
|
|
// GetEntityID is a generic function to get an entity ID using its handler
|
|
func GetEntityID[T any](entity T, handler domain.EntityHandler[T]) string {
|
|
return handler.GetEntityID(entity)
|
|
}
|
|
|
|
// LoadEntities is a generic function to load entities using their handler
|
|
func LoadEntities[T any](db *gorm.DB, handler domain.EntityHandler[T], options LoadOptions) ([]T, error) {
|
|
return handler.LoadEntities(db, domain.EntityLoadOptions{IncludeAllSites: options.IncludeAllSites})
|
|
}
|
|
|
|
// FindExistingTranslationInDB searches for existing translations in the database
|
|
// This is a generic version that works with any entity type
|
|
func FindExistingTranslationInDB[T any](db *gorm.DB, entityType, field, targetLocale, russianText string, handler domain.EntityHandler[T]) string {
|
|
if russianText == "" {
|
|
return ""
|
|
}
|
|
|
|
normalized := fmt.Sprintf("%%%s%%", russianText)
|
|
|
|
// Query: Find entities of the same type with similar Russian text in the same field
|
|
// that already have a translation to the target locale
|
|
query := fmt.Sprintf(`
|
|
SELECT l.value
|
|
FROM localizations l
|
|
WHERE l.entity_type = '%s'
|
|
AND l.field = '%s'
|
|
AND l.locale = '%s'
|
|
AND EXISTS (
|
|
SELECT 1 FROM localizations l2
|
|
WHERE l2.entity_type = l.entity_type
|
|
AND l2.entity_id = l.entity_id
|
|
AND l2.field = l.field
|
|
AND l2.locale = 'ru'
|
|
AND l2.value LIKE '%s'
|
|
)
|
|
LIMIT 1
|
|
`, entityType, field, targetLocale, normalized)
|
|
|
|
var translation string
|
|
err := db.Raw(query).Scan(&translation).Error
|
|
if err != nil || translation == "" {
|
|
return ""
|
|
}
|
|
|
|
return translation
|
|
}
|
|
|
|
// GetRussianContent is a generic wrapper that gets field values from entities
|
|
func GetRussianContent[T any](entity T, field string, handler domain.EntityHandler[T]) string {
|
|
return handler.GetFieldValue(entity, field)
|
|
}
|
|
|
|
// HasRussianContent checks if an entity field has content
|
|
func HasRussianContent[T any](entity T, field string, handler domain.EntityHandler[T]) bool {
|
|
value := handler.GetFieldValue(entity, field)
|
|
return value != ""
|
|
}
|
|
|