Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

intellij-idea - 在其他本地化json文件中找到相同的键(Find the same key in other localization json-file)

I want to find the same key in an other jsons localization file.

(我想在其他jsons本地化文件中找到相同的键。)

My json's looks like this.

(我的json看起来像这样。)

{
  "general": {
    "main": {
      "title": "This is a title",
      "header": "This is a header",
      "sub": {
        "sub_thing_1": "Something",
        "sub_thing_2": "Something2"
      }
    }
  },
  "other_things": {
    "title": "Other title"
    "sub": {
       "sub_thing_1": "Something",
       "sub_thing_2": "Something2"
    }
  }
}

As you can see the same key can be in more places

(如您所见,相同的键可以在更多地方)

My thinking is, to generate a collection that contains, the depth values like this

(我的想法是,要生成一个包含这样的深度值的集合)

eg: this "sub_thing_2" can be found at -> ["general", "main", "sub"]

(例如:此“ sub_thing_2”可以在-> [“ general”,“ main”,“ sub”]中找到)

and with this information i can find the exact key in other files.

(有了这些信息,我可以在其他文件中找到确切的密钥。)

And with this code I geathering the depth information for each key, it's just a scratch, thinkering code fragement, I'm new to Kotlin by the way

(借助此代码,我可以收集每个键的深度信息,这只是从头开始,思考代码的琐碎,顺便说一下,我对Kotlin还是陌生的)


psi?.accept(object : JsonRecursiveElementVisitor() {
    override fun visitObject(o: JsonObject) {
        val childrens = o.children
        for (child in childrens) {
            val childProp = child as JsonProperty
            if (child.lastChild is JsonObject) {
                depth.add(childProp.name)
                visitObject(child.lastChild as JsonObject)
            } else {
                println(depth.toString() + childProp.name + ": " + childProp.lastChild.text)
            }
        }

        if (depth.isNotEmpty()) {
            val lastItem = depth.last();
            depth.remove(lastItem)
        }
    }
})

My question is there is other easier way to do this maybe without the PSI or I'm in the right direction?

(我的问题是,如果没有PSI,或者我朝正确的方向,还有其他更简便的方法吗?)

  ask by Kurucz Dávid translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...