Skip to content

Commit c50e6f8

Browse files
committed
Replace StringUtils.isNumeric() with custom method
1 parent ed0afe0 commit c50e6f8

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.extensions
19+
20+
private val NON_TRANSLATABLE_REGEX = Regex("^[\\d\\p{P}\\p{S}\\s]+$")
21+
22+
/**
23+
* Checks if this string contains only numbers, symbols, punctuation, and whitespace.
24+
* This is used to determine if text needs translation - strings with only these characters
25+
* typically don't require translation.
26+
*
27+
* @return `true` if this string is not null, not empty, and contains only digits, punctuation,
28+
* symbols, and whitespace. `false` if this string is null, empty, or contains any
29+
* letters that would need translation.
30+
*
31+
* @sample
32+
* ```
33+
* "123".hasNoTranslatableText() // returns true
34+
* "12.3".hasNoTranslatableText() // returns true
35+
* "-123".hasNoTranslatableText() // returns true
36+
* "123!@#".hasNoTranslatableText() // returns true
37+
* "12 34".hasNoTranslatableText() // returns true
38+
* "abc".hasNoTranslatableText() // returns false
39+
* "123abc".hasNoTranslatableText() // returns false
40+
* null.hasNoTranslatableText() // returns true
41+
* ```
42+
*/
43+
fun String?.hasNoTranslatableText(): Boolean {
44+
if (this.isNullOrEmpty()) return true
45+
return this.matches(NON_TRANSLATABLE_REGEX)
46+
}

src/main/kotlin/com/airsaid/localization/translate/services/TranslatorService.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717

1818
package com.airsaid.localization.translate.services
1919

20+
import com.airsaid.localization.extensions.hasNoTranslatableText
2021
import com.airsaid.localization.translate.AbstractTranslator
2122
import com.airsaid.localization.translate.interceptors.EscapeCharactersInterceptor
2223
import com.airsaid.localization.translate.lang.Lang
2324
import com.intellij.openapi.application.ApplicationManager
2425
import com.intellij.openapi.components.Service
2526
import com.intellij.openapi.components.service
2627
import com.intellij.openapi.diagnostic.Logger
27-
import org.apache.commons.lang.StringUtils
2828
import java.util.*
2929
import java.util.function.Consumer
30-
import kotlin.jvm.Volatile
3130

3231
/**
3332
* @author airsaid
@@ -115,8 +114,8 @@ class TranslatorService {
115114
}
116115
}
117116

118-
// Arabic numbers skip translation
119-
if (StringUtils.isNumeric(text)) {
117+
// Skip translation for text containing only numbers, symbols, and punctuation
118+
if (text.hasNoTranslatableText()) {
120119
return text
121120
}
122121

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.airsaid.localization.extensions
2+
3+
import org.junit.jupiter.api.Assertions.assertFalse
4+
import org.junit.jupiter.api.Assertions.assertTrue
5+
import org.junit.jupiter.api.Test
6+
7+
class StringExtensionsTest {
8+
9+
@Test
10+
fun hasNoTranslatableText() {
11+
// Numbers only - should return true
12+
assertTrue("123".hasNoTranslatableText())
13+
assertTrue("0".hasNoTranslatableText())
14+
assertTrue("999999".hasNoTranslatableText())
15+
16+
// Numbers with symbols/punctuation - should return true
17+
assertTrue("12.3".hasNoTranslatableText())
18+
assertTrue("-123".hasNoTranslatableText())
19+
assertTrue("+123".hasNoTranslatableText())
20+
assertTrue("123!@#".hasNoTranslatableText())
21+
assertTrue("12,34".hasNoTranslatableText())
22+
assertTrue("$100".hasNoTranslatableText())
23+
assertTrue("50%".hasNoTranslatableText())
24+
assertTrue("(123)".hasNoTranslatableText())
25+
26+
// Numbers with whitespace - should return true
27+
assertTrue("12 3".hasNoTranslatableText())
28+
assertTrue("12\n3".hasNoTranslatableText())
29+
assertTrue("12\t3".hasNoTranslatableText())
30+
assertTrue(" 123 ".hasNoTranslatableText())
31+
32+
// Symbols/punctuation only - should return true
33+
assertTrue("!@#".hasNoTranslatableText())
34+
assertTrue("...".hasNoTranslatableText())
35+
assertTrue("***".hasNoTranslatableText())
36+
assertTrue("???".hasNoTranslatableText())
37+
38+
// Text with letters - should return false (needs translation)
39+
assertFalse("abc".hasNoTranslatableText())
40+
assertFalse("123abc".hasNoTranslatableText())
41+
assertFalse("abc123".hasNoTranslatableText())
42+
assertFalse("Hello".hasNoTranslatableText())
43+
assertFalse("Hello123".hasNoTranslatableText())
44+
assertFalse("123Hello".hasNoTranslatableText())
45+
assertFalse("中文".hasNoTranslatableText())
46+
assertFalse("测试123".hasNoTranslatableText())
47+
48+
// Empty/null - should return true
49+
assertTrue("".hasNoTranslatableText())
50+
val nullString: String? = null
51+
assertTrue(nullString.hasNoTranslatableText())
52+
}
53+
}

0 commit comments

Comments
 (0)