Skip to content

Commit

Permalink
fix: translations
Browse files Browse the repository at this point in the history
  • Loading branch information
zdm committed Feb 17, 2024
1 parent 7981a3b commit 54d4e10
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions lib/package/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,28 @@ if ( process.env.GCLOUD_TRANSLATION_API_KEY ) {

const SQL = {
"schema": sql`
CREATE TABLE IF NOT EXISTS translation (
CREATE TABLE IF NOT EXISTS message (
language text NOT NULL,
msgid text NOT NULL,
msgid_plural text NOT NULL,
id text NOT NULL,
translations json NOT NULL,
PRIMARY KEY ( language, msgid )
PRIMARY KEY ( language, id )
);
`,

"updateTranslations": sql`
INSERT INTO translation
INSERT INTO message
(
language,
msgid,
msgid_plural,
translations
)
VALUES
( ?, ?, ?, ? )
ON CONFLICT ( language, msgid ) DO UPDATE SET
msgid_plural = EXCLUDED.msgid_plural,
ON CONFLICT ( language, id ) DO UPDATE SET
translations = EXCLUDED.translations
`.prepare(),

"getTranslations": sql`SELECT * FROM translation WHERE language = ? AND msgid = ?`.prepare(),
"getTranslatedMessage": sql`SELECT * FROM message WHERE language = ? AND id = ?`.prepare(),
};

export default class {
Expand Down Expand Up @@ -159,6 +156,7 @@ export default class {
for ( const message of Object.values( poFile.messages ) ) {
if ( message.isDisabled ) continue;

// XXX
// delete invalid translation
if ( message.id === message.singleFormTranslation ) {
console.warn( `Failed translation removed:
Expand All @@ -174,37 +172,44 @@ ${ message.singleFormTranslation }
message.setTranslations( null );
}

const translatedMessage = this.#dbh.selectRow( SQL.getTranslations, [
const translatedMessage = this.#dbh.selectRow( SQL.getTranslatedMessage, [

//
poFile.language,
message.id,
] ).data;

// message is fully translated
// XXX
if ( message.isTranslated && !message.isFuzzy ) {
if ( translatedMessage ) {
await this.#resolveConflict( poFile, message, translatedMessage );

// await this.#resolveConflict( poFile, message, translatedMessage );
}

// store to the translatuin memory
else {
this.#storeMessage( poFile.language, message );

// this.#storeMessage( poFile.language, message );
}
}

// message is not translated
else {

// XXX
// get message from the translation memory
if ( translatedMessage ) {
message.isFuzzy = false;
message.setTranslations( translatedMessage.translations );

// message.isFuzzy = false;
// message.setTranslations( translatedMessage.translations );
}

// XXX
// pre-translate single form
else if ( cloudTranslationApi && !message.isSingleFormTranslated ) {
await this.#preTranslate( poFile, message );

// await this.#preTranslate( poFile, message );
}
}
}
Expand All @@ -231,6 +236,7 @@ ${ message.singleFormTranslation }
this.#dbh.exec( SQL.schema );
}

// XXX
async #preTranslate ( poFile, message ) {
const res = await cloudTranslationApi.translate( poFile.language, message.id );

Expand Down Expand Up @@ -264,6 +270,7 @@ ${ res.data }
}
}

// XXX
async #resolveConflict ( poFile, message, translatedMessage ) {
var messageHash, translatedMessageHash;

Expand Down Expand Up @@ -343,6 +350,7 @@ ${ JSON.stringify( message.translations, null, 4 ) }
}
}

// XXX
#storeMessage ( language, { id, pluralId, translations } ) {
this.#dbh.do( SQL.updateTranslations, [

Expand Down

0 comments on commit 54d4e10

Please sign in to comment.