getLanguage.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. anchorCliNotFound: string;
  31. cannotOverrideDirectory: string;
  32. invalidSolanaVersion: string;
  33. operationCancelled: string;
  34. solanaCliNotFound: string;
  35. solanaKeygenFailed: string;
  36. };
  37. defaultToggleOptions: {
  38. active: string;
  39. inactive: string;
  40. };
  41. instructions: {
  42. select: string;
  43. multiselect: string;
  44. };
  45. infos: {
  46. detectAnchorVersion: string;
  47. detectSolanaVersion: string;
  48. generateKeypair: string;
  49. scaffold: string;
  50. done: string;
  51. };
  52. }
  53. /**
  54. *
  55. * This function is used to link obtained locale with correct locale file in order to make locales reusable
  56. *
  57. * @param locale the obtained locale
  58. * @returns locale that linked with correct name
  59. */
  60. function linkLocale(locale: string) {
  61. try {
  62. // @ts-ignore
  63. switch (Intl.getCanonicalLocales(locale)[0]) {
  64. case 'zh-TW':
  65. case 'zh-HK':
  66. case 'zh-MO':
  67. return 'zh-Hant';
  68. break;
  69. case 'zh-CN':
  70. case 'zh-SG':
  71. return 'zh-Hans';
  72. break;
  73. default:
  74. return locale;
  75. }
  76. } catch (error) {
  77. console.log(`${(error as Error).toString()}\n`);
  78. return locale;
  79. }
  80. }
  81. function getLocale() {
  82. const shellLocale =
  83. process.env.LC_ALL || // POSIX locale environment variables
  84. process.env.LC_MESSAGES ||
  85. process.env.LANG ||
  86. Intl.DateTimeFormat().resolvedOptions().locale || // Built-in ECMA-402 support
  87. 'en-US'; // Default fallback
  88. return linkLocale(shellLocale.split('.')[0].replace('_', '-'));
  89. }
  90. export function getLanguage() {
  91. const locale = getLocale();
  92. // Note here __dirname would not be transpiled,
  93. // so it refers to the __dirname of the file `<repositoryRoot>/outfile.cjs`
  94. const localesRoot = path.resolve(__dirname, 'locales');
  95. const languageFilePath = path.resolve(localesRoot, `${locale}.json`);
  96. const doesLanguageExist = fs.existsSync(languageFilePath);
  97. const lang: Language = doesLanguageExist
  98. ? require(languageFilePath)
  99. : require(path.resolve(localesRoot, 'en-US.json'));
  100. return lang;
  101. }