update-i18n.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const path_1 = require("path");
  4. const config_1 = require("../../utility/config");
  5. const dependencies_1 = require("../../utility/dependencies");
  6. const json_utils_1 = require("../../utility/json-utils");
  7. const latest_versions_1 = require("../../utility/latest-versions");
  8. const workspace_models_1 = require("../../utility/workspace-models");
  9. const utils_1 = require("./utils");
  10. function updateI18nConfig() {
  11. return (tree, context) => {
  12. // this is whole process of partial change writing and repeat loading/looping is only necessary
  13. // to workaround underlying issues with the recorder and ast helper functions
  14. const workspacePath = config_1.getWorkspacePath(tree);
  15. let workspaceAst = utils_1.getWorkspace(tree);
  16. // Update extract targets
  17. const extractTargets = utils_1.getTargets(workspaceAst, 'extract-i18n', workspace_models_1.Builders.ExtractI18n);
  18. if (extractTargets.length > 0) {
  19. const recorder = tree.beginUpdate(workspacePath);
  20. for (const { target, project } of extractTargets) {
  21. addProjectI18NOptions(recorder, tree, target, project);
  22. removeExtracti18nDeprecatedOptions(recorder, target);
  23. }
  24. tree.commitUpdate(recorder);
  25. // workspace was changed so need to reload
  26. workspaceAst = utils_1.getWorkspace(tree);
  27. }
  28. // Update base HREF values for existing configurations
  29. let recorder = tree.beginUpdate(workspacePath);
  30. for (const { target } of utils_1.getTargets(workspaceAst, 'build', workspace_models_1.Builders.Browser)) {
  31. updateBaseHrefs(recorder, target);
  32. }
  33. for (const { target } of utils_1.getTargets(workspaceAst, 'test', workspace_models_1.Builders.Karma)) {
  34. updateBaseHrefs(recorder, target);
  35. }
  36. tree.commitUpdate(recorder);
  37. // Remove i18n format option
  38. workspaceAst = utils_1.getWorkspace(tree);
  39. recorder = tree.beginUpdate(workspacePath);
  40. for (const { target } of utils_1.getTargets(workspaceAst, 'build', workspace_models_1.Builders.Browser)) {
  41. removeFormatOption(recorder, target);
  42. }
  43. for (const { target } of utils_1.getTargets(workspaceAst, 'test', workspace_models_1.Builders.Karma)) {
  44. removeFormatOption(recorder, target);
  45. }
  46. tree.commitUpdate(recorder);
  47. // Add new i18n options to build target configurations
  48. workspaceAst = utils_1.getWorkspace(tree);
  49. recorder = tree.beginUpdate(workspacePath);
  50. for (const { target } of utils_1.getTargets(workspaceAst, 'build', workspace_models_1.Builders.Browser)) {
  51. addBuilderI18NOptions(recorder, target, context.logger);
  52. }
  53. tree.commitUpdate(recorder);
  54. // Add new i18n options to test target configurations
  55. workspaceAst = utils_1.getWorkspace(tree);
  56. recorder = tree.beginUpdate(workspacePath);
  57. for (const { target } of utils_1.getTargets(workspaceAst, 'test', workspace_models_1.Builders.Karma)) {
  58. addBuilderI18NOptions(recorder, target, context.logger);
  59. }
  60. tree.commitUpdate(recorder);
  61. return tree;
  62. };
  63. }
  64. exports.updateI18nConfig = updateI18nConfig;
  65. function addProjectI18NOptions(recorder, tree, builderConfig, projectConfig) {
  66. const browserConfig = utils_1.getProjectTarget(projectConfig, 'build', workspace_models_1.Builders.Browser);
  67. if (!browserConfig || browserConfig.kind !== 'object') {
  68. return;
  69. }
  70. // browser builder options
  71. let locales;
  72. const options = utils_1.getAllOptions(browserConfig);
  73. for (const option of options) {
  74. const localeId = json_utils_1.findPropertyInAstObject(option, 'i18nLocale');
  75. if (!localeId || localeId.kind !== 'string') {
  76. continue;
  77. }
  78. const localeFile = json_utils_1.findPropertyInAstObject(option, 'i18nFile');
  79. if (!localeFile || localeFile.kind !== 'string') {
  80. continue;
  81. }
  82. const localIdValue = localeId.value;
  83. const localeFileValue = localeFile.value;
  84. const baseHref = json_utils_1.findPropertyInAstObject(option, 'baseHref');
  85. let baseHrefValue;
  86. if (baseHref) {
  87. if (baseHref.kind === 'string' && baseHref.value !== `/${localIdValue}/`) {
  88. baseHrefValue = baseHref.value;
  89. }
  90. }
  91. else {
  92. // If the configuration does not contain a baseHref, ensure the main option value is used.
  93. baseHrefValue = '';
  94. }
  95. if (!locales) {
  96. locales = {
  97. [localIdValue]: baseHrefValue === undefined
  98. ? localeFileValue
  99. : {
  100. translation: localeFileValue,
  101. baseHref: baseHrefValue,
  102. },
  103. };
  104. }
  105. else {
  106. locales[localIdValue] =
  107. baseHrefValue === undefined
  108. ? localeFileValue
  109. : {
  110. translation: localeFileValue,
  111. baseHref: baseHrefValue,
  112. };
  113. }
  114. }
  115. if (locales) {
  116. // Get sourceLocale from extract-i18n builder
  117. const i18nOptions = utils_1.getAllOptions(builderConfig);
  118. const sourceLocale = i18nOptions
  119. .map(o => {
  120. const sourceLocale = json_utils_1.findPropertyInAstObject(o, 'i18nLocale');
  121. return sourceLocale && sourceLocale.value;
  122. })
  123. .find(x => !!x);
  124. // Add i18n project configuration
  125. json_utils_1.insertPropertyInAstObjectInOrder(recorder, projectConfig, 'i18n', {
  126. locales,
  127. // tslint:disable-next-line: no-any
  128. sourceLocale: sourceLocale,
  129. }, 6);
  130. // Add @angular/localize if not already a dependency
  131. if (!dependencies_1.getPackageJsonDependency(tree, '@angular/localize')) {
  132. dependencies_1.addPackageJsonDependency(tree, {
  133. name: '@angular/localize',
  134. version: latest_versions_1.latestVersions.Angular,
  135. type: dependencies_1.NodeDependencyType.Default,
  136. });
  137. }
  138. }
  139. }
  140. function addBuilderI18NOptions(recorder, builderConfig, logger) {
  141. const options = utils_1.getAllOptions(builderConfig);
  142. for (const option of options) {
  143. const localeId = json_utils_1.findPropertyInAstObject(option, 'i18nLocale');
  144. const i18nFile = json_utils_1.findPropertyInAstObject(option, 'i18nFile');
  145. const outputPath = json_utils_1.findPropertyInAstObject(option, 'outputPath');
  146. if (localeId &&
  147. localeId.kind === 'string' &&
  148. i18nFile &&
  149. outputPath &&
  150. outputPath.kind === 'string') {
  151. if (outputPath.value.match(new RegExp(`[/\\\\]${localeId.value}[/\\\\]?$`))) {
  152. const newOutputPath = outputPath.value.replace(new RegExp(`[/\\\\]${localeId.value}[/\\\\]?$`), '');
  153. const { start, end } = outputPath;
  154. recorder.remove(start.offset, end.offset - start.offset);
  155. recorder.insertLeft(start.offset, `"${newOutputPath}"`);
  156. }
  157. else {
  158. logger.warn(`Output path value "${outputPath.value}" for locale "${localeId.value}" is not supported with the new localization system. ` +
  159. `With the current value, the localized output would be written to "${path_1.posix.join(outputPath.value, localeId.value)}". ` +
  160. `Keeping existing options for the target configuration of locale "${localeId.value}".`);
  161. continue;
  162. }
  163. }
  164. if (localeId && localeId.kind === 'string') {
  165. // add new localize option
  166. json_utils_1.insertPropertyInAstObjectInOrder(recorder, option, 'localize', [localeId.value], 12);
  167. json_utils_1.removePropertyInAstObject(recorder, option, 'i18nLocale');
  168. }
  169. if (i18nFile) {
  170. json_utils_1.removePropertyInAstObject(recorder, option, 'i18nFile');
  171. }
  172. }
  173. }
  174. function removeFormatOption(recorder, builderConfig) {
  175. const options = utils_1.getAllOptions(builderConfig);
  176. for (const option of options) {
  177. // The format is always auto-detected now
  178. const i18nFormat = json_utils_1.findPropertyInAstObject(option, 'i18nFormat');
  179. if (i18nFormat) {
  180. json_utils_1.removePropertyInAstObject(recorder, option, 'i18nFormat');
  181. }
  182. }
  183. }
  184. function updateBaseHrefs(recorder, builderConfig) {
  185. const options = utils_1.getAllOptions(builderConfig);
  186. const mainOptions = json_utils_1.findPropertyInAstObject(builderConfig, 'options');
  187. const mainBaseHref = mainOptions &&
  188. mainOptions.kind === 'object' &&
  189. json_utils_1.findPropertyInAstObject(mainOptions, 'baseHref');
  190. const hasMainBaseHref = !!mainBaseHref && mainBaseHref.kind === 'string' && mainBaseHref.value !== '/';
  191. for (const option of options) {
  192. const localeId = json_utils_1.findPropertyInAstObject(option, 'i18nLocale');
  193. const i18nFile = json_utils_1.findPropertyInAstObject(option, 'i18nFile');
  194. // localize base HREF values are controlled by the i18n configuration
  195. const baseHref = json_utils_1.findPropertyInAstObject(option, 'baseHref');
  196. if (localeId && i18nFile && baseHref) {
  197. // if the main option set has a non-default base href,
  198. // ensure that the augmented base href has the correct base value
  199. if (hasMainBaseHref) {
  200. const { start, end } = baseHref;
  201. recorder.remove(start.offset, end.offset - start.offset);
  202. recorder.insertLeft(start.offset, `"/"`);
  203. }
  204. else {
  205. json_utils_1.removePropertyInAstObject(recorder, option, 'baseHref');
  206. }
  207. }
  208. }
  209. }
  210. function removeExtracti18nDeprecatedOptions(recorder, builderConfig) {
  211. const options = utils_1.getAllOptions(builderConfig);
  212. for (const option of options) {
  213. // deprecated options
  214. json_utils_1.removePropertyInAstObject(recorder, option, 'i18nLocale');
  215. const i18nFormat = option.properties.find(({ key }) => key.value === 'i18nFormat');
  216. if (i18nFormat) {
  217. // i18nFormat has been changed to format
  218. const key = i18nFormat.key;
  219. const offset = key.start.offset + 1;
  220. recorder.remove(offset, key.value.length);
  221. recorder.insertLeft(offset, 'format');
  222. }
  223. }
  224. }