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