i18nm

package
v0.0.0-...-2fd717b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2026 License: AGPL-3.0 Imports: 20 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSaveModelTranslationsOptions = SaveModelTranslationsOptions{
	FormNamePrefix: formbuilder.FormInputNamePrefixDefault,
	ContentType:    map[string]string{},
	Project:        "",
	// contains filtered or unexported fields
}

DefaultSaveModelTranslationsOptions is the default options for SaveModelTranslations.

View Source
var HideTagFields = map[string][]string{
	`official_common_article`: []string{`content`},
}

Functions

func AutoTranslate

func AutoTranslate(ctx echo.Context, table string, queryAll bool, translateAll bool, eqID uint64, gtID uint64, chunks int) (uint64, error)

AutoTranslate 自动翻译指定表中的内容

参数:

ctx: echo上下文
table: 要翻译的表名
queryAll: 是否查询所有记录(忽略已翻译记录)
translateAll: 是否强制翻译所有内容
eqID: 指定ID等于该值的记录
gtID: 指定ID大于该值的记录
chunks: 每次查询的记录数限制

返回值:

uint64: 最后处理的记录ID
error: 错误信息

功能:

  1. 检查表是否存在
  2. 获取表中需要翻译的字段
  3. 构建查询条件并执行查询
  4. 对查询结果进行批量翻译
  5. 支持内容类型(contype)的特殊处理
  6. 支持分块查询和翻译

func DefaultOriginalTextPickout

func DefaultOriginalTextPickout(table string, fieldName string, originalText string) (string, func(translatedText string) string)

func DeleteModelTranslations

func DeleteModelTranslations(ctx echo.Context, mdl Model, id uint64) error

DeleteModelTranslations deletes all translations associated with a specific model instance. It removes both the resource entries and their corresponding translations from the database. Parameters:

mdl - the model instance containing context and table information
id  - the ID of the model instance whose translations should be deleted

Returns:

error - any error encountered during the deletion process

func GetAllTranslations

func GetAllTranslations(ctx echo.Context, table string, ids []uint64, columns ...string) map[uint64]map[string]map[string]string

GetAllTranslations 获取指定表和ID集合的多语言翻译数据

参数:

  • ctx: echo上下文对象
  • table: 数据库表名
  • ids: 需要查询的ID集合
  • columns: 可选字段列表,指定要获取的列

返回值:

  • 三层嵌套map结构: ID -> 语言代码 -> 字段名 -> 翻译值
  • 如果未启用多语言功能,返回空map

func GetColumnDefaultLangText

func GetColumnDefaultLangText(ctx echo.Context, table string, column string, text string) (string, error)

GetColumnDefaultLangText retrieves the default language text for a specific column in a table. It first gets the resource row ID using GetResourceRowID, then queries the database for the column value. Returns the text in default language or empty string if not found, along with any error encountered.

func GetModelRowID

func GetModelRowID(ctx echo.Context, mdl Model, column string, text string) (uint64, error)

GetModelRowID retrieves the row ID of a model based on the specified column and text in the current language context. It first gets the resource ID from the model and column, then looks up the translation matching the given text. Returns the row ID if found, or 0 with an error if the resource or translation cannot be found.

func GetModelTranslations

func GetModelTranslations(ctx echo.Context, mdl Model, columns ...string)

GetModelTranslations retrieves translations for a model instance by its ID. It returns translations as a map where each key is a field name and the value is the translated text. If the model lacks an ID field, it does nothing. If the context is nil, it uses the model's context. It fetches translations using the model's context and table name. If translations are found, it applies them to the model instance using the FromRow method.

func GetModelTranslationsByIDs

func GetModelTranslationsByIDs(ctx echo.Context, mdl Model, ids []uint64, columns ...string) map[uint64]map[string]string

GetModelTranslationsByIDs retrieves translations for multiple model instances by their IDs. It returns a map where each key is a model ID and the value is another map of language translations. The translations are fetched using the model's context and table name.

func GetModelsAllTranslations

func GetModelsAllTranslations[T Model](ctx echo.Context, models []T, columns ...string) []map[string]echo.H

GetModelsAllTranslations 获取多个模型的所有翻译数据

参数:

ctx - echo 上下文对象
models - 模型实例切片
columns - 可选的要获取的字段列表

返回值:

返回一个切片,每个元素是一个映射,包含对应模型的多语言翻译数据
映射的键是语言代码,值是字段到翻译文本的映射

func GetModelsTranslations

func GetModelsTranslations[T Model](ctx echo.Context, models []T, columns ...string) []T

GetModelsTranslations retrieves translations for a slice of models and applies them to each model. It takes a slice of models as input and returns the same slice with translations applied. For each model, it extracts the ID, fetches translations using GetModelTranslations, and updates the model fields with the translated values. If the input slice is empty or any model lacks an ID field, it returns the original slice unchanged.

func GetResourceRowID

func GetResourceRowID(ctx echo.Context, table string, column string, text string) (uint64, error)

GetResourceRowID retrieves the row ID for a given internationalized text in the specified table and column. It first looks up the resource ID for the table-column pair, then finds the translation matching the given text and language. Returns the row ID (0 if not found) and any error that occurred during the lookup.

func GetRowID

func GetRowID(mdl Model) uint64

GetRowID returns the ID of a model instance.

func GetTranslations

func GetTranslations(ctx echo.Context, table string, ids []uint64, columns ...string) map[uint64]map[string]string

GetTranslations retrieves translations for the specified table and row IDs. It returns a map where keys are row IDs and values are maps of field names to translated texts. The translations are filtered by the current request language and the specified table prefix.

func Initialize

func Initialize(ctx echo.Context) error

Initialize scans all database fields marked as multilingual and ensures they have corresponding entries in the i18n resource table. It creates new i18n resource records for any multilingual fields that don't already exist in the resource table. Returns any error encountered during the process.

func IsDefaultLang

func IsDefaultLang(ctx echo.Context) bool

IsDefaultLang checks if the current language in context is the default language

func IsMultilingual

func IsMultilingual() bool

IsMultilingual returns true if the application supports multiple languages based on the configuration

func LangDefault

func LangDefault() string

LangDefault 返回配置文件中设置的默认语言代码

func LangIsDefault

func LangIsDefault(lang string) bool

LangIsDefault checks if the given language is the default language configured in the system.

func ListenTable

func ListenTable()

ListenTable listen table

func OptionAllowForceTranslate

func OptionAllowForceTranslate(allowForceTranslate func(echo.Context) bool) func(*SaveModelTranslationsOptions)

OptionAllowForceTranslate 返回一个配置函数,用于设置是否允许强制翻译 allowForceTranslate: 是否允许强制覆盖现有翻译

func OptionAutoTranslate

func OptionAutoTranslate(autoTranslate bool) func(*SaveModelTranslationsOptions)

OptionAutoTranslate sets whether translations should be automatically generated when missing

func OptionContentType

func OptionContentType(fieldName string, contentType string) func(*SaveModelTranslationsOptions)

OptionContentType returns a function that sets the content type for the specified field in SaveModelTranslationsOptions. The returned function can be used as an option when saving model translations.

func OptionContentTypes

func OptionContentTypes(ct map[string]string) func(*SaveModelTranslationsOptions)

OptionContentTypes returns a function that sets content types for multiple fields in SaveModelTranslationsOptions. The input map associates field names with their corresponding content types.

func OptionDebugFormValue

func OptionDebugFormValue(debugFormValue bool) func(*SaveModelTranslationsOptions)

OptionDebugFormValue returns a function option that sets whether to store debug form values in the form context. If true, will store the form values in the form context after saving model translations. This is useful for debugging purposes, especially when the form value is not visible in the request body.

func OptionForceTranslate

func OptionForceTranslate(forceTranslate bool) func(*SaveModelTranslationsOptions)

OptionTranslator sets the translator function for SaveModelTranslationsOptions

func OptionFormNamePrefix

func OptionFormNamePrefix(formNamePrefix string) func(*SaveModelTranslationsOptions)

OptionFormNamePrefix sets the form name prefix for SaveModelTranslationsOptions

func OptionLanguageValues

func OptionLanguageValues(languageValues map[string]map[string]string) func(*SaveModelTranslationsOptions)

OptionLanguageValues returns a function option that sets the language values for SaveModelTranslationsOptions. The input map associates language codes with their corresponding corresponding values. The language values are used to populate the form with language-specific values. For example, the values can be used to populate the form with the default translation values for each language.

func OptionOriginalTextPickout

func OptionOriginalTextPickout(originalTextPickout OriginalTextPicker) func(*SaveModelTranslationsOptions)

OptionOriginalTextPickout returns a function option that sets the function for picking out the original text. The input function is used to determine the original text for a translation.

func OptionProject

func OptionProject(project string) func(*SaveModelTranslationsOptions)

OptionProject sets the project name for SaveModelTranslationsOptions

func OptionResourceIDsByField

func OptionResourceIDsByField(resourceIDsByField map[string]uint) func(*SaveModelTranslationsOptions)

OptionResourceIDsByField returns a function option that sets the resource IDs for each field. The input map associates field names with their corresponding resource IDs. The resource IDs are used to retrieve translations for the associated fields.

func OptionTranslator

func OptionTranslator(translator Translator) func(*SaveModelTranslationsOptions)

OptionTranslator sets the translator function for SaveModelTranslationsOptions

func OptionTrimOverflowText

func OptionTrimOverflowText(trimOverflowText bool) func(*SaveModelTranslationsOptions)

OptionTrimOverflowText returns a function option that sets whether to trim overflow text when saving translations trimOverflowText: if true, will trim text that exceeds the field length limit

func RegisterHideTagFields

func RegisterHideTagFields(table string, fields ...string)

func SaveModelTranslations

func SaveModelTranslations(ctx echo.Context, mdl Model, id uint64, options ...func(*SaveModelTranslationsOptions)) error

SaveModelTranslations saves multilingual field translations for a model. It processes all multilingual fields of the given model, creating/updating translations for each language configured in the system. Parameters:

  • mdl: The model instance containing multilingual fields
  • id: The row ID of the model to save translations for
  • formNamePrefix: Optional prefix for form field names (defaults to "Language")

Returns:

  • error: Any error encountered during the save process
func Search(ctx echo.Context, table string, keyword string, param *factory.Param, columns ...string) error

Search performs a keyword search on i18n translations for the specified table and resource IDs. It joins with the translations table to find matches in the specified language. Parameters:

  • ctx: echo context containing request information
  • table: name of the resource table to search
  • keyword: search term to look for in translations
  • param: factory parameters for building the query
  • columns: optional columns to select from the resource table

Returns:

  • error if any occurs during the search operation

func SearchModel

func SearchModel(ctx echo.Context, mdl Model, keyword string, param *factory.Param, columns ...string) error

SearchModel searches for records in the specified model using the given keyword and parameters. It delegates the actual search operation to the Search function with the model's short name. ctx: Echo context for the request mdl: The model to search in keyword: The search term param: Additional search parameters columns: Optional columns to search in Returns an error if the search fails

func SetModelTranslationsToForm

func SetModelTranslationsToForm(ctx echo.Context, mdl Model, id uint64, formNamePrefix ...string) error

SetModelTranslationsToForm sets translation texts from a model to form fields. It retrieves translations for the given model ID and populates the form with language-specific values. The form field names are prefixed with the given prefix (default "Language") in the format: prefix[lang][field]. Returns any error encountered during the operation.

func SetSearchDefaultLangColumns

func SetSearchDefaultLangColumns(ctx echo.Context, enable bool)

SetSearchDefaultLangColumns sets whether to enable default language columns in search operations. The setting is stored in the echo.Context's internal data.

func SetTranstationsTTL

func SetTranstationsTTL(ctx echo.Context, ttl int64)

SetTranstationsTTL sets the TTL (Time To Live) for translations in the given echo context. The ttl parameter specifies the duration in seconds that translations should be cached.

func TranslationsToMapByIndex

func TranslationsToMapByIndex(langsets []map[string]echo.H, index int) echo.H

TranslationsToMapByIndex converts a slice of language sets to a map. It takes a slice of language sets and an index as input and returns a map where each key is a field name and the value is the translated text for the given index. The map keys are in the format: "Language[lang][field]" where lang is the language code and field is the field name.

func TranslationsToMaps

func TranslationsToMaps(langsets []map[string]echo.H) []echo.H

TranslationsToMaps converts a slice of language sets to a slice of maps. Each map in the output slice contains translations for a single language set. The map keys are in the format: "Language[lang][field]" where lang is the language code and field is the field name.

Types

type Model

type Model interface {
	Short_() string
	GetField(string) interface{}
	FromRow(map[string]interface{})
}

Model is an interface for model instances.

type OriginalTextPicker

type OriginalTextPicker = func(table string, fieldName string, originalValue string) (string, func(translatedText string) string)

OriginalTextPicker is a function that picks out the original text from the content.

type SaveModelTranslationsOptions

type SaveModelTranslationsOptions struct {
	FormNamePrefix      string
	ContentType         map[string]string             // map[fieldName]contentType
	LanguageValues      *map[string]map[string]string // map[langCode]map[fieldName]value
	Project             string
	AutoTranslate       *bool
	ForceTranslate      *bool
	TrimOverflowText    *bool
	AllowForceTranslate func(echo.Context) bool
	// contains filtered or unexported fields
}

SaveModelTranslationsOptions is a struct that holds options for saving model translations.

func (*SaveModelTranslationsOptions) SetAllowForceTranslate

func (o *SaveModelTranslationsOptions) SetAllowForceTranslate(allowForceTranslate func(echo.Context) bool)

SetAllowForceTranslate sets whether to allow force translation updates

func (*SaveModelTranslationsOptions) SetAutoTranslate

func (o *SaveModelTranslationsOptions) SetAutoTranslate(autoTranslate bool)

SetAutoTranslate sets whether translations should be automatically generated when missing

func (*SaveModelTranslationsOptions) SetContentType

func (o *SaveModelTranslationsOptions) SetContentType(fieldName string, contentType string)

SetContentType sets the content type for the specified field name

func (*SaveModelTranslationsOptions) SetDebugFormValue

func (o *SaveModelTranslationsOptions) SetDebugFormValue(debugFormValue bool)

SetDebugFormValue sets whether to store debug form values in the form context. If true, will store the form values in the form context after saving model translations. This is useful for debugging purposes, especially when the form value is not visible in the request body.

func (*SaveModelTranslationsOptions) SetDefaults

func (o *SaveModelTranslationsOptions) SetDefaults()

SetDefaults sets default values for SaveModelTranslationsOptions fields when they are not explicitly set. It copies values from DefaultSaveModelTranslationsOptions to the receiver for empty/zero fields including FormNamePrefix, Project, translator, AutoTranslate and ContentType.

func (*SaveModelTranslationsOptions) SetForceTranslate

func (o *SaveModelTranslationsOptions) SetForceTranslate(forceTranslate bool)

SetForceTranslate sets whether to force translation updates regardless of existing translations

func (*SaveModelTranslationsOptions) SetFormNamePrefix

func (o *SaveModelTranslationsOptions) SetFormNamePrefix(formNamePrefix string)

SetFormNamePrefix sets the prefix for form field names

func (*SaveModelTranslationsOptions) SetLanguageValues

func (o *SaveModelTranslationsOptions) SetLanguageValues(languageValues map[string]map[string]string)

SetLanguageValues sets the language values for SaveModelTranslationsOptions. The input map associates language codes with their corresponding corresponding corresponding values. The language values are used to populate the form with language-specific values. For example, the values can be used to populate the form with the default translation values for each language.

func (*SaveModelTranslationsOptions) SetOriginalTextPickout

func (o *SaveModelTranslationsOptions) SetOriginalTextPickout(originalTextPickout OriginalTextPicker)

SetOriginalTextPickout sets the function for picking out the original text

func (*SaveModelTranslationsOptions) SetProject

func (o *SaveModelTranslationsOptions) SetProject(project string)

SetProject sets the project name for the translation options

func (*SaveModelTranslationsOptions) SetResourceIDsByField

func (o *SaveModelTranslationsOptions) SetResourceIDsByField(resourceIDsByField map[string]uint)

SetResourceIDsByField sets the resource IDs for each field

func (*SaveModelTranslationsOptions) SetTranslator

func (o *SaveModelTranslationsOptions) SetTranslator(translator Translator)

SetTranslator sets the translator function for converting field values

func (*SaveModelTranslationsOptions) SetTrimOverflowText

func (o *SaveModelTranslationsOptions) SetTrimOverflowText(trimOverflowText bool)

SetTrimOverflowText sets whether to trim overflow text when saving translations

func (*SaveModelTranslationsOptions) Translate

func (o *SaveModelTranslationsOptions) Translate(ctx echo.Context, fieldName string, value string, originalValue string, contentType string, langCode string, originalLangCode string) (string, error)

Translate translates the given field value for the specified language code. If a translator function is set in options, it will be used for translation. Returns the translated value or the original value if no translator is set.

type Translator

type Translator = func(ctx echo.Context, fieldName string, value string, originalValue string, contentType string, langCode string, originalLangCode string) (string, error)

Translator is a function that translates a field value to the specified language code.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL