Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

231124 remove deleted msg ids #11

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions webapp/src/app/[lang]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function Home({ params }: { params: { lang: string } }) {
key={msg.id}
message={msg}
onSave={async (text) => {
const res = await fetch(
await fetch(
`/api/translations/${params.lang}/${msg.id}`,
{
body: JSON.stringify({
Expand All @@ -66,7 +66,6 @@ export default function Home({ params }: { params: { lang: string } }) {
},
);

await res.json();
setTranslations((cur) => ({
...cur,
[msg.id]: text,
Expand Down
57 changes: 57 additions & 0 deletions webapp/src/app/api/Store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* global globalThis */
import fs from 'fs/promises';
import { parse } from 'yaml';
import { envVarNotFound, logDebug } from '@/utils/util';
import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git';

const REPO_PATH = process.env.REPO_PATH ?? envVarNotFound('REPO_PATH');
const MAIN_BRANCH = 'main';

export class Store {
public static async getLanguage(lang: string) {
let languages: Map<string, Record<string, unknown>>;
if (!globalThis.languages) {
logDebug('Initializing languages');
const options: Partial<SimpleGitOptions> = {
baseDir: REPO_PATH,
binary: 'git',
maxConcurrentProcesses: 1,
trimmed: false,
};
const git: SimpleGit = simpleGit(options);
logDebug('git checkout main pull...');
await git.checkout(MAIN_BRANCH);
logDebug('git pull...');
await git.pull();
logDebug('git done checkout main branch and pull');
languages = new Map<string, Record<string, unknown>>();
globalThis.languages = languages;
} else {
logDebug('find languages in Memory');
languages = globalThis.languages;
}

let translations: Record<string, unknown>;
if (!languages.has(lang)) {
logDebug('read language[' + lang +'] from file');
// TODO: read this from .lyra.yml setting file in client repo
const yamlPath = REPO_PATH + `/src/locale/${lang}.yml`;

const yamlBuf = await fs.readFile(yamlPath);
// TODO: change parsing to be flattened map of key to value, instead of object
// so key will be like 'key1.key2.key3' and value will be 'translated text'
// this will reduce the cost of looping for the object every time we need to save a message
translations = parse(yamlBuf.toString()) as Record<string, unknown>;
languages.set(lang, translations);
} else {
logDebug('read language [' + lang + '] from Memory');
translations = languages.get(lang) ?? Store.throwLangNotFound(lang);
}

return translations;
}

private static throwLangNotFound(lang: string): never {
throw new Error(`Language ${lang} not found`);
}
}
51 changes: 0 additions & 51 deletions webapp/src/app/api/languages.ts

This file was deleted.

4 changes: 4 additions & 0 deletions webapp/src/app/api/messages/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ const REPO_PATH = process.env.REPO_PATH ?? envVarNotFound('REPO_PATH');

export async function GET() {
const messages: MessageData[] = [];
// TODO: read path or src from .lyra.yml setting file in client repo
for await (const item of getMessageFiles(REPO_PATH + '/src')) {
messages.push(...readTypedMessages(item));
}

// TODO: change data instruction to be a map of key to value, instead of object
// message id is the key, and value is an object with default and params
// example: { 'key1.key2.key3': { default: 'default text', params: [] }}
return NextResponse.json({
data: messages,
});
Expand Down
1 change: 1 addition & 0 deletions webapp/src/app/api/pull-request/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function POST() {
const languages = globalThis.languages;
for (const lang of languages.keys()) {
const yamlPath = REPO_PATH + `/src/locale/${lang}.yml`;
// TODO: when language obj become a map of key to value, then it need to be converted to object before stringify
const yamlOutput = stringify(languages.get(lang), {
doubleQuotedAsJSON: true,
singleQuote: true,
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/app/api/translations/[lang]/[msgId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLanguage } from '@/app/api/languages';
import { Store } from '@/app/api/Store';
import { NextRequest, NextResponse } from 'next/server';

export async function PUT(
Expand All @@ -15,7 +15,7 @@ export async function PUT(
const { text } = payload;

const objKeyPath = msgId.split('.');
let curObj = await getLanguage(lang);
let curObj = await Store.getLanguage(lang);
objKeyPath.forEach((key, index) => {
if (index == objKeyPath.length - 1) {
curObj[key] = text;
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/app/api/translations/[lang]/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getLanguage } from '@/app/api/languages';
import { Store } from '@/app/api/Store';
import { NextRequest, NextResponse } from 'next/server';

export async function GET(
req: NextRequest, // keep this here even if unused
context: { params: { lang: string; msgId: string } },
) {
const lang = context.params.lang;
const langObj = await getLanguage(lang);
const langObj = await Store.getLanguage(lang);
const flattenLangObj = flattenObject(langObj);

return NextResponse.json({
Expand Down