-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi delimiter support #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,7 +81,7 @@ Default is `!!]`. | |
pseudoloc.str('A test string with a %token%.') | ||
// [!!Á ţȇšŧ śťřīņğ ŵıţħ ą %token%.##] | ||
|
||
#### Delimiter, StartDelimiter, EndDelimiter | ||
#### Delimiter, StartDelimiter, EndDelimiter, Delimiters | ||
|
||
Specifies the token delimiter. Any characters between token delimiters will not be pseudolocalized. Tokens are used to replace data within localized strings. You can either specify a single delimiter or use startDelimiter and endDelimiter to specify the delimiters seperately. | ||
|
||
|
@@ -96,6 +96,39 @@ Default is `%`. | |
pseudoloc.str('A test string with a {{token}}.') | ||
// [!!Á ţȇšŧ śťřīņğ ŵıţħ ą {{token}}.!!] | ||
|
||
If you need to support multiple types of delimiters, you can pass an array of delimiters (single or pairs) to the `delimiters` option. | ||
|
||
The `delimiters` option takes an array of objects. Set properties on the objects as follows: | ||
|
||
* `{ start, end }`: specifies a pair of start and end delimiters, just like using `startDelimiter` and `endDelimiter`: | ||
``` | ||
{ start: '<', end: '>' } | ||
``` | ||
|
||
* `{ both }`: specifies a marker to use as both the start and end delimiters, just like using `delimiter` | ||
``` | ||
{ both: '$$' } | ||
``` | ||
|
||
* `{ full }`: specifies the entire pattern for the delimiter. This is useful for cases where the token doesn't have a start marker, name, and end marker, for example with printf-style placeholders `%s`, `%d`, etc. | ||
``` | ||
{ full: '%d' } | ||
``` | ||
|
||
Under the hood these strings are combined into a pattern that eventually is compiled into a RegExp. That can affect you in a couple of ways: | ||
|
||
1. You can use regular expression matchers in your delimiters | ||
2. If your delimiter includes any characters that are special characters in regular expressions, they will need to be escaped | ||
|
||
For example, to match named sprintf-style placeholders (such as `%(name)s`), you need to escape the parentheses: | ||
|
||
// Note the double-backslash, which becomes a `\(` in the string | ||
pseudoloc.option.startDelimiter = '%\\('; | ||
// Note the square brackets, so it matches `)s` or `)d` | ||
pseudoloc.option.endDelimiter = '\\)[sd]'; | ||
pseudoloc.str('A test string with a %(token)s.'); | ||
// [!!Á ţȇšŧ śťřīņğ ŵıţħ ą %(token)s.!!] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just some additional explanation about the way the |
||
#### Extend | ||
|
||
Extends the width of the string by the specified percentage. Useful if you will be localizing into languages such as German which can be 30% longer than English. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,26 @@ | |
* http://bunkat.github.com/pseudoloc | ||
*/ | ||
pseudoloc.str = function(str) { | ||
function makeTokenRegex(delims, tokenNameDelim) { | ||
var tokenMatchers = delims.reduce(function(result, delim) { | ||
if (delim.hasOwnProperty('both')) { | ||
result.push(delim.both + tokenNameDelim + delim.both); | ||
} else if (delim.hasOwnProperty('start') && delim.hasOwnProperty('end')) { | ||
result.push(delim.start + tokenNameDelim + delim.end); | ||
} else if (delim.hasOwnProperty('full')) { | ||
result.push(delim.full); | ||
} | ||
return result; | ||
}, []); | ||
return new RegExp(tokenMatchers.join('|'), 'g'); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the core of the change. Now it uses the It constructs a regular expression (as a string) for each element of the array, then joins them together with |
||
var opts = pseudoloc.option, | ||
tokenNameDelim = '\\s*[\\w\\.\\s*]+\\s*', | ||
startdelim = opts.startDelimiter || opts.delimiter, | ||
enddelim = opts.endDelimiter || opts.delimiter, | ||
re = new RegExp(startdelim + '\\s*[\\w\\.\\s*]+\\s*' + enddelim, 'g'), | ||
delims = opts.delimiters || [{ start: startdelim, end: enddelim }], | ||
re = makeTokenRegex(delims, tokenNameDelim), | ||
m, tokens = [], i = 0, tokenIdx = 0, result = '', c, pc; | ||
|
||
while((m = re.exec(str))) { | ||
|
@@ -41,4 +57,4 @@ pseudoloc.str = function(str) { | |
} | ||
|
||
return opts.prepend + pseudoloc.pad(result, opts.extend) + opts.append; | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the documentation for the change