getLanguage.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import * as fs from "node:fs";
  2. import * as path from "node:path";
  3. interface LanguageItem {
  4. hint?: string;
  5. message: string;
  6. invalidMessage?: string;
  7. dirForPrompts?: {
  8. current: string;
  9. target: string;
  10. };
  11. toggleOptions?: {
  12. active: string;
  13. inactive: string;
  14. };
  15. selectOptions?: {
  16. [key: string]: { title: string; desc?: string };
  17. };
  18. }
  19. export interface Language {
  20. programName: LanguageItem;
  21. shouldOverride: LanguageItem;
  22. organizationName: LanguageItem;
  23. programCrateName: LanguageItem;
  24. programAddress: LanguageItem;
  25. programFramework: LanguageItem;
  26. clients: LanguageItem;
  27. jsClientPackageName: LanguageItem;
  28. rustClientCrateName: LanguageItem;
  29. errors: {
  30. cannotOverrideDirectory: string;
  31. invalidSolanaVersion: string;
  32. operationCancelled: string;
  33. solanaKeygenFailed: string;
  34. solanaKeygenNotFound: string;
  35. };
  36. defaultToggleOptions: {
  37. active: string;
  38. inactive: string;
  39. };
  40. instructions: {
  41. select: string;
  42. multiselect: string;
  43. };
  44. infos: {
  45. scaffold: string;
  46. generateKeypair: string;
  47. done: string;
  48. };
  49. }
  50. /**
  51. *
  52. * This function is used to link obtained locale with correct locale file in order to make locales reusable
  53. *
  54. * @param locale the obtained locale
  55. * @returns locale that linked with correct name
  56. */
  57. function linkLocale(locale: string) {
  58. try {
  59. // @ts-ignore
  60. switch (Intl.getCanonicalLocales(locale)[0]) {
  61. case "zh-TW":
  62. case "zh-HK":
  63. case "zh-MO":
  64. return "zh-Hant";
  65. break;
  66. case "zh-CN":
  67. case "zh-SG":
  68. return "zh-Hans";
  69. break;
  70. default:
  71. return locale;
  72. }
  73. } catch (error) {
  74. console.log(`${(error as Error).toString()}\n`);
  75. return locale;
  76. }
  77. }
  78. function getLocale() {
  79. const shellLocale =
  80. process.env.LC_ALL || // POSIX locale environment variables
  81. process.env.LC_MESSAGES ||
  82. process.env.LANG ||
  83. Intl.DateTimeFormat().resolvedOptions().locale || // Built-in ECMA-402 support
  84. "en-US"; // Default fallback
  85. return linkLocale(shellLocale.split(".")[0].replace("_", "-"));
  86. }
  87. export function getLanguage() {
  88. const locale = getLocale();
  89. // Note here __dirname would not be transpiled,
  90. // so it refers to the __dirname of the file `<repositoryRoot>/outfile.cjs`
  91. const localesRoot = path.resolve(__dirname, "locales");
  92. const languageFilePath = path.resolve(localesRoot, `${locale}.json`);
  93. const doesLanguageExist = fs.existsSync(languageFilePath);
  94. const lang: Language = doesLanguageExist
  95. ? require(languageFilePath)
  96. : require(path.resolve(localesRoot, "en-US.json"));
  97. return lang;
  98. }