Template string function

This commit is contained in:
斟酌 鵬兄 2016-07-03 17:41:59 +08:00
parent 5bfb607c60
commit 334b2057d0
2 changed files with 30 additions and 0 deletions

View File

@ -35,3 +35,10 @@ module.exports = {
"ERROR1": " 錯誤訊息 1"
};
```
### Templated String
`String.L`
```javascript
"%s world".L( "hello" ); // hello world
"%%s world".L( "hello" ); // %s world
```

View File

@ -73,4 +73,27 @@ const rLocale = function( lang, stack )
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 );