-
Notifications
You must be signed in to change notification settings - Fork 57
Bundle WordPress translation files with download retry and failure handling #2575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
364b74d
Bundle WordPress translation files for faster site creation (#2558)
ivan-ottinger bd691fa
Move language pack downloads out of postinstall into npm run make
ivan-ottinger 1a272b6
Skip translation downloads for older bundled themes
ivan-ottinger 60380fe
Add retry with exponential backoff to language pack downloads
ivan-ottinger 7672a87
Add Hungarian locale to language pack downloads
ivan-ottinger 99abb52
Refactor fetchWithRetry to use recursion instead of for loop
ivan-ottinger 5bcca78
Remove redundant 'Files extracted' log message
ivan-ottinger a11ea8d
Rename download:language-packs script to download-language-packs
ivan-ottinger dd1270d
Rename usedBundledPacks to isUsingBundledLanguagePacks for clarity
ivan-ottinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import { pathExists } from 'common/lib/fs-utils'; | ||
| import { getLanguagePacksPath } from 'cli/lib/server-files'; | ||
|
|
||
| /** | ||
| * Filters files in a directory that match a given WordPress locale. | ||
| * Matches .l10n.php files ({locale}.l10n.php, {domain}-{locale}.l10n.php) | ||
| * and .json files ({locale}-{hash}.json). | ||
| */ | ||
| function filterLocaleFiles( files: string[], wpLocale: string ): string[] { | ||
| return files.filter( ( file ) => { | ||
| const name = path.basename( file ); | ||
| return ( | ||
| name === `${ wpLocale }.l10n.php` || | ||
| name.endsWith( `-${ wpLocale }.l10n.php` ) || | ||
| ( name.startsWith( `${ wpLocale }-` ) && name.endsWith( '.json' ) ) | ||
| ); | ||
| } ); | ||
| } | ||
|
|
||
| /** | ||
| * Copies matching locale files from a source directory to a destination directory. | ||
| */ | ||
| async function copyLocaleFiles( | ||
| sourceDir: string, | ||
| destDir: string, | ||
| wpLocale: string | ||
| ): Promise< number > { | ||
| if ( ! ( await pathExists( sourceDir ) ) ) { | ||
| return 0; | ||
| } | ||
| const allFiles = await fs.promises.readdir( sourceDir ); | ||
| const localeFiles = filterLocaleFiles( allFiles, wpLocale ); | ||
| if ( localeFiles.length === 0 ) { | ||
| return 0; | ||
| } | ||
| await fs.promises.mkdir( destDir, { recursive: true } ); | ||
| for ( const file of localeFiles ) { | ||
| await fs.promises.copyFile( path.join( sourceDir, file ), path.join( destDir, file ) ); | ||
| } | ||
| return localeFiles.length; | ||
| } | ||
|
|
||
| /** | ||
| * Copies bundled WordPress language pack files for a given locale into a site's | ||
| * wp-content/languages/ directory, including core, plugin, and theme translations. | ||
| * Returns true if files were copied successfully. | ||
| */ | ||
| export async function copyLanguagePackToSite( | ||
| sitePath: string, | ||
| wpLocale: string | ||
| ): Promise< boolean > { | ||
| const languagePacksDir = getLanguagePacksPath(); | ||
| if ( ! ( await pathExists( languagePacksDir ) ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| const destBase = path.join( sitePath, 'wp-content', 'languages' ); | ||
|
|
||
| let totalCopied = 0; | ||
| totalCopied += await copyLocaleFiles( languagePacksDir, destBase, wpLocale ); | ||
| totalCopied += await copyLocaleFiles( | ||
| path.join( languagePacksDir, 'plugins' ), | ||
| path.join( destBase, 'plugins' ), | ||
| wpLocale | ||
| ); | ||
| totalCopied += await copyLocaleFiles( | ||
| path.join( languagePacksDir, 'themes' ), | ||
| path.join( destBase, 'themes' ), | ||
| wpLocale | ||
| ); | ||
|
|
||
| return totalCopied > 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * WordPress locale codes for all non-English locales supported by Studio. | ||
| * Used for downloading and installing WordPress core language packs. | ||
| */ | ||
| export const WP_LOCALES = [ | ||
| 'ar', | ||
| 'de_DE', | ||
| 'es_ES', | ||
| 'fr_FR', | ||
| 'he_IL', | ||
| 'hu_HU', | ||
| 'id_ID', | ||
| 'it_IT', | ||
| 'ja', | ||
| 'ko_KR', | ||
| 'nl_NL', | ||
| 'pl_PL', | ||
| 'pt_BR', | ||
| 'ru_RU', | ||
| 'sv_SE', | ||
| 'tr_TR', | ||
| 'vi', | ||
| 'uk', | ||
| 'zh_CN', | ||
| 'zh_TW', | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.