Skip to content

Commit

Permalink
Feature/I18N: Read locale from browser, pass to Tortoise
Browse files Browse the repository at this point in the history
So far only `en_us` and `zh_cn` are supported and only for runtime
errors, but it's a start.
  • Loading branch information
LaCuneta committed Mar 15, 2023
1 parent 5e18e92 commit 87f9674
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
7 changes: 5 additions & 2 deletions app/assets/javascripts/beak/session-lite.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class SessionLite
widgetController: undefined # WidgetController

# (Tortoise, Element|String, BrowserCompiler, Array[Rewriter], Array[Listener], Array[Widget],
# String, String, Boolean, String, NlogoSource, String, Boolean)
# String, String, Boolean, String, String, NlogoSource, String, Boolean)
constructor: (@tortoise, container, @compiler, @rewriters, listeners, widgets,
code, info, isReadOnly, workInProgressState, @nlogoSource, modelJS, lastCompileFailed) ->
code, info, isReadOnly, @locale, workInProgressState, @nlogoSource, modelJS, lastCompileFailed) ->

@_eventLoopTimeout = -1
@_lastRedraw = 0
Expand Down Expand Up @@ -90,6 +90,7 @@ class SessionLite
window.modelConfig = Object.assign(window.modelConfig ? {}, @widgetController.configs)
window.modelConfig.version = NETLOGO_VERSION
globalEval(modelJS)
workspace.i18nBundle.switch(@locale)

modelTitle: ->
@widgetController.ractive.get('modelTitle')
Expand Down Expand Up @@ -220,6 +221,8 @@ class SessionLite
@widgetController.freshenUpWidgets(oldWidgets, globalEval(res.widgets))

globalEval(res.model.result)
workspace.i18nBundle.switch(@locale)

breedShapePairs.forEach(([name, shape]) -> world.breedManager.get(name).setShape(shape))
plots = plotManager.getPlots()
state.plotManager =
Expand Down
23 changes: 13 additions & 10 deletions app/assets/javascripts/beak/tortoise.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { toNetLogoWebMarkdown, nlogoToSections, sectionsToNlogo } from "./tortoi
import { createNotifier, listenerEvents } from "../notifications/listener-events.js"

# (String|DomElement, BrowserCompiler, Array[Rewriter], Array[Listener], ModelResult,
# Boolean, String, NlogoSource, Boolean) => SessionLite
# Boolean, String, String, NlogoSource, Boolean) => SessionLite
newSession = (container, compiler, rewriters, listeners, modelResult,
isReadOnly, workInProgressState, nlogoSource, lastCompileFailed) ->
isReadOnly, locale, workInProgressState, nlogoSource, lastCompileFailed) ->
{ code, info, model: { result }, widgets: wiggies } = modelResult
widgets = globalEval(wiggies)
info = toNetLogoWebMarkdown(info)
Expand All @@ -20,6 +20,7 @@ newSession = (container, compiler, rewriters, listeners, modelResult,
, code
, info
, isReadOnly
, locale
, workInProgressState
, nlogoSource
, result
Expand All @@ -42,16 +43,16 @@ finishLoading = ->

# type CompileCallback = (Result[SessionLite, Array[CompilerError | String]]) => Unit

# (NlogoSource, Element, Boolean, (NlogoSource) => String, CompileCallback, Array[Rewriter], Array[Listener]) => Unit
fromNlogo = (nlogoSource, container, isUndoReversion, getWorkInProgress, callback, rewriters = [], listeners = []) ->
# (NlogoSource, Element, String, Boolean, (NlogoSource) => String, CompileCallback, Array[Rewriter], Array[Listener]) => Unit
fromNlogo = (nlogoSource, container, locale, isUndoReversion, getWorkInProgress, callback, rewriters = [], listeners = []) ->
startLoading(->
fromNlogoSync(nlogoSource, container, isUndoReversion, getWorkInProgress, callback, rewriters, listeners)
fromNlogoSync(nlogoSource, container, locale, isUndoReversion, getWorkInProgress, callback, rewriters, listeners)
finishLoading()
)
return

# (String, Element, (NlogoSource) => String, CompileCallback, Array[Rewriter], Array[Listener]) => Unit
fromURL = (url, container, getWorkInProgress, callback, rewriters = [], listeners = []) ->
# (String, Element, String, (NlogoSource) => String, CompileCallback, Array[Rewriter], Array[Listener]) => Unit
fromURL = (url, container, locale, getWorkInProgress, callback, rewriters = [], listeners = []) ->
startLoading(() ->
req = new XMLHttpRequest()
req.open('GET', url)
Expand All @@ -62,7 +63,7 @@ fromURL = (url, container, getWorkInProgress, callback, rewriters = [], listener
else
nlogo = req.responseText
urlSource = new UrlSource(url, nlogo)
fromNlogoSync(urlSource, container, false, getWorkInProgress, callback, rewriters, listeners)
fromNlogoSync(urlSource, container, locale, false, getWorkInProgress, callback, rewriters, listeners)
finishLoading()
return

Expand All @@ -71,9 +72,9 @@ fromURL = (url, container, getWorkInProgress, callback, rewriters = [], listener
)
return

# (NlogoSource, Element, Boolean, Boolean,
# (NlogoSource, Element, String, Boolean,
# (NlogoSource) => String, CompileCallback, Array[Rewriter], Array[Listener]) => Unit
fromNlogoSync = (nlogoSource, container, isUndoReversion,
fromNlogoSync = (nlogoSource, container, locale, isUndoReversion,
getWorkInProgress, callback, rewriters, listeners) ->

compiler = new BrowserCompiler()
Expand Down Expand Up @@ -113,6 +114,7 @@ fromNlogoSync = (nlogoSource, container, isUndoReversion,
, listeners
, result
, false
, locale
, workInProgressState
, nlogoSource
, false
Expand All @@ -137,6 +139,7 @@ fromNlogoSync = (nlogoSource, container, isUndoReversion,
, listeners
, secondChanceResult
, false
, locale
, workInProgressState
, nlogoSource
, true
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/nettango/netlogo-model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RactiveNetLogoModel = Ractive.extend({
listeners: null # Array[Listener]
alerter: null # AlertDisplay
modelContainer: null # Element
locale: null # String
session: null # SessionLite
widgetController: null # WidgetController
workspace: null # Workspace
Expand Down Expand Up @@ -83,6 +84,7 @@ RactiveNetLogoModel = Ractive.extend({
Tortoise.fromNlogoSync(
nlogoSource
, @modelContainer
, @locale
, false
, null
, @makeCompileResultHandler(callback)
Expand All @@ -98,6 +100,7 @@ RactiveNetLogoModel = Ractive.extend({
Tortoise.fromURL(
url
, @modelContainer
, @locale
, ((s) -> s.nlogo)
, @makeCompileResultHandler(callback)
, @rewriters
Expand Down
8 changes: 6 additions & 2 deletions app/assets/javascripts/pages/simulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ if (isInFrame && params.has('relayIframeEvents')) {
}

const notifyListeners = createNotifier(listenerEvents, listeners);
const locale = navigator.language.replace('-', '_').toLowerCase()

if (isInFrame) {
const getSession = () => { return globalThis.session };
Expand All @@ -132,6 +133,7 @@ var loadModel = function(nlogo, sourceType, path, isUndoReversion) {
Tortoise.fromNlogo(
nlogoSource
, modelContainer
, locale
, isUndoReversion
, getWorkInProgress
, handleCompileResult
Expand Down Expand Up @@ -198,8 +200,8 @@ const redirectOnProtocolMismatch = function(url) {
return false;
}

speed = readSpeed(params);
isVertical = !(params.has('tabs') && params.get('tabs') === 'right');
speed = readSpeed(params);
isVertical = !(params.has('tabs') && params.get('tabs') === 'right');

if (nlogoScript.textContent.length > 0) {
const nlogo = nlogoScript.textContent;
Expand All @@ -209,6 +211,7 @@ if (nlogoScript.textContent.length > 0) {
Tortoise.fromNlogo(
nlogoSource
, modelContainer
, locale
, false
, getWorkInProgress
, handleCompileResult
Expand All @@ -224,6 +227,7 @@ if (nlogoScript.textContent.length > 0) {
Tortoise.fromURL(
url
, modelContainer
, locale
, getWorkInProgress
, handleCompileResult
, []
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ lazy val root = (project in file("."))
TestAssets / JsEngineKeys.npmNodeModules := Nil
)

val tortoiseVersion = "1.0-8824b1d"
val tortoiseVersion = "1.0-5318fa9"

resolvers ++= Seq(
"compilerjvm" at "https://dl.cloudsmith.io/public/netlogo/tortoise/maven/"
Expand Down

0 comments on commit 87f9674

Please sign in to comment.