tag: implement translations

This commit is contained in:
perennial
2024-10-21 18:45:57 +11:00
parent 0ac7785219
commit b591eb94a9
4 changed files with 56 additions and 29 deletions
+4 -1
View File
@@ -53,7 +53,10 @@
{{- range .Data.RelatedTags }}
<div class="d-inline-block mb-2 me-2">
<small>
<a href="/tags/{{ . }}" class="text-primary-emphasis fw-semibold text-decoration-none text-wrap p-0 mb-0">#{{ . }}</a>
<a href="/tags/{{ escapeString(.Name) }}" class="text-primary-emphasis fw-semibold text-decoration-none text-wrap p-0 mb-0">#{{ .Name }}</a>
{{- if .TranslatedName }}
<small class="text-body-secondary fw-medium">({{ .TranslatedName }})</small>
{{- end }}
</small>
</div>
{{- end }}
+15 -15
View File
@@ -86,7 +86,7 @@ type Image struct {
IllustType int
}
type Tag struct {
type ArtworkTag struct {
Name string `json:"tag"`
TranslatedName string `json:"translation"`
}
@@ -129,18 +129,18 @@ type Illust struct {
UserAccount string `json:"userAccount"`
Date time.Time `json:"uploadDate"`
Images []Image
Tags []Tag `json:"tags"`
Pages int `json:"pageCount"`
Bookmarks int `json:"bookmarkCount"`
Likes int `json:"likeCount"`
Comments int `json:"commentCount"`
Views int `json:"viewCount"`
CommentDisabled int `json:"commentOff"`
SanityLevel int `json:"sl"`
XRestrict XRestrict `json:"xRestrict"`
AiType AiType `json:"aiType"`
BookmarkData any `json:"bookmarkData"`
Liked bool `json:"likeData"`
Tags []ArtworkTag `json:"tags"`
Pages int `json:"pageCount"`
Bookmarks int `json:"bookmarkCount"`
Likes int `json:"likeCount"`
Comments int `json:"commentCount"`
Views int `json:"viewCount"`
CommentDisabled int `json:"commentOff"`
SanityLevel int `json:"sl"`
XRestrict XRestrict `json:"xRestrict"`
AiType AiType `json:"aiType"`
BookmarkData any `json:"bookmarkData"`
Liked bool `json:"likeData"`
SeriesNavData struct {
SeriesType string `json:"seriesType"`
SeriesID string `json:"seriesId"`
@@ -389,9 +389,9 @@ func GetArtworkByID(auditor *audit.Auditor, r *http.Request, id string, full boo
}
// Store translated tags and preserve the original language form for international users.
var tagsList []Tag
var tagsList []ArtworkTag
for _, tag := range tags.Tags {
var newTag Tag
var newTag ArtworkTag
newTag.Name = tag.Tag
newTag.TranslatedName = tag.Translation["en"]
+36 -13
View File
@@ -25,18 +25,19 @@ type SearchArtworks struct {
Total int `json:"total"`
}
// TODO: find a better name for this struct
type TagTag struct {
Name string `json:"tag"`
TranslatedName string `json:"translation"`
}
type SearchResult struct {
Artworks SearchArtworks
Popular struct {
Permanent []ArtworkBrief `json:"permanent"`
Recent []ArtworkBrief `json:"recent"`
} `json:"popular"`
RelatedTags []string `json:"relatedTags"`
}
type TagWithTranslation struct {
Name string `json:"relatedTags"`
TranslatedName string `json:"tagTranslation"`
RelatedTags []TagTag `json:"relatedTags"`
}
type SearchPageSettings struct {
@@ -106,31 +107,53 @@ func GetSearch(auditor *audit.Auditor, r *http.Request, settings SearchPageSetti
}
response = session.ProxyImageUrl(r, response)
// IDK how to do better than this lol
// Replace "illust", "manga", and "illustManga" with "works" in the JSON response
//
// VnPower: IDK how to do better than this lol
temp := strings.ReplaceAll(string(response), `"illust"`, `"works"`)
temp = strings.ReplaceAll(temp, `"manga"`, `"works"`)
temp = strings.ReplaceAll(temp, `"illustManga"`, `"works"`)
// Define an intermediate struct to unmarshal the JSON response
var resultRaw struct {
*SearchResult
ArtworksRaw json.RawMessage `json:"works"`
Popular struct {
Permanent []ArtworkBrief `json:"permanent"`
Recent []ArtworkBrief `json:"recent"`
} `json:"popular"`
RelatedTags []string `json:"relatedTags"`
TagTranslation map[string]struct{ En string } `json:"tagTranslation"`
}
var artworks SearchArtworks
var result *SearchResult
var artworks SearchArtworks
var result SearchResult
// Unmarshal the modified JSON response into the intermediate struct
err = json.Unmarshal([]byte(temp), &resultRaw)
if err != nil {
return nil, err
}
result = resultRaw.SearchResult
// Unmarshal the artworks data
err = json.Unmarshal([]byte(resultRaw.ArtworksRaw), &artworks)
if err != nil {
return nil, err
}
// Assign artworks and popular data to the result
result.Artworks = artworks
result.Popular = resultRaw.Popular
return result, nil
// Process related tags and translations to populate the RelatedTags slice
for _, tagName := range resultRaw.RelatedTags {
tag := TagTag{
Name: tagName,
}
if translation, exists := resultRaw.TagTranslation[tagName]; exists {
tag.TranslatedName = translation.En
}
result.RelatedTags = append(result.RelatedTags, tag)
}
return &result, nil
}
+1
View File
@@ -519,6 +519,7 @@
"assets/views/tag.jet.html:tLDBgUWw9-g": "Safe",
"assets/views/tag.jet.html:vQknITzlB0I": "Search options",
"assets/views/tag.jet.html:vUR6oAGwbwE": "Order",
"assets/views/tag.jet.html:xd9cjh9m0Go": "({{ .TranslatedName }})",
"assets/views/tag.jet.html:yHft19aQloM": "Minimum image width",
"assets/views/tag.jet.html:yxDtQeZtxgU": "Portrait",
"assets/views/unauthorized.jet.html:Q84lx6mjuV0": "Unauthorized",