Skip to content

Commit

Permalink
Allow setting document locale
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Aug 1, 2024
1 parent d464155 commit 86c4b19
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.iamgio.quarkdown.document

import eu.iamgio.quarkdown.document.locale.Locale
import eu.iamgio.quarkdown.document.page.PageFormatInfo

/**
Expand All @@ -9,11 +10,14 @@ import eu.iamgio.quarkdown.document.page.PageFormatInfo
* @param name name of the document, if specified
* @param author author of the document, if specified
* @param theme theme of the document, if specified
* @param locale language of the document
* @param pageFormat format of the pages of the document
*/
data class DocumentInfo(
var type: DocumentType = DocumentType.PLAIN,
var name: String? = null,
var author: String? = null,
var locale: Locale? = null,
var theme: DocumentTheme? = null,
val pageFormat: PageFormatInfo = PageFormatInfo(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ internal class JVMLocale(private val jvmLocale: java.util.Locale) : Locale {
get() = jvmLocale.country.takeIf { it.isNotBlank() }

override val localizedName: String
get() = jvmLocale.getDisplayLanguage(jvmLocale)
get() = jvmLocale.getDisplayName(jvmLocale)

override val localizedCountryName: String?
get() = jvmLocale.getDisplayCountry(jvmLocale).takeIf { it.isNotBlank() }

override val tag: String
get() = jvmLocale.toLanguageTag()
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ interface Locale {
* For instance, `United States` for `en-US` and `Italia` for `it`.
*/
val localizedCountryName: String?

/**
* Tag of the locale.
* For instance, `en-US` for US English and `it` for Italian.
*/
val tag: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ interface LocaleLoader {
* @return [Locale] with the given tag, or `null` if not found
*/
fun fromTag(tag: String): Locale?

companion object {
/**
* Default system [LocaleLoader] implementation.
*/
val SYSTEM: LocaleLoader = JVMLocaleLoader
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HtmlPostRenderer(private val context: Context) : PostRenderer {
override fun createCodeWrapper() =
RenderWrapper.fromResourceName("/render/html-wrapper.html")
.value(TemplatePlaceholders.TITLE, context.documentInfo.name ?: "Quarkdown")
.value(TemplatePlaceholders.LANGUAGE, "en") // TODO set language
.optionalValue(TemplatePlaceholders.LANGUAGE, context.documentInfo.locale?.tag)
// "Paged" document rendering via PagesJS.
.conditional(TemplatePlaceholders.IS_PAGED, context.documentInfo.type == DocumentType.PAGED)
// "Slides" document rendering via RevealJS.
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/render/html-wrapper.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<!--suppress ALL -->
<html lang="[[LANG]]">
<html[[if:LANG]] lang="[[LANG]]"[[endif:LANG]]>
<head>
<meta charset="UTF-8">
<title>[[TITLE]]</title>
Expand Down
17 changes: 15 additions & 2 deletions core/src/test/kotlin/eu/iamgio/quarkdown/MiscTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MiscTest {
with(retriever.fromTag("en")) {
assertNotNull(this)
assertEquals("en", code)
assertEquals("en", tag)
assertEquals("English", localizedName)
assertNull(countryCode)
assertNull(localizedCountryName)
Expand All @@ -26,6 +27,7 @@ class MiscTest {
with(retriever.fromTag("it")) {
assertNotNull(this)
assertEquals("it", code)
assertEquals("it", tag)
assertEquals("italiano", localizedName)
assertNull(countryCode)
assertNull(localizedCountryName)
Expand All @@ -34,15 +36,26 @@ class MiscTest {
with(retriever.fromTag("en-US")) {
assertNotNull(this)
assertEquals("en", code)
assertEquals("English", localizedName)
assertEquals("en-US", tag)
assertEquals("English (United States)", localizedName)
assertEquals("US", countryCode)
assertEquals("United States", localizedCountryName)
}

with(retriever.fromTag("En-us")) {
assertNotNull(this)
assertEquals("en", code)
assertEquals("en-US", tag)
assertEquals("English (United States)", localizedName)
assertEquals("US", countryCode)
assertEquals("United States", localizedCountryName)
}

with(retriever.fromTag("fr-CA")) {
assertNotNull(this)
assertEquals("fr", code)
assertEquals("français", localizedName)
assertEquals("fr-CA", tag)
assertEquals("français (Canada)", localizedName)
assertEquals("CA", countryCode)
assertEquals("Canada", localizedCountryName)
}
Expand Down
24 changes: 24 additions & 0 deletions stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Document.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import eu.iamgio.quarkdown.context.MutableContext
import eu.iamgio.quarkdown.document.DocumentInfo
import eu.iamgio.quarkdown.document.DocumentTheme
import eu.iamgio.quarkdown.document.DocumentType
import eu.iamgio.quarkdown.document.locale.LocaleLoader
import eu.iamgio.quarkdown.document.page.PageMarginPosition
import eu.iamgio.quarkdown.document.page.PageOrientation
import eu.iamgio.quarkdown.document.page.PageSizeFormat
Expand All @@ -34,6 +35,7 @@ val Document: Module =
::docType,
::docName,
::docAuthor,
::docLanguage,
::theme,
::pageFormat,
::pageMarginContent,
Expand Down Expand Up @@ -115,6 +117,28 @@ fun docAuthor(
set = { this.author = it },
)

/**
* If [localeTag] is not `null`, it sets the document locale to its value.
* If it's `null`, the localized name of the current document locale is returned.
* @param localeTag (optional) well-formed, case-insensitive, locale tag to assign to the document. Example: `en-US`, `it`, `fr-CA`
* @return the localized name of the current document locale if [localeTag] is `null`
* @throws IllegalArgumentException if the locale tag is not invalid or not found
*/
@Name("doclang")
fun docLanguage(
@Injected context: Context,
@Name("locale") localeTag: String? = null,
): OutputValue<*> =
context.modifyOrEchoDocumentInfo(
localeTag,
get = { this.locale?.localizedName ?: "" },
set = {
this.locale =
LocaleLoader.SYSTEM.fromTag(it)
?: throw IllegalArgumentException("Locale $it not found")
},
)

/**
* Sets the global document theme.
* @param color (optional) color scheme to assign (searched in `resources/render/theme/color`)
Expand Down

0 comments on commit 86c4b19

Please sign in to comment.