Skip to content

Commit

Permalink
Accelerated translation lookup with fastmap
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathon-love committed Nov 15, 2021
1 parent 9632e83 commit 0aaf432
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ Suggests:
knitr,
ggplot2,
RColorBrewer,
ragg
ragg,
fastmap
RoxygenNote: 6.1.1
8 changes: 1 addition & 7 deletions R/analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Analysis <- R6::R6Class('Analysis',
.analysisId='',
.name='',
.package='',
.language='',
.title='',
.options=NA,
.results=NA,
Expand All @@ -47,7 +46,6 @@ Analysis <- R6::R6Class('Analysis',
.parent=NA,
.addons=NA,
.stacktrace='',
.translator=NA,
.checkpoint=function(flush=TRUE) {
if (is.null(private$.checkpointCB))
return()
Expand Down Expand Up @@ -128,7 +126,6 @@ Analysis <- R6::R6Class('Analysis',
version,
options,
results,
language='en',
pause=NULL,
data=NULL,
datasetId='',
Expand All @@ -140,7 +137,6 @@ Analysis <- R6::R6Class('Analysis',

private$.package <- package
private$.name <- name
private$.language <- language
private$.version <- version
private$.options <- options
private$.results <- results
Expand All @@ -152,8 +148,6 @@ Analysis <- R6::R6Class('Analysis',
private$.completeWhenFilled <- completeWhenFilled
private$.requiresMissings <- requiresMissings

private$.translator <- createTranslator(package, language)

private$.results$.setParent(self)
private$.options$analysis <- self

Expand All @@ -163,7 +157,7 @@ Analysis <- R6::R6Class('Analysis',
private$.addons <- list()
},
translate=function(text, n=1) {
private$.translator$translate(text, n)
private$.options$translate(text, n)
},
check=function(checkValues=FALSE, checkVars=FALSE, checkData=FALSE) {
private$.options$check(
Expand Down
57 changes: 40 additions & 17 deletions R/i18n.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,48 @@ Translator <- R6Class('Translator',
private=list(
.table=NA
),
public=list(
initialize=function(langDef) {
if (length(langDef) == 0) {
private$.table <- list()
} else {
private$.table <- langDef$locale_data$messages
public=`if`(requireNamespace('fastmap'),
list(
initialize=function(langDef) {
private$.table <- fastmap::fastmap()
if (length(langDef) > 0) {
messages <- langDef$locale_data$messages
messages <- messages[names(messages) != ""]
private$.table$mset(.list=messages)
}
},
translate=function(text, n=1) {
if (is.null(text) || text == '')
return(text)
result <- private$.table$get(text)
if ( ! is.null(result)) {
result <- result[[1]]
if (result != '')
text <- result
}
text
}
},
translate=function(text, n=1) {
if (is.null(text))
return(text)
result <- private$.table[[text]]
if ( ! is.null(result)) {
result <- result[[1]]
if (result != '')
text <- result
),
list(
initialize=function(langDef) {
if (length(langDef) == 0) {
private$.table <- list()
} else {
private$.table <- langDef$locale_data$messages
}
},
translate=function(text, n=1) {
if (is.null(text) || text == '')
return(text)
result <- private$.table[[text]]
if ( ! is.null(result)) {
result <- result[[1]]
if (result != '')
text <- result
}
text
}
text
}
)
)
)

Expand Down

0 comments on commit 0aaf432

Please sign in to comment.