allow to overide locale method __() to all object

This commit is contained in:
Xuan Sang LE 2018-03-10 22:46:28 +01:00
parent 6367c321da
commit 702a83df6a
7 changed files with 24 additions and 16 deletions

View File

@ -3,13 +3,12 @@ class FormatedString
@values = [] @values = []
return unless args return unless args
@values[i] = args[i] for i in [0..args.length - 1] @values[i] = args[i] for i in [0..args.length - 1]
toString: ()-> toString: () ->
@ @__()
__: () -> __: () ->
me = @ me = @
return __(@fs).replace /{(\d+)}/g, (match, number) -> return @fs.l().replace /{(\d+)}/g, (match, number) ->
return if typeof me.values[number] != 'undefined' then me.values[number] else match return if typeof me.values[number] != 'undefined' then me.values[number] else match
hash: () -> hash: () ->
@__().hash() @__().hash()
@ -25,7 +24,12 @@ class FormatedString
format: () -> format: () ->
args = arguments args = arguments
@values[i] = args[i] for i in [0..args.length - 1] @values[i] = args[i] for i in [0..args.length - 1]
Object.defineProperty Object.prototype, '__',
value: () ->
return @toString()
enumerable: false
writable: true
String.prototype.hash = () -> String.prototype.hash = () ->
hash = 5381 hash = 5381
@ -66,8 +70,12 @@ String.prototype.f = () ->
String.prototype.__ = () -> String.prototype.__ = () ->
match = @match(/^__\((.*)\)$/) match = @match(/^__\((.*)\)$/)
return window.__(match[1]) if match return match[1].l() if match
return @ return @
String.prototype.l = () ->
_API = window.OS.API
_API.lang[@] = @ unless _API.lang[@]
return _API.lang[@]
# language directive # language directive
this.__ = () -> this.__ = () ->
@ -75,9 +83,8 @@ this.__ = () ->
args = arguments args = arguments
return "Undefined" unless args.length > 0 return "Undefined" unless args.length > 0
d = args[0] d = args[0]
_API.lang[d] = d unless _API.lang[d] d.l()
return _API.lang[d] unless args.length > 1 return new FormatedString 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 = () -> Date.prototype.toString = () ->
dd = @getDate() dd = @getDate()

View File

@ -408,7 +408,7 @@ self.OS.GUI =
_GUI.buildSystemMenu() _GUI.buildSystemMenu()
# push startup services # push startup services
# TODO: get services list from user setting # 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 a) for a in _OS.setting.system.startup.apps
#_GUI.launch "DummyApp" #_GUI.launch "DummyApp"
# initDM # initDM

View File

@ -65,7 +65,8 @@ self.OS.API.handler =
path = "#{_REST}/system/application" path = "#{_REST}/system/application"
auth: (c) -> auth: (c) ->
p = "#{_REST}/system/auth" p = "#{_REST}/system/auth"
_API.post p, {}, c, () -> _API.post p, {}, c, (e, s) ->
console.log e, s
alert __("Resource not found: {0}", p) alert __("Resource not found: {0}", p)
login: (d, c) -> login: (d, c) ->
p = "#{_REST}/system/login" p = "#{_REST}/system/login"

View File

@ -4,7 +4,7 @@
<li class = "afx-window-close" onclick = {close}></li> <li class = "afx-window-close" onclick = {close}></li>
<li if = {minimizable == true} class = "afx-window-minimize" onclick = {minimize}></li> <li if = {minimizable == true} class = "afx-window-minimize" onclick = {minimize}></li>
<li if = {resizable == true} class = "afx-window-maximize" onclick={maximize}></li> <li if = {resizable == true} class = "afx-window-maximize" onclick={maximize}></li>
<li ref = "dragger" class = "afx-window-title">{ apptitle?apptitle.toString().__():apptitle }</li> <li ref = "dragger" class = "afx-window-title">{ apptitle?apptitle.__():apptitle }</li>
</ul> </ul>
<div class = "afx-clear"></div> <div class = "afx-clear"></div>
<div ref = "content" class = "afx-window-content"> <div ref = "content" class = "afx-window-content">

View File

@ -2,7 +2,7 @@
<span style = {color?"color:" + color:""} > <span style = {color?"color:" + color:""} >
<i if={iconclass} class = {iconclass} ></i> <i if={iconclass} class = {iconclass} ></i>
<i if={icon} class="icon-style" style = { "background: url("+icon+");background-size: 100% 100%;background-repeat: no-repeat;" }></i> <i if={icon} class="icon-style" style = { "background: url("+icon+");background-size: 100% 100%;background-repeat: no-repeat;" }></i>
{ text?text.toString().__():"" } { text?text.__():"" }
</span> </span>
<script> <script>
this.iconclass = opts.iconclass this.iconclass = opts.iconclass

View File

@ -61,7 +61,7 @@ class CodeBlock extends this.OS.GUI.BaseApplication
menu = [{ menu = [{
text: "File", text: "File",
child: [ child: [
{ text: "__(New)", dataid: "#{@name}-New", shortcut: "A-N" }, { text: __("New"), dataid: "#{@name}-New", shortcut: "A-N" },
{ text: "__(Open)", dataid: "#{@name}-Open", shortcut: "A-O" }, { text: "__(Open)", dataid: "#{@name}-Open", shortcut: "A-O" },
{ text: "__(Save)", dataid: "#{@name}-Save", shortcut: "C-S" }, { text: "__(Save)", dataid: "#{@name}-Save", shortcut: "C-S" },
{ text: "__(Save as)", dataid: "#{@name}-Saveas", shortcut: "A-W" } { text: "__(Save as)", dataid: "#{@name}-Saveas", shortcut: "A-W" }

View File

@ -62,8 +62,8 @@ class PushNotification extends this.OS.GUI.BaseService
icon: o.data.icon, icon: o.data.icon,
iconclass: o.data.iconclass, iconclass: o.data.iconclass,
closable: true } closable: true }
console.log o.data.s #console.log o.data.s
console.log o.data.e #console.log o.data.e
@mlist.unshift d, true @mlist.unshift d, true
@notifeed d @notifeed d