简体   繁体   English

从代码中仅提取某些字符串

[英]Extracting only certain strings from code

I have various very large JavaScript files, and need to extract only certain strings. 我有各种非常大的JavaScript文件,只需要提取某些字符串即可。 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? 编写规则并不难,但是JavaScript可以读取100页代码并将其视为字符串来进行解析吗? 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. 根据上面的评论,您需要一个JavaScript本地化机制。 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). 1)为每种语言创建一个单独的JS文件,并根据客户端加载正确的JS文件(通过从请求中获取其信息)。

Quite poor - a lot of duplication and overhead. 很差-很多重复和开销。

2) A bit improved method is to represent each string by an object: 2)一种改进的方法是用对象表示每个字符串:

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' . 比起您的代码,您还可以通过调用localeStrings['some message']['en/US']检索真实消息,再次从请求标头中检索语言,并像我使用'en/US'

Also poor - since, again, a lot of unnecessary data is loaded to the client. 效果也很差-因为再次将很多不必要的数据加载到客户端。

3) Some form of dynamic replacement upon request. 3)根据要求提供某种形式的动态更换。

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. 一般而言,您的想法是,您有一些用于服务器端组件(例如servlet)的组件,可以用包含转换的服务器端文件中的数据替换令牌(比如说## {some token})。 。

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 . 编辑 :忘记指向您指向相关对话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM