Skip to content

Commit

Permalink
Update foliate-js
Browse files Browse the repository at this point in the history
Metadata now contains localized strings.

See johnfactotum/foliate-js@541bc07
  • Loading branch information
johnfactotum committed Sep 25, 2024
1 parent cce868f commit f1a1602
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
22 changes: 15 additions & 7 deletions src/book-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import { gettext as _ } from 'gettext'
import * as utils from './utils.js'
import * as format from './format.js'

export const formatLanguageMap = x => {
if (!x) return ''
if (typeof x === 'string') return x
const keys = Object.keys(x)
return /*x[format.matchLocales(keys)[0]] ??*/ x[keys[0]]
}

const formatContributors = contributors => Array.isArray(contributors)
? format.list(contributors.map(contributor =>
typeof contributor === 'string' ? contributor : contributor.name))
: contributors
typeof contributor === 'string' ? contributor
: formatLanguageMap(contributor?.name)))
: typeof contributors === 'string' ? contributors
: formatLanguageMap(contributors?.name)

export const formatAuthors = metadata => metadata?.author
? formatContributors(metadata.author)
Expand Down Expand Up @@ -46,8 +55,7 @@ const makeSubjectBox = subject => {
xalign: 0,
wrap: true,
selectable: true,
label: typeof subject === 'string'
? subject : subject.name ?? subject.code,
label: formatContributors(subject) || subject?.code,
valign: Gtk.Align.START,
}), 'caption'))
return box
Expand All @@ -64,14 +72,14 @@ const makeBookHeader = (metadata, pixbuf) => {
xalign: 0,
wrap: true,
selectable: true,
label: metadata.title ?? '',
label: formatLanguageMap(metadata.title),
}), 'title-2'))

if (metadata.subtitle) box.append(utils.addClass(new Gtk.Label({
xalign: 0,
wrap: true,
selectable: true,
label: metadata.subtitle,
label: formatLanguageMap(metadata.subtitle),
}), 'title-4'))

box.append(new Gtk.Label({
Expand Down Expand Up @@ -122,7 +130,7 @@ const makeBookInfoBox = (metadata, pixbuf) => {
box.append(flowbox)

for (const [title, value] of [
[_('Publisher'), metadata.publisher],
[_('Publisher'), formatContributors(metadata.publisher)],
// Translators: this is the heading for the publication date
[_('Published'), format.date(metadata.published)],
// Translators: this is the heading for the modified date
Expand Down
8 changes: 4 additions & 4 deletions src/book-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import './navbar.js'
import { AnnotationPopover, importAnnotations, exportAnnotations } from './annotations.js'
import { SelectionPopover } from './selection-tools.js'
import { ImageViewer } from './image-viewer.js'
import { formatAuthors, makeBookInfoWindow } from './book-info.js'
import { formatLanguageMap, formatAuthors, makeBookInfoWindow } from './book-info.js'
import { themes, invertTheme, themeCssProvider } from './themes.js'
import { dataStore } from './data.js'

Expand Down Expand Up @@ -725,10 +725,10 @@ export const BookViewer = GObject.registerClass({
this._top_overlay_box.hide()
this.#book = book
book.metadata ??= {}
this.root.title = book.metadata.title ?? ''
this._book_title.label = book.metadata.title ?? ''
this._book_title.label = formatLanguageMap(book.metadata.title)
this._book_author.label = formatAuthors(book.metadata)
this._book_author.visible = !!book.metadata?.author?.length
this._book_author.visible = !!this._book_author.label
this.root.title = this._book_title.label

const { language: { direction } } = reader.view
utils.setDirection(this._book_info, direction)
Expand Down
2 changes: 1 addition & 1 deletion src/foliate-js
Submodule foliate-js updated 9 files
+16 −4 README.md
+205 −143 epub.js
+8 −2 fb2.js
+6 −7 fixed-layout.js
+109 −22 paginator.js
+3 −3 pdf.js
+86 −0 quote-image.js
+17 −7 reader.js
+2 −2 search.js
7 changes: 6 additions & 1 deletion src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const getRegionEmoji = code => {
...Array.from(code.toUpperCase()).map(x => 127397 + x.charCodeAt()))
}
const displayName = new Intl.DisplayNames(locales, { type: 'language' })
export const language = code => {
const formatLangauge = code => {
if (!code) return ''
try {
const locale = new Intl.Locale(code)
Expand All @@ -101,6 +101,11 @@ export const language = code => {
return ''
}
}
export const language = lang => {
if (typeof lang === 'string') return formatLangauge(lang)
if (Array.isArray(lang)) return list(lang.map(formatLangauge))
return ''
}

const minuteFormat = new Intl.NumberFormat(locales, { style: 'unit', unit: 'minute' })
const hourFormat = new Intl.NumberFormat(locales, { style: 'unit', unit: 'hour' })
Expand Down
4 changes: 2 additions & 2 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { gettext as _ } from 'gettext'
import * as utils from './utils.js'
import * as format from './format.js'
import { exportAnnotations } from './annotations.js'
import { formatAuthors, makeBookInfoWindow } from './book-info.js'
import { formatLanguageMap, formatAuthors, makeBookInfoWindow } from './book-info.js'

import WebKit from 'gi://WebKit'
import { WebView } from './webview.js'
Expand Down Expand Up @@ -246,7 +246,7 @@ const BookItem = GObject.registerClass({
}
update(item, data, cover) {
this.#item = item
const title = data.metadata?.title ?? ''
const title = formatLanguageMap(data.metadata?.title)
this._title.text = title
this._image.load(cover?.then ? null : cover, title)
this._progress.label = format.percent(fraction(data.progress))
Expand Down
9 changes: 8 additions & 1 deletion src/reader/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ const debounce = (f, wait, immediate) => {
}
}

const formatLanguageMap = x => {
if (!x) return ''
if (typeof x === 'string') return x
const keys = Object.keys(x)
return x[keys[0]]
}

const getSelectionRange = sel => {
if (!sel.rangeCount) return
const range = sel.getRangeAt(0)
Expand Down Expand Up @@ -429,7 +436,7 @@ class Reader {
const { tocItem } = e.detail
heads.at(-1).innerText = tocItem?.label ?? ''
if (heads.length > 1)
heads[0].innerText = this.book.metadata.title
heads[0].innerText = formatLanguageMap(this.book.metadata.title)
}
if (feet) {
const { pageItem, location: { current, next, total } } = e.detail
Expand Down

0 comments on commit f1a1602

Please sign in to comment.