Skip to content

Commit c9bb951

Browse files
authored
Merge pull request #55 from simplelocalize/adjust-language-key
Adjust language key processing
2 parents 7a8a8bd + 9bbf929 commit c9bb951

File tree

5 files changed

+122
-12
lines changed

5 files changed

+122
-12
lines changed

junit/empty-test/strings.xml

Whitespace-only changes.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>io.simplelocalize</groupId>
66
<artifactId>simplelocalize-cli</artifactId>
7-
<version>2.0.2</version>
7+
<version>2.0.3</version>
88
<packaging>jar</packaging>
99
<name>simplelocalize-cli</name>
1010
<description>Official SimpleLocalize Command Line Interface</description>

src/main/java/io/simplelocalize/cli/Version.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class Version
44
{
55

6-
public static final String NUMBER = "2.0.2";
6+
public static final String NUMBER = "2.0.3";
77

88
private Version()
99
{

src/main/java/io/simplelocalize/cli/command/UploadCommand.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,46 @@ public void invoke()
5555
}
5656

5757
log.info(" 📄 Found {} files to upload", filesToUpload.size());
58-
String languageKey = configuration.getLanguageKey();
5958
for (FileToUpload fileToUpload : filesToUpload)
6059
{
6160
try
6261
{
63-
String fileLanguageKey = Optional.of(fileToUpload).map(FileToUpload::getLanguage).orElse("");
64-
boolean hasLanguageKey = StringUtils.isNotBlank(languageKey);
65-
boolean isLanguageMatching = fileLanguageKey.equals(languageKey);
66-
if (!isLanguageMatching && hasLanguageKey)
62+
long length = fileToUpload.getPath().toFile().length();
63+
if (length == 0)
6764
{
68-
log.info(" 🤔 Skipping '{}' language file: {}", fileToUpload.getLanguage(), fileToUpload.getPath());
65+
log.warn(" 🤔 Skipping empty file: {}", fileToUpload.getPath());
6966
continue;
7067
}
7168

72-
long length = fileToUpload.getPath().toFile().length();
73-
if (length == 0)
69+
String fileLanguageKey = Optional.of(fileToUpload).map(FileToUpload::getLanguage).orElse("");
70+
boolean hasFileLanguageKey = StringUtils.isNotBlank(fileLanguageKey);
71+
72+
String configurationLanguageKey = configuration.getLanguageKey();
73+
boolean hasConfigurationLanguageKey = StringUtils.isNotBlank(configurationLanguageKey);
74+
75+
boolean isLanguageMatching = fileLanguageKey.equals(configurationLanguageKey);
76+
if (hasFileLanguageKey && hasConfigurationLanguageKey && !isLanguageMatching)
7477
{
75-
log.warn(" 🤔 Skipping empty file: {}", fileToUpload.getPath());
78+
log.info(" 🤔 Skipping '{}' language, file: {}", fileToUpload.getLanguage(), fileToUpload.getPath());
7679
continue;
7780
}
7881

82+
String requestLanguageKey = fileLanguageKey;
83+
if (hasConfigurationLanguageKey && !hasFileLanguageKey)
84+
{
85+
requestLanguageKey = configurationLanguageKey;
86+
}
87+
88+
if (!hasFileLanguageKey && !hasConfigurationLanguageKey)
89+
{
90+
log.info(" 🤔 Uploading only translation keys, language key not present in '--uploadPath' nor '--languageKey' parameter, file: {}", fileToUpload.getPath());
91+
}
92+
7993
String uploadFormat = configuration.getUploadFormat();
8094
List<String> uploadOptions = configuration.getUploadOptions();
8195
UploadRequest uploadRequest = anUploadFileRequest()
8296
.withPath(fileToUpload.getPath())
83-
.withLanguageKey(fileLanguageKey)
97+
.withLanguageKey(requestLanguageKey)
8498
.withNamespace(fileToUpload.getNamespace())
8599
.withFormat(uploadFormat)
86100
.withOptions(uploadOptions)

src/test/java/io/simplelocalize/cli/command/UploadCommandTest.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,102 @@ public void shouldUploadTwelveFiles() throws Exception
4545
);
4646
}
4747

48+
@Test
49+
public void shouldUploadOneFileWithLangTemplate() throws Exception
50+
{
51+
//given
52+
Configuration configuration = new Configuration();
53+
configuration.setApiKey("my-api-key");
54+
configuration.setUploadPath("./junit/download-test/values-{lang}/strings.xml");
55+
configuration.setLanguageKey("en");
56+
configuration.setUploadFormat("android");
57+
58+
//when
59+
UploadCommand uploadCommand = new UploadCommand(client, configuration);
60+
uploadCommand.invoke();
61+
62+
//then
63+
Mockito.verify(client, Mockito.times(1)).uploadFile(
64+
Mockito.refEq(UploadRequest.UploadFileRequestBuilder.anUploadFileRequest()
65+
.withPath(Path.of("./junit/download-test/values-{lang}/strings.xml"))
66+
.withFormat("android")
67+
.withLanguageKey("en")
68+
.withOptions(Collections.emptyList())
69+
.build(),
70+
"path"
71+
)
72+
);
73+
}
74+
75+
@Test
76+
public void shouldUploadOneFileWithOnlyTranslationKeys() throws Exception
77+
{
78+
//given
79+
Configuration configuration = new Configuration();
80+
configuration.setApiKey("my-api-key");
81+
configuration.setUploadPath("./junit/download-test/values-en/strings.xml");
82+
configuration.setUploadFormat("android");
83+
84+
//when
85+
UploadCommand uploadCommand = new UploadCommand(client, configuration);
86+
uploadCommand.invoke();
87+
88+
//then
89+
Mockito.verify(client, Mockito.times(1)).uploadFile(
90+
Mockito.refEq(UploadRequest.UploadFileRequestBuilder.anUploadFileRequest()
91+
.withPath(Path.of("./junit/download-test/values-en/strings.xml"))
92+
.withFormat("android")
93+
.withLanguageKey("")
94+
.withOptions(Collections.emptyList())
95+
.build()
96+
)
97+
);
98+
}
99+
100+
@Test
101+
public void shouldSkipEmptyFile() throws Exception
102+
{
103+
//given
104+
Configuration configuration = new Configuration();
105+
configuration.setApiKey("my-api-key");
106+
configuration.setUploadPath("./junit/empty-test/strings.xml");
107+
configuration.setUploadFormat("android");
108+
109+
//when
110+
UploadCommand uploadCommand = new UploadCommand(client, configuration);
111+
uploadCommand.invoke();
112+
113+
//then
114+
Mockito.verifyNoInteractions(client);
115+
}
116+
117+
@Test
118+
public void shouldUploadOneFile() throws Exception
119+
{
120+
//given
121+
Configuration configuration = new Configuration();
122+
configuration.setApiKey("my-api-key");
123+
configuration.setUploadPath("./junit/download-test/values-en/strings.xml");
124+
configuration.setLanguageKey("en");
125+
configuration.setUploadFormat("android");
126+
127+
//when
128+
UploadCommand uploadCommand = new UploadCommand(client, configuration);
129+
uploadCommand.invoke();
130+
131+
//then
132+
Mockito.verify(client, Mockito.times(1)).uploadFile(
133+
Mockito.refEq(UploadRequest.UploadFileRequestBuilder.anUploadFileRequest()
134+
.withPath(Path.of("./junit/download-test/values-{lang}/strings.xml"))
135+
.withFormat("android")
136+
.withLanguageKey("en")
137+
.withOptions(Collections.emptyList())
138+
.build(),
139+
"path"
140+
)
141+
);
142+
}
143+
48144
@Test
49145
public void shouldUploadZeroFiles() throws Exception
50146
{

0 commit comments

Comments
 (0)