Skip to content

Commit 6bd0bb9

Browse files
feat(srtLoader): Implement SRT file loader (lingodotdev#300)
* feat(cli): implemented srt loader Signed-off-by: Partik <partikbumrah13508@gmail.com> * chore: removed comments Signed-off-by: Partik <partikbumrah13508@gmail.com> * chore: changesets Signed-off-by: Partik <partikbumrah13508@gmail.com> * chore: changesets Signed-off-by: Partik <partikbumrah13508@gmail.com> * chore: fixed build errors Signed-off-by: Partik <partikbumrah13508@gmail.com> * chore: fixed build errors Signed-off-by: Partik <partikbumrah13508@gmail.com> * chore: fixed build errors Signed-off-by: Partik <partikbumrah13508@gmail.com> * feat: changes to the srt loader Signed-off-by: Partik <partikbumrah13508@gmail.com> --------- Signed-off-by: Partik <partikbumrah13508@gmail.com> Co-authored-by: Max Prilutskiy <5614659+maxprilutskiy@users.noreply.github.com>
1 parent e08b264 commit 6bd0bb9

8 files changed

Lines changed: 140 additions & 10 deletions

File tree

.changeset/sixty-suns-look.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@replexica/spec": minor
3+
"@replexica/cli": minor
4+
---
5+
6+
implemented srt file loader and added support for srt file format in spec

bin/act

17.6 MB
Binary file not shown.

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"prettier": "^3.3.3",
4747
"properties-parser": "^0.6.0",
4848
"slugify": "^1.6.6",
49+
"srt-parser-2": "^1.2.3",
4950
"typescript": "^5.6.3",
5051
"vitest": "^2.1.4",
5152
"xml2js": "^0.6.2",

packages/cli/src/loaders/index.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,73 @@ user.password=Contraseña
859859
);
860860
});
861861
});
862+
describe('srt bucket loader', () => {
863+
it('should load srt', async () => {
864+
setupFileMocks();
865+
866+
const input = `
867+
1
868+
00:00:00,000 --> 00:00:01,000
869+
Hello!
870+
871+
2
872+
00:00:01,000 --> 00:00:02,000
873+
World!
874+
`.trim();
875+
const expectedOutput = {"1#00:00:00,000-00:00:01,000": "Hello!","2#00:00:01,000-00:00:02,000": "World!"};
876+
877+
mockFileOperations(input);
878+
879+
const srtLoader = createBucketLoader('srt', 'i18n/[locale].srt');
880+
srtLoader.setDefaultLocale('en');
881+
const data = await srtLoader.pull('en');
882+
883+
expect(data).toEqual(expectedOutput);
884+
});
885+
886+
887+
it('should save srt', async () => {
888+
setupFileMocks();
889+
890+
const input = `
891+
1
892+
00:00:00,000 --> 00:00:01,000
893+
Hello!
894+
895+
2
896+
00:00:01,000 --> 00:00:02,000
897+
World!
898+
`.trim();
899+
900+
const payload = {"1#00:00:00,000-00:00:01,000": "¡Hola!","2#00:00:01,000-00:00:02,000": "Mundo!"}
901+
902+
const expectedOutput = `1
903+
00:00:00,000 --> 00:00:01,000
904+
¡Hola!
905+
906+
2
907+
00:00:01,000 --> 00:00:02,000
908+
Mundo!\n`;
909+
910+
911+
mockFileOperations(input);
912+
913+
const srtLoader = createBucketLoader('srt', 'i18n/[locale].srt');
914+
srtLoader.setDefaultLocale('en');
915+
await srtLoader.pull('en');
916+
917+
await srtLoader.push('es', payload);
918+
919+
expect(fs.writeFile).toHaveBeenCalledWith(
920+
'i18n/es.srt',
921+
expectedOutput,
922+
{ encoding: 'utf-8', flag: 'w' },
923+
);
924+
});
925+
926+
});
927+
928+
862929
});
863930

864931
// Helper functions

packages/cli/src/loaders/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import createXcodeXcstringsLoader from './xcode-xcstrings';
1919
import createPrettierLoader from './prettier';
2020
import createUnlocalizableLoader from './unlocalizable';
2121
import createPoLoader from './po';
22+
import createSrtLoader from './srt';
2223

2324
export default function createBucketLoader(
2425
bucketType: Z.infer<typeof bucketTypeSchema>,
@@ -109,5 +110,10 @@ export default function createBucketLoader(
109110
createFlatLoader(),
110111
createUnlocalizableLoader(),
111112
);
113+
case 'srt': return composeLoaders(
114+
createTextFileLoader(bucketPathPattern),
115+
createSrtLoader(),
116+
createUnlocalizableLoader(),
117+
);
112118
}
113119
}

packages/cli/src/loaders/srt.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { ILoader } from "./_types";
2+
import { createLoader } from './_utils';
3+
import srtParser from "srt-parser-2";
4+
import fs from 'fs/promises';
5+
6+
export default function createSrtLoader(): ILoader<string, Record<string, any>> {
7+
const parser = new srtParser();
8+
return createLoader({
9+
10+
async pull(locale, input) {
11+
const parsed = parser.fromSrt(input) || [];
12+
const result: Record<string, string> = {};
13+
14+
parsed.forEach((entry) => {
15+
const key = `${entry.id}#${entry.startTime}-${entry.endTime}`;
16+
result[key] = entry.text;
17+
});
18+
19+
return result;
20+
},
21+
22+
async push(locale, payload) {
23+
const output = Object.entries(payload).map(([key, text]) => {
24+
const [id, timeRange] = key.split('#');
25+
const [startTime, endTime] = timeRange.split('-');
26+
27+
return {
28+
id: id,
29+
startTime: startTime,
30+
startSeconds: 0,
31+
endTime: endTime,
32+
endSeconds: 0,
33+
text: text
34+
};
35+
});
36+
37+
const srtContent = parser.toSrt(output).trim().replace(/\r?\n/g, '\n');
38+
console.log(srtContent);
39+
40+
return srtContent;
41+
}
42+
});
43+
}

packages/spec/src/formats.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const bucketTypes = [
1414
'yaml-root-key',
1515
'properties',
1616
'po',
17-
17+
'srt',
1818
'compiler',
1919
] as const;
2020

pnpm-lock.yaml

Lines changed: 16 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)