Template string function
This commit is contained in:
parent
5bfb607c60
commit
334b2057d0
@ -35,3 +35,10 @@ module.exports = {
|
|||||||
"ERROR1": " 錯誤訊息 1"
|
"ERROR1": " 錯誤訊息 1"
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Templated String
|
||||||
|
`String.L`
|
||||||
|
```javascript
|
||||||
|
"%s world".L( "hello" ); // hello world
|
||||||
|
"%%s world".L( "hello" ); // %s world
|
||||||
|
```
|
||||||
|
@ -73,4 +73,27 @@ const rLocale = function( lang, stack )
|
|||||||
return Zone + "." + stack[0];
|
return Zone + "." + stack[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
String.prototype.L = function( ...args )
|
||||||
|
{
|
||||||
|
var i = 0;
|
||||||
|
var j = -1;
|
||||||
|
var str = "";
|
||||||
|
|
||||||
|
var a = 0;
|
||||||
|
while( ~( j = this.indexOf( "%s", i ) ) )
|
||||||
|
{
|
||||||
|
i = j + 2;
|
||||||
|
|
||||||
|
// %% => % literal
|
||||||
|
if( this[ j - 1 ] == "%" ) continue;
|
||||||
|
|
||||||
|
str += this.substring( i, j ) + args[ a ++ ];
|
||||||
|
}
|
||||||
|
|
||||||
|
if( str == "" ) return this.replace( "%%", "%" );
|
||||||
|
else str += this.substring( i, this.length );
|
||||||
|
|
||||||
|
return str;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = ProxyLocale( "", rLocale );
|
module.exports = ProxyLocale( "", rLocale );
|
||||||
|
Loading…
Reference in New Issue
Block a user