{"id":15039801,"url":"https://github.com/automattic/i18n-loader-webpack-plugin","last_synced_at":"2025-10-19T18:31:48.134Z","repository":{"id":43207699,"uuid":"436180112","full_name":"Automattic/i18n-loader-webpack-plugin","owner":"Automattic","description":"[READ ONLY] A Webpack plugin to load WordPress i18n when Webpack lazy-loads a bundle. This repository is a mirror, for issue tracking and development head to: https://github.com/automattic/jetpack","archived":false,"fork":false,"pushed_at":"2025-02-03T21:00:43.000Z","size":185,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"trunk","last_synced_at":"2025-02-03T22:18:56.079Z","etag":null,"topics":["webpack","webpack-plugin"],"latest_commit_sha":null,"homepage":"https://github.com/Automattic/jetpack/","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Automattic.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-08T08:58:10.000Z","updated_at":"2025-02-03T21:00:46.000Z","dependencies_parsed_at":"2024-02-05T18:55:34.618Z","dependency_job_id":"942dcc2b-2556-457e-9fde-a06e2f9fa95b","html_url":"https://github.com/Automattic/i18n-loader-webpack-plugin","commit_stats":null,"previous_names":[],"tags_count":73,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automattic%2Fi18n-loader-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automattic%2Fi18n-loader-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automattic%2Fi18n-loader-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automattic%2Fi18n-loader-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Automattic","download_url":"https://codeload.github.com/Automattic/i18n-loader-webpack-plugin/tar.gz/refs/heads/trunk","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237191873,"owners_count":19269775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["webpack","webpack-plugin"],"created_at":"2024-09-24T20:43:51.115Z","updated_at":"2025-10-19T18:31:42.798Z","avatar_url":"https://github.com/Automattic.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# i18n-loader-webpack-plugin\n\nA Webpack plugin to load WordPress translation data for [@wordpress/i18n] when Webpack lazy-loads a bundle.\n\n## Installation\n\nGenerally you'll install this via your package manager, e.g.\n\n```\nnpm install --save-dev @automattic/i18n-loader-webpack-plugin\n```\n\n## Usage\n\n### Basic usage\n\nThis goes in the `plugins` section of your Webpack config, e.g.\n```js\n{\n\tplugins: [\n\t\tnew I18nLoaderWebpackPlugin( {\n\t\t\ttextdomain: 'mydomain',\n\t\t} ),\n\t],\n};\n```\n\nParameters recognized by the plugin are:\n\n- `textdomain`: The text domain used in the JavaScript code. This is required, unless nothing in your JS actually uses [@wordpress/i18n].\n- `loaderModule`: The name of a module supplying the i18n loader. See [Loader module](#loader-module) below for details.\n- `loaderMethod`: The name of the function from `loaderModule` to download the i18n. See [Loader module](#loader-module) below for details.\n- `target`: The target of the build: 'plugin' (the default), 'theme', or 'core'. This is used to determine where in WordPress's languages directory to look for the translation files.\n- `path`: See [Webpack context](#webpack-context) below for details.\n- `ignoreModules`: If some bundles in your build depend on [@wordpress/i18n] for purposes other than translating strings, i18n-loader-webpack-plugin will none the less count them as \"using @wordpress/i18n\" which may result in it trying to load translations for bundles that do not need it. This option may be used to ignore the relevant source files when examining the bundles.\n\n  The value may be a function, which will be passed the file path relative to [Webpack's context] and the Webpack Module object and which should return true if the file should be ignored, or a string or RegExp to be compared with the relative file path, or an array of such strings, RegExps, and/or functions.\n\n### Use of [@wordpress/i18n]\n\nFor best results, each JavaScript file translating strings should import @wordpress/i18n directly to access `__()` and similar methods.\nThe use of @wordpress/i18n in the lazy bundle may not be detected if `__()` is imported indirectly or used by accessing the global `wp.i18n`.\n\nOf course, you don't actually want @wordpress/i18n included in the bundle. The recommended way to handle this is to use [@wordpress/dependency-extraction-webpack-plugin] in your Webpack configuration.\nBut if for some reason you want to do it manually, something like this in your Webpack config should work:\n```js\n{\n\texternals: {\n\t\t'@wordpress/i18n': [ 'window wp', 'i18n' ],\n\t}\n}\n```\n\n### Loader module\n\nIn order to load the translations, the generated bundle needs to call a method to do the actual downloading at runtime. This is handled by loading a module that must be externalized by your Webpack configuration.\n\nThe default module name is `@wordpress/jp-i18n-loader`, which will automatically be externalized by [@wordpress/dependency-extraction-webpack-plugin], which will also register it as a dependency for \"wp-jp-i18n-loader\" in the generated `.asset.php` file. That, in turn, is provided by the [automattic/jetpack-assets] Composer package (which also provides an `Automattic\\Jetpack\\Assets::register_script()` function to easily consume the `.asset.php` file).\n\nBut if for some reason you don't want to use those packages, you can set the plugin's `loaderModule` and `loaderMethod` options to point to a different module name, use [Webpack's externals configuration] to externalize it, and appropriate PHP code to provide the corresponding global variable for your externals configuration to retrieve.\n\nThe loader method might be documented like this:\n```js\n/**\n * Download and register translations for a bundle.\n *\n * @param {string} path - Bundle path being fetched. May have a query part.\n * @param {string} domain - Text domain to register into.\n * @param {string} location - Location for the translation: 'plugin', 'theme', or 'core'.\n * @returns {Promise} Resolved when the translations are registered, or rejected with an `Error`.\n */\n```\nMost likely the method will separate any query part from the path, hash it, build the download url, fetch it, then register it via `@wordpress/i18n`'s `setLocaleData()` method.\n\n### Webpack context\n\nWordPress's translation infrastructure generates a file for each JS script named like \"_textdomain_-_locale_-_hash_.json\". The _hash_ is an MD5 hash of the path of the script file relative to the plugin's root.\n\nI18n-loader-webpack-plugin assumes that [Webpack's context] is the base of the WordPress package or plugin in which the bundles will be included, and that the [loader module](#loader-module) will handle mapping from package root to plugin root.\nIf this is not the case, you'll need to set the plugin's `path` parameter to the relative path from the plugin's root to Webpack's `output.path`.\n\n### Other useful Webpack configuration\n\nIf you're having Webpack minify your bundle, you'll likely also want to do the following:\n\n* Be sure that you're not naming the output files with an extension `.min.js`, as that will cause the WordPress translation infrastructure to ignore them.\n* Set `optimization.concatenateModules` to false, as that optimization can mangle the i18n method names.\n* Configure Terser to not mangle `__`, `_n`, `_nx`, and `_x`.\n* Use [@automattic/babel-plugin-preserve-i18n](https://www.npmjs.com/package/@automattic/babel-plugin-preserve-i18n) to help further preserve those method names.\n* Configure Terser to preserve comments starting with \"Translators\" or \"translators\" in the output.\n* Check your code to avoid [certain constructs](https://github.com/Automattic/jetpack/blob/trunk/projects/js-packages/i18n-check-webpack-plugin/README.md#known-problematic-code-patterns) that will dissociate translator comments from the i18n method calls when the minifier rearranges the code.\n\n### Use in Composer packages\n\nComposer packages are useful for holding shared code, but when it comes to WordPress plugin i18n there are some problems.\n\nWordPress's plugin infrastructure doesn't natively support Composer packages, so the usual thing to do is to include the `vendor/` directory in the push to the WordPress.org SVN.\nThat won't work for packages needing translation, though, as WordPress's translation infrastructure ignores the `vendor/` directory when looking for strings to be translated.\nYou'll need to use something like [automattic/jetpack-composer-plugin] so that the composer packages with translated strings are installed to a different path.\n\nAlso, as the translation file will be named using the plugin's textdomain rather than the Composer package's, the consuming plugin will also need to arrange for the [loader module](#loader-module) to fetch the proper file.\nThis may be done using [automattic/jetpack-assets] along with [automattic/jetpack-composer-plugin], as described in the latter's documentation.\n\n## Security\n\nNeed to report a security vulnerability? Go to [https://automattic.com/security/](https://automattic.com/security/) or directly to our security bug bounty site [https://hackerone.com/automattic](https://hackerone.com/automattic).\n\n## License\n\ni18n-loader-webpack-plugin is licensed under [GNU General Public License v2 (or later)](./LICENSE.txt)\n\n[@wordpress/i18n]: https://www.npmjs.com/package/@wordpress/i18n\n[@wordpress/dependency-extraction-webpack-plugin]: https://www.npmjs.com/package/@wordpress/dependency-extraction-webpack-plugin\n[automattic/jetpack-assets]: https://packagist.org/packages/automattic/jetpack-assets\n[automattic/jetpack-composer-plugin]: https://packagist.org/packages/automattic/jetpack-composer-plugin\n[Webpack's context]: https://webpack.js.org/configuration/entry-context/#context\n[Webpack's externals configuration]: https://webpack.js.org/configuration/externals/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fautomattic%2Fi18n-loader-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fautomattic%2Fi18n-loader-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fautomattic%2Fi18n-loader-webpack-plugin/lists"}