needEscapeChars = new ArrayList<>();
32 |
33 | public EscapeCharactersInterceptor() {
34 | needEscapeChars.add('@');
35 | needEscapeChars.add('?');
36 | needEscapeChars.add('\'');
37 | needEscapeChars.add('\"');
38 | }
39 |
40 | @Override
41 | public String process(String text) {
42 | if (StringUtil.isEmpty(text)) {
43 | return text;
44 | }
45 | final StringBuilder result = new StringBuilder();
46 | final char[] chars = text.toCharArray();
47 | for (char ch : chars) {
48 | if (needEscapeChars.contains(ch)) {
49 | result.append('\\');
50 | }
51 | result.append(ch);
52 | }
53 | return result.toString();
54 | }
55 | }
--------------------------------------------------------------------------------
/src/main/java/com/airsaid/localization/translate/lang/Lang.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Airsaid. https://github.com/airsaid
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.airsaid.localization.translate.lang;
19 |
20 | import com.intellij.openapi.util.text.StringUtil;
21 |
22 | import java.util.Objects;
23 |
24 | /**
25 | * Language data class, which is an immutable class,
26 | * any modification to it will generate you a new object.
27 | *
28 | * @author airsaid
29 | */
30 | public final class Lang implements Cloneable {
31 | private final int id;
32 | private final String code;
33 | private final String name;
34 | private final String englishName;
35 | private String translationCode;
36 |
37 | public Lang(int id, String code, String name, String englishName) {
38 | this.id = id;
39 | this.code = code;
40 | this.name = name;
41 | this.englishName = englishName;
42 | }
43 |
44 | public int getId() {
45 | return id;
46 | }
47 |
48 | public String getCode() {
49 | return code;
50 | }
51 |
52 | public String getName() {
53 | return name;
54 | }
55 |
56 | public String getEnglishName() {
57 | return englishName;
58 | }
59 |
60 | public Lang setTranslationCode(String translationCode) {
61 | final Lang newLang = this.clone();
62 | Objects.requireNonNull(newLang).translationCode = translationCode;
63 | return newLang;
64 | }
65 |
66 | public String getTranslationCode() {
67 | if (!StringUtil.isEmpty(translationCode)) {
68 | return translationCode;
69 | }
70 | return code;
71 | }
72 |
73 | @Override
74 | public boolean equals(Object o) {
75 | if (this == o) return true;
76 | if (o == null || getClass() != o.getClass()) return false;
77 | Lang language = (Lang) o;
78 | return id == language.id;
79 | }
80 |
81 | @Override
82 | public int hashCode() {
83 | return Objects.hash(id);
84 | }
85 |
86 | @Override
87 | public Lang clone() {
88 | try {
89 | return (Lang) super.clone();
90 | } catch (CloneNotSupportedException e) {
91 | e.printStackTrace();
92 | }
93 | return null;
94 | }
95 |
96 | @Override
97 | public String toString() {
98 | return "Lang{" +
99 | "id=" + id +
100 | ", code='" + code + '\'' +
101 | ", name='" + name + '\'' +
102 | ", englishName='" + englishName + '\'' +
103 | ", translationCode='" + translationCode + '\'' +
104 | '}';
105 | }
106 | }
--------------------------------------------------------------------------------
/src/main/java/com/airsaid/localization/translate/services/TranslationCacheService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Airsaid. https://github.com/airsaid
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.airsaid.localization.translate.services;
19 |
20 | import com.airsaid.localization.translate.util.GsonUtil;
21 | import com.airsaid.localization.translate.util.LRUCache;
22 | import com.google.gson.reflect.TypeToken;
23 | import com.intellij.openapi.Disposable;
24 | import com.intellij.openapi.components.*;
25 | import com.intellij.util.xmlb.Converter;
26 | import com.intellij.util.xmlb.XmlSerializerUtil;
27 | import com.intellij.util.xmlb.annotations.OptionTag;
28 | import com.intellij.util.xmlb.annotations.Transient;
29 | import org.jetbrains.annotations.NotNull;
30 | import org.jetbrains.annotations.Nullable;
31 |
32 | import java.lang.reflect.Type;
33 | import java.util.LinkedHashMap;
34 | import java.util.Map;
35 |
36 | /**
37 | * Cache the translated text to local disk.
38 | *
39 | * The maximum number of caches is set by the {@link #setMaxCacheSize(int)} method,
40 | * if exceed this size, remove old data through the LRU algorithm.
41 | *
42 | * @author airsaid
43 | */
44 | @State(
45 | name = "com.airsaid.localization.translate.services.TranslationCacheService",
46 | storages = {@Storage("androidLocalizeTranslationCaches.xml")}
47 | )
48 | @Service
49 | public final class TranslationCacheService implements PersistentStateComponent, Disposable {
50 |
51 | @Transient
52 | private static final int CACHE_MAX_SIZE = 500;
53 |
54 | @OptionTag(converter = LruCacheConverter.class)
55 | private final LRUCache lruCache = new LRUCache<>(CACHE_MAX_SIZE);
56 |
57 | public static TranslationCacheService getInstance() {
58 | return ServiceManager.getService(TranslationCacheService.class);
59 | }
60 |
61 | public void put(@NotNull String key, @NotNull String value) {
62 | lruCache.put(key, value);
63 | }
64 |
65 | @NotNull
66 | public String get(String key) {
67 | String value = lruCache.get(key);
68 | return value != null ? value : "";
69 | }
70 |
71 | public void setMaxCacheSize(int maxCacheSize) {
72 | lruCache.setMaxCapacity(maxCacheSize);
73 | }
74 |
75 | @Override
76 | public @NotNull TranslationCacheService getState() {
77 | return this;
78 | }
79 |
80 | @Override
81 | public void loadState(@NotNull TranslationCacheService state) {
82 | XmlSerializerUtil.copyBean(state, this);
83 | }
84 |
85 | @Override
86 | public void dispose() {
87 | lruCache.clear();
88 | }
89 |
90 | static class LruCacheConverter extends Converter> {
91 | @Override
92 | public @Nullable LRUCache fromString(@NotNull String value) {
93 | Type type = new TypeToken