-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scripts to build localization on local machine (#2701)
* Add scripts to build localization on local machine * Remove unneeded parameters from LocProject * Ignore files generated by local localization build * Add copy translations script to Dist pipeline * Remove destination JSON path parameter * Change scripts to use new translation function * Add json files when script is run locally * Change name for download artifacts pipeline * Add npm commands to run localization scripts * Add README with localization setup instructions * Rename destRESJSON, fix script, and other PR fixes * Silence New-Item and add clean resources task * Use powershell and modify clean script on desktop * Replace cross-env-shell for npm loc:restore script
- Loading branch information
Showing
24 changed files
with
295 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -15,3 +15,7 @@ docs | |
.vscode | ||
|
||
package-lock.json | ||
|
||
Localize/out/ | ||
Localize/packages/ | ||
/**/resources/ |
This file contains 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,8 @@ | ||
steps: | ||
- task: DownloadBuildArtifacts@0 | ||
displayName: 'Download Localization Build Artifacts' | ||
inputs: | ||
buildType: 'current' | ||
downloadType: 'single' | ||
artifactName: 'drop' | ||
downloadPath: '$(Agent.BuildDirectory)' |
This file contains 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 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 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 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 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 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 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,42 @@ | ||
# build.ps1 | ||
$RepoRoot = (Resolve-Path "$PSScriptRoot\..").Path | ||
$LocProject = Join-Path $PSScriptRoot "LocProject.json" | ||
$LocalizationXLocPkgVer = "2.0.7" | ||
|
||
$OutDir = Join-Path $PSScriptRoot "out" | ||
$NUGET_PACKAGES = Join-Path $PSScriptRoot "packages" | ||
$XLocPath = Join-Path $NUGET_PACKAGES "Localization.XLoc.$LocalizationXLocPkgVer" | ||
|
||
# Uncomment and set the following variable if needed | ||
# $LocCustomPowerShellScript = "{path to the script to customize localized files}" | ||
|
||
Write-Host "Running Localization..." | ||
|
||
# Set the RepoRoot and NUGET_PACKAGES environment variables | ||
$env:RepoRoot = $RepoRoot | ||
$env:NUGET_PACKAGES = $NUGET_PACKAGES | ||
|
||
# Delete and re-create the output directory if it already exists | ||
# The following code deletes and re-creates the Localize/out directory to ensure it is empty and ready to receive the | ||
# generated localization files. This is necessary because if the directory already exists but is empty or partially empty, | ||
# the script will not move any files into it, and the localization files will not be generated. | ||
if (Test-Path -Path "$OutDir") { | ||
Remove-Item -Path "$OutDir" -Recurse -Force | ||
} | ||
New-Item -ItemType Directory -Path "$OutDir" > $null | ||
|
||
& "$XLocPath\tools\netfx\Microsoft.Localization.XLoc.exe" /f "$LocProject" | ||
$localizationExitCode = $LASTEXITCODE | ||
|
||
# Clear the RepoRoot and NUGET_PACKAGES environment variables | ||
Remove-Item Env:\RepoRoot | ||
Remove-Item Env:\NUGET_PACKAGES | ||
|
||
# Move the generated files to the desired output directory | ||
if ($localizationExitCode -eq 0) { | ||
Move-Item -Path "$RepoRoot\out\*" -Destination $OutDir -Force | ||
Remove-Item -Path "$RepoRoot\out" -Recurse -Force | ||
} | ||
|
||
Write-Host "Localization finished with exit code '$localizationExitCode'." | ||
Exit $localizationExitCode |
This file contains 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,105 @@ | ||
param ( | ||
[string]$artifactsPath = "" | ||
) | ||
|
||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition | ||
|
||
# If artifacts path is empty, then this script is run locally | ||
# But if artifactsPath is set, then the script is run on the Azure DevOps path | ||
if ($artifactsPath -eq "") { | ||
$sourceRoot = Join-Path $scriptDir "out/loc" | ||
} else { | ||
$sourceRoot = $artifactsPath | ||
} | ||
|
||
$packageNames = @("common", "service", "playground", "react") | ||
|
||
# Get language directories | ||
$languageDirs = Get-ChildItem -Path $sourceRoot -Directory | ||
|
||
# If this script is run locally, in addition to the resjson files, it adds a json directory containing json files for local development | ||
# If script is run on ADO, it only adds the resjson files used in production | ||
|
||
# Strip out resjson comments and resjson-specific formatting before writing the result to json file | ||
function Convert-ResjsonToJson { | ||
param ( | ||
[string]$sourcePath, | ||
[string]$targetPath | ||
) | ||
|
||
$content = Get-Content -Path $sourcePath -Raw | ||
|
||
$strippedContent = $content -replace '//.*$' -replace '/\*[\s\S]*?\*/' -replace ',\s*}' -replace ',\s*]' | ||
$parsedContent = $strippedContent | ConvertFrom-Json | ||
$cleanContent = New-Object -TypeName PSObject | ||
|
||
foreach ($key in $parsedContent.PSObject.Properties.Name) { | ||
if (-not $key.StartsWith("_")) { | ||
$cleanContent | Add-Member -MemberType NoteProperty -Name $key -Value $parsedContent.$key | ||
} | ||
} | ||
|
||
$cleanedJsonContent = $cleanContent | ConvertTo-Json -Depth 100 | ||
Set-Content -Path $targetPath -Value $cleanedJsonContent | ||
} | ||
|
||
Write-Host "Copying translation files" | ||
|
||
foreach ($languageDir in $languageDirs) { | ||
$languageId = $languageDir.Name | ||
Write-Verbose "Processing language: $languageId" | ||
|
||
# Process package directories | ||
foreach ($packageName in $packageNames) { | ||
$sourcePath = Join-Path $($languageDir.FullName) "packages/$packageName/i18n/resources.resjson" | ||
$targetDir = (Join-Path $scriptDir ".." -Resolve) | Join-Path -ChildPath "packages/$packageName/resources/i18n/resjson" | ||
$targetPath = Join-Path $targetDir "resources.$languageId.resjson" | ||
|
||
Write-Verbose "Checking source path: $sourcePath" | ||
if (Test-Path $sourcePath) { | ||
Write-Verbose "Source path exists, preparing target directory" | ||
if (-not (Test-Path $targetDir)) { | ||
New-Item -ItemType Directory -Force -Path $targetDir > $null | ||
} | ||
|
||
Write-Verbose "Copying file from $sourcePath to $targetPath" | ||
Copy-Item -Path $sourcePath -Destination $targetPath | ||
|
||
$jsonTargetDir = $targetDir.Replace("resjson", "json") | ||
$jsonTargetPath = $targetPath.Replace("resjson", "json") | ||
|
||
if (-not (Test-Path $jsonTargetDir)) { | ||
New-Item -ItemType Directory -Force -Path $jsonTargetDir > $null | ||
} | ||
|
||
Write-Verbose "Converting resjson to json: $jsonTargetPath" | ||
Convert-ResjsonToJson -sourcePath $sourcePath -targetPath $jsonTargetPath | ||
} | ||
} | ||
|
||
# Handle the desktop directory exception | ||
$desktopSource = Join-Path $($languageDir.FullName) "desktop/i18n/resources.resjson" | ||
$desktopTargetDir = (Join-Path $scriptDir "../desktop/resources/i18n/resjson") | ||
$desktopTarget = Join-Path $desktopTargetDir "resources.$languageId.resjson" | ||
|
||
Write-Verbose "Checking desktop source path: $desktopSource" | ||
if (Test-Path $desktopSource) { | ||
Write-Verbose "Desktop source path exists, preparing target directory" | ||
if (-not (Test-Path $desktopTargetDir)) { | ||
New-Item -ItemType Directory -Force -Path $desktopTargetDir > $null | ||
} | ||
|
||
Write-Verbose "Copying file from $desktopSource to $desktopTarget" | ||
Copy-Item -Path $desktopSource -Destination $desktopTarget | ||
|
||
$jsonDesktopTargetDir = $desktopTargetDir.Replace("resjson", "json") | ||
$jsonDesktopTarget = $desktopTarget.Replace("resjson", "json") | ||
|
||
if (-not (Test-Path $jsonDesktopTargetDir)) { | ||
New-Item -ItemType Directory -Force -Path $jsonDesktopTargetDir > $null | ||
} | ||
|
||
Write-Verbose "Converting desktop resjson to json: $jsonDesktopTarget" | ||
Convert-ResjsonToJson -sourcePath $desktopSource -targetPath $jsonDesktopTarget | ||
} | ||
} |
This file contains 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,3 @@ | ||
nuget install Localization.XLoc -OutputDirectory packages -Verbosity detailed -Source https://azurebatch.pkgs.visualstudio.com/_packaging/BatchExplorer/nuget/v3/index.json | ||
nuget install LSBuild.XLoc -OutputDirectory packages -Verbosity detailed -Source https://azurebatch.pkgs.visualstudio.com/_packaging/BatchExplorer/nuget/v3/index.json | ||
nuget install Localization.Languages -OutputDirectory packages -Verbosity detailed -Source https://azurebatch.pkgs.visualstudio.com/_packaging/BatchExplorer/nuget/v3/index.json |
This file contains 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 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 @@ | ||
# Building Localization on Local Machine | ||
|
||
## Building the English File | ||
|
||
To build the latest English translation resjson file from the YAML files: | ||
|
||
* Run `npm run build` to build translations for the entire repository | ||
* Run `npm run build-translations` in any package directory (desktop, packages/*) to build translations for a specific package | ||
|
||
The output will be in `{packageName}/i18n/resources.resjson` | ||
|
||
## Building Translations Files for Other Languages (Windows-Only) | ||
|
||
To build the localization translations for all languages besides English: | ||
|
||
* Follow the steps above first | ||
* Install the latest, recommended version of nuget.exe from <https://www.nuget.org/downloads> at C:\Users\{userName}, for instance. | ||
* Navigate to the root of the repository | ||
* Run `npm run loc:restore` to install all dependencies | ||
* Run `npm run loc:build` to build the translations and move them to their correct directories | ||
* If needed, run `npm run clean` to clear out all previously built translation files | ||
|
||
The output will be in `{packageName}/resources/i18n` | ||
|
||
* `{packageName}/resources/i18n/resjson` contains RESJSON translations | ||
* `{packageName}/resources/i18n/json` contains JSON translations (RESJSON syntax and comments have been stripped out) |
This file contains 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 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 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.