Extracting only certain strings from code
I have various very large JavaScript files, and need to ext开发者_如何学Pythonract only certain strings. The input would be hundreds of lines like this:
function foo("ignore this string"){
var n = "copy this";
}
function bar("copy this string"){
var i = "ignore this string";
}
var globalX ="copy if over a certain length";
var globalY ="copy if within 2 lines of 'globalX'";
The output would be a simple list of strings. Writing the rules would not be hard, but can JavaScript read 100 pages of code and treat it like a string for parsing purposes? if not, what other tools should I use? Any suggestions are welcome!
Based on your comment above - what you need is a JavaScript localization mechanism. There are several options that depend on the way you build and package your application.
Here are some of the mechanisms:
1) Create a separate JS file for each language and load the correct one depending on the client (by retrieving his information from the request).
Quite poor - a lot of duplication and overhead.
2) A bit improved method is to represent each string by an object:
var localeStrings =
{
'some message':
{
'en/US':'Some text',
'ru/RU':'Какой-то текст'
}
}
than i your code you retrieve the real message by calling localeStrings['some message']['en/US']
, again, retrieving the language from request header and using just as I used 'en/US'
.
Also poor - since, again, a lot of unnecessary data is loaded to the client.
3) Some form of dynamic replacement upon request.
Generally speaking the idea is that you have some for of a server-side component (say, servlet) that replaces tokens (let's say `##{some token}) with the data from the server side file(s) that contains the translations.
This one may cause user responses to be a bit delayed, but this is only an assumption and may be negligible.
Pick your poison.
I am sure there are more, and am anxious to see them in answers. I couldn't find something I could use out-of-the-box.
EDIT: forgot to point you to a related conversation.
精彩评论