Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

3.2 KB
Raw Blame
/*
This file is part of the Notesnook project (https://notesnook.com/)

Copyright (C) 2023 Streetwriters (Private) Limited

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import { writeFileSync, mkdirSync, existsSync } from "fs";
import { createRequire } from "module";
import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url);

const linguiConfig = require("../lingui.config.js");
const locales = linguiConfig.locales.filter((l) => l !== "pseudo-LOCALE");

const LICENSE_HEADER = `/*
This file is part of the Notesnook project (https://notesnook.com/)

Copyright (C) 2023 Streetwriters (Private) Limited

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// THIS FILE IS AUTO-GENERATED BY scripts/generate-locale-loaders.mjs. DO NOT EDIT.
`;

function generateWebLocaleMap() {
  const entries = locales.map((locale) => {
    const key = locale.includes("-") ? `"${locale}"` : locale;
    return `  ${key}: () => import("../../locales/$${locale}.json")`;
  });

  return `${LICENSE_HEADER}
import { type Messages } from "@lingui/core";

export const localeMap: Record<
  string,
  () => Promise<{ default: Messages } | Messages>
> = {
${entries.join(",\n")}
};
`;
}

function generateMobileLocaleLoaders() {
  const entries = locales.map((locale) => {
    const key = locale.includes("-") ? `"${locale}"` : locale;
    return `  ${key}: () => require("@notesnook/intl/dist/locales/$${locale}.json").messages`;
  });

  return `${LICENSE_HEADER}
import { type Messages } from "@lingui/core";

export const localeMap: Record<string, () => Messages> = {
${entries.join(",\n")}
};
`;
}

// 1. Write Web/Desktop locale map
const generatedDir = path.join(__dirname, "../src/generated");
if (!existsSync(generatedDir)) {
  mkdirSync(generatedDir, { recursive: true });
}
writeFileSync(path.join(generatedDir, "locale-map.ts"), generateWebLocaleMap());

// 2. Write Mobile locale loaders
writeFileSync(
  path.join(generatedDir, "loaders.mobile.ts"),
  generateMobileLocaleLoaders()
);