From 6367c321da3dd02d68e033bb56932fb51c664b3f Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Sat, 10 Mar 2018 20:42:09 +0100 Subject: [PATCH] add FormatedString class, automatically translate to new locale when locale is set --- src/core/BaseDialog.coffee | 22 +++++++------- src/core/api.coffee | 41 +++++++++++++++++++++++--- src/core/gui.coffee | 2 +- src/core/schemes/filedialog.html | 4 +-- src/core/tags/afx-file-view.tag | 11 +++---- src/core/vfs.coffee | 3 +- src/core/vfs/GoogleDriveHandler.coffee | 2 +- src/packages/Files/main.coffee | 8 ++--- src/packages/NotePad/main.coffee | 15 +++++----- src/packages/NotePad/main.css | 2 +- src/packages/NotePad/scheme.html | 2 +- src/themes/antos/afx-file-view.css | 2 +- 12 files changed, 75 insertions(+), 39 deletions(-) diff --git a/src/core/BaseDialog.coffee b/src/core/BaseDialog.coffee index a0f6523..e726864 100644 --- a/src/core/BaseDialog.coffee +++ b/src/core/BaseDialog.coffee @@ -87,7 +87,7 @@ class PromptDialog extends BasicDialog resizable: false, buttons: [ { - label: __("Ok"), + label: "__(Ok)", onclick: (d) -> txt = (d.find "content1").value return d.quit() if txt is "" @@ -95,7 +95,7 @@ class PromptDialog extends BasicDialog d.quit() }, { - label: __("Cancel"), + label: "__(Cancel)", onclick: (d) -> d.quit() } ], @@ -119,7 +119,7 @@ class CalendarDialog extends BasicDialog resizable: false, buttons: [ { - label: __('Ok'), + label: "__(Ok)", onclick: (d) -> date = (d.find "content0").get "selectedDate" if date @@ -129,7 +129,7 @@ class CalendarDialog extends BasicDialog d.notify __("Please select a date") }, { - label: __('Cancel'), + label: "__(Cancel)", onclick: (d) -> d.quit() } ] @@ -145,7 +145,7 @@ class ColorPickerDialog extends BasicDialog resizable: false, buttons: [ { - label: __('Ok'), + label: "__(Ok)", onclick: (d) -> c = (d.find "content0").get "selectedColor" if c @@ -155,7 +155,7 @@ class ColorPickerDialog extends BasicDialog d.notify "Please select a color" }, { - label: __('Cancel'), + label: "__(Cancel)", onclick: (d) -> d.quit() } ] @@ -169,7 +169,7 @@ class InfoDialog extends BasicDialog width: 250, height: 300, resizable: true, - buttons: [ { label: __('Cancel'), onclick: (d) -> d.quit() } ], + buttons: [ { label: "__(Cancel)", onclick: (d) -> d.quit() } ], filldata: (d) -> return unless d.data rows = [] @@ -188,12 +188,12 @@ class YesNoDialog extends BasicDialog resizable: true, buttons: [ { - label: __("Yes"), onclick: (d) -> + label: "__(Yes)", onclick: (d) -> d.handler true if d.handler d.quit() }, { - label: __("No"), onclick: (d) -> + label: "__(No)", onclick: (d) -> d.handler false if d.handler d.quit() } @@ -215,14 +215,14 @@ class SelectionDialog extends BasicDialog resizable: false, buttons: [ { - label: __("Ok"), onclick: (d) -> + label: "__(Ok)", onclick: (d) -> el = d.find "content0" it = el.get "selected" return unless it d.handler it if d.handler d.quit() }, - { label: __("Cancel"), onclick: (d) -> d.quit() } + { label: "__(Cancel)", onclick: (d) -> d.quit() } ], filldata: (d) -> return unless d.data diff --git a/src/core/api.coffee b/src/core/api.coffee index 093ff3c..fd59411 100644 --- a/src/core/api.coffee +++ b/src/core/api.coffee @@ -1,3 +1,32 @@ +class FormatedString + constructor: (@fs, args) -> + @values = [] + return unless args + @values[i] = args[i] for i in [0..args.length - 1] + toString: ()-> + @ + __: () -> + me = @ + return __(@fs).replace /{(\d+)}/g, (match, number) -> + return if typeof me.values[number] != 'undefined' then me.values[number] else match + + hash: () -> + @__().hash() + + asBase64: () -> + @__().asBase64() + + unescape: () -> + @__().unescape() + + asUint8Array: () -> + @__().asUint8Array() + + format: () -> + args = arguments + @values[i] = args[i] for i in [0..args.length - 1] + + String.prototype.hash = () -> hash = 5381 i = this.length @@ -19,7 +48,7 @@ String.prototype.unescape = () -> d = d.replace /\\f/g, "\f" d = d.replace /\\r/g, "\r" d -String.prototype.asUnit8Array = () -> +String.prototype.asUint8Array = () -> bytes = [] for i in [0..(@length - 1)] bytes.push @charCodeAt i @@ -29,14 +58,18 @@ String.prototype.asUnit8Array = () -> if not String.prototype.format String.prototype.format = () -> args = arguments - return @replace /{(\d+)}/g, (match, number) -> - return if typeof args[number] != 'undefined' then args[number] else match + return new FormatedString(@, args) + +String.prototype.f = () -> + args = arguments + return new FormatedString(@, args) String.prototype.__ = () -> match = @match(/^__\((.*)\)$/) return window.__(match[1]) if match return @ # language directive + this.__ = () -> _API = window.OS.API args = arguments @@ -44,7 +77,7 @@ this.__ = () -> d = args[0] _API.lang[d] = d unless _API.lang[d] return _API.lang[d] unless args.length > 1 - return String.prototype.format.apply _API.lang[d], (args[i] for i in [1 .. args.length - 1]) + return String.prototype.format.apply d, (args[i] for i in [1 .. args.length - 1]) Date.prototype.toString = () -> dd = @getDate() diff --git a/src/core/gui.coffee b/src/core/gui.coffee index f58edfe..f84a8eb 100644 --- a/src/core/gui.coffee +++ b/src/core/gui.coffee @@ -408,7 +408,7 @@ self.OS.GUI = _GUI.buildSystemMenu() # push startup services # TODO: get services list from user setting - _GUI.pushServices (v for v in _OS.setting.system.startup.services) + #_GUI.pushServices (v for v in _OS.setting.system.startup.services) (_GUI.launch a) for a in _OS.setting.system.startup.apps #_GUI.launch "DummyApp" # initDM diff --git a/src/core/schemes/filedialog.html b/src/core/schemes/filedialog.html index 9800846..380f660 100644 --- a/src/core/schemes/filedialog.html +++ b/src/core/schemes/filedialog.html @@ -5,8 +5,8 @@
- - + +
diff --git a/src/core/tags/afx-file-view.tag b/src/core/tags/afx-file-view.tag index 8c226ea..706da44 100644 --- a/src/core/tags/afx-file-view.tag +++ b/src/core/tags/afx-file-view.tag @@ -4,7 +4,7 @@
-
+