getLanguage.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. operationCancelled: string;
  31. cannotOverrideDirectory: string;
  32. solanaKeygenNotFound: string;
  33. };
  34. defaultToggleOptions: {
  35. active: string;
  36. inactive: string;
  37. };
  38. instructions: {
  39. select: string;
  40. multiselect: string;
  41. };
  42. infos: {
  43. scaffolding: string;
  44. generatingKeypair: string;
  45. done: string;
  46. };
  47. }
  48. /**
  49. *
  50. * This function is used to link obtained locale with correct locale file in order to make locales reusable
  51. *
  52. * @param locale the obtained locale
  53. * @returns locale that linked with correct name
  54. */
  55. function linkLocale(locale: string) {
  56. let linkedLocale: string;
  57. try {
  58. // @ts-ignore
  59. linkedLocale = Intl.getCanonicalLocales(locale)[0];
  60. } catch (error) {
  61. console.log(`${error.toString()}\n`);
  62. }
  63. switch (linkedLocale) {
  64. case "zh-TW":
  65. case "zh-HK":
  66. case "zh-MO":
  67. linkedLocale = "zh-Hant";
  68. break;
  69. case "zh-CN":
  70. case "zh-SG":
  71. linkedLocale = "zh-Hans";
  72. break;
  73. default:
  74. linkedLocale = locale;
  75. }
  76. return linkedLocale;
  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. }