Skip to content

Commit

Permalink
Refactor the locale copying code and add 'zlm' to the installer
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonleenaylor committed Dec 13, 2024
1 parent b74acf5 commit e2bb08b
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 61 deletions.
1 change: 1 addition & 0 deletions Build/Installer.targets
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
<L10nFiles Include="$(OutputDirForConfig)\ur\**\*"/>
<L10nFiles Include="$(OutputDirForConfig)\vi\**\*"/>
<L10nFiles Include="$(OutputDirForConfig)\zh-CN\**\*"/>
<L10nFiles Include="$(OutputDirForConfig)\zlm\**\*"/>
<!-- no need to install the following; most are installed by merge modules -->
<MergeModules Include="$(OutputDirForConfig)\asserts.log"/>
<MergeModules Include="$(OutputDirForConfig)\basicTest.xml"/>
Expand Down
6 changes: 5 additions & 1 deletion Build/Localize.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="Current">

<UsingTask TaskName="CopyLocale" AssemblyFile="FwBuildTasks.dll"/>
<UsingTask TaskName="GoldEticToXliff" AssemblyFile="FwBuildTasks.dll"/>
<UsingTask TaskName="ListsToXliff" AssemblyFile="FwBuildTasks.dll"/>
<UsingTask TaskName="LocalizeFieldWorks" AssemblyFile="FwBuildTasks.dll"/>
Expand All @@ -13,7 +14,7 @@

<!-- Localize strings-en.xml, lists, and all resx files -->
<Target Name="localize" DependsOnTargets="localize-source;localize-binaries"/>
<Target Name="localize-source" DependsOnTargets="Initialize;downloadTranslatedFiles;copyLibL10ns;combineGOLDEtic;zipLocalizedLists;processLanguages-source" Condition="'$(DisableLocalization)'!='true'"/>
<Target Name="localize-source" DependsOnTargets="Initialize;downloadTranslatedFiles;copyLibL10ns;combineGOLDEtic;zipLocalizedLists;copyLocales;processLanguages-source" Condition="'$(DisableLocalization)'!='true'"/>
<Target Name="localize-binaries" DependsOnTargets="Initialize;processLanguages-binaries" Condition="'$(DisableLocalization)'!='true'"/>

<PropertyGroup>
Expand All @@ -34,6 +35,9 @@
<!-- Install overcrowdin, or update if already installed. -->
<Exec WorkingDirectory="$(fwrt)" Command="dotnet tool update -g overcrowdin || dotnet tool install -g overcrowdin"/>
</Target>
<Target Name="copyLocales"> <!-- Copy locales to alternate ws identifiers -->
<CopyLocale SourceL10n="$(L10nsDirectory)/ms" DestL10n="$(L10nsDirectory)/zlm" LcmDir="$(LcmSrcDir)" />
</Target>

<!-- Update localizable files in Crowdin -->
<Target Name="uploadUpdatesForTranslation" DependsOnTargets="CopyLcmResxFiles;InstallOvercrowdin">
Expand Down
3 changes: 1 addition & 2 deletions Build/Src/FwBuildTasks/FwBuildTasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
<RootNamespace>SIL.FieldWorks.Build.Tasks</RootNamespace>
<Description>Additional msbuild tasks for FieldWorks</Description>
<Product>FwBuildTasks</Product>
<TargetFramework>net461</TargetFramework>
<TargetFramework>net462</TargetFramework>
<OutputPath>../..</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" PrivateAssets="All" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
Expand Down
105 changes: 105 additions & 0 deletions Build/Src/FwBuildTasks/Localization/CopyLocale.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) 2024 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)

using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;


namespace SIL.FieldWorks.Build.Tasks.Localization
{
public class CopyLocale : Task
{
[Required]
public string SourceL10n { get; set; }

[Required]
public string DestL10n { get; set; }

[Required]
public string LcmDir { get; set; }

public override bool Execute()
{
var srcLangCode = Path.GetFileName(SourceL10n);
var destLangCode = Path.GetFileName(DestL10n);
if (!Directory.Exists(SourceL10n))
{
Log.LogError($"Source directory '{SourceL10n}' does not exist.");
return false;
}
if (Directory.Exists(DestL10n))
{
Log.LogError($"Destination directory '{DestL10n}' already exists.");
return false;
}
// Create the destination directory
Directory.CreateDirectory(DestL10n);

// Get the files in the source directory and copy to the destination directory
CopyDirectory(SourceL10n, DestL10n, true);

NormalizeLocales.RenameLocaleFiles(DestL10n, srcLangCode, destLangCode);
// Get the files in the source directory and copy to the destination directory
foreach (var file in Directory.GetFiles(LcmDir, "*.resx", SearchOption.AllDirectories))
{
var relativePath = GetRelativePath(LcmDir, file);
Log.LogMessage(MessageImportance.Normal, "CopyLocale: relpath - " + relativePath);
var newFileName = Path.GetFileNameWithoutExtension(file) + $".{destLangCode}.resx";
var newFilePath = Path.Combine(DestL10n, Path.Combine("Src", Path.GetDirectoryName(relativePath)));

// Create the directory for the new file if it doesn't exist
Directory.CreateDirectory(newFilePath);

Log.LogMessage(MessageImportance.Normal, $"CopyLocale: {newFilePath}, {newFileName}");
// Copy the file to the new location
File.Move(file, Path.Combine(newFilePath, newFileName));
}

return true;
}

static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
// From: https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);

// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");

// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();

// Create the destination directory
Directory.CreateDirectory(destinationDir);

// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
}

// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}

static string GetRelativePath(string baseDir, string filePath)
{
Uri baseUri = new Uri(baseDir);
Uri fileUri = new Uri(filePath);
return Uri.UnescapeDataString(baseUri.MakeRelativeUri(fileUri).ToString().Replace('/', Path.DirectorySeparatorChar));
}
}
}
60 changes: 2 additions & 58 deletions Build/Src/FwBuildTasks/Localization/NormalizeLocales.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public override bool Execute()
{
var normalizedLocale = Normalize(locale);
RenameLocale(locale, normalizedLocale);
if (normalizedLocale == "ms")
CopyLocale(normalizedLocale, "zlm");
}
return true;
}
Expand Down Expand Up @@ -55,63 +53,9 @@ private void RenameLocale(string source, string dest)
RenameLocaleFiles(destDir, source, dest);
}

private void CopyLocale(string source, string dest)
internal static void RenameLocaleFiles(string destDirName, string source, string dest, string extension = "*")
{
var sourceDirName = Path.Combine(L10nsDirectory, source);
var destDirName = Path.Combine(L10nsDirectory, dest);
var destDir = new DirectoryInfo(destDirName);

if (destDir.Exists)
{
Log.LogMessage($"'{source}' already exists.");
return;
}
// Create the destination directory
Directory.CreateDirectory(destDirName);

// Get the files in the source directory and copy to the destination directory
CopyDirectory(sourceDirName, destDirName, true);

RenameLocaleFiles(destDirName, source, dest);
}

static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
// From: https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);

// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");

// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();

// Create the destination directory
Directory.CreateDirectory(destinationDir);

// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
}

// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}

private void RenameLocaleFiles(string destDirName, string source, string dest)
{
foreach (var file in Directory.EnumerateFiles(destDirName, "*", SearchOption.AllDirectories))
foreach (var file in Directory.EnumerateFiles(destDirName, extension, SearchOption.AllDirectories))
{
var nameNoExt = Path.GetFileNameWithoutExtension(file);
// ReSharper disable once PossibleNullReferenceException - no files are null
Expand Down
4 changes: 4 additions & 0 deletions FLExInstaller/CustomComponents.wxi
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
<Directory Id="urL10NFOLDER" Name="ur"/>
<Directory Id="viL10NFOLDER" Name="vi"/>
<Directory Id="zhL10NFOLDER" Name="zh-CN"/>
<Directory Id="zlmL10NFOLDER" Name="zlm"/>
</DirectoryRef>

<DirectoryRef Id='HARVESTDATAFOLDER'>
Expand Down Expand Up @@ -225,6 +226,7 @@
<WixVariable Id="MASTERurL10NDIR" Value="$(var.MASTERBUILDDIR)_L10n\ur"/>
<WixVariable Id="MASTERviL10NDIR" Value="$(var.MASTERBUILDDIR)_L10n\vi"/>
<WixVariable Id="MASTERzhL10NDIR" Value="$(var.MASTERBUILDDIR)_L10n\zh-CN"/>
<WixVariable Id="MASTERzlmL10NDIR" Value="$(var.MASTERBUILDDIR)_L10n\zlm"/>
<WixVariable Id="MASTERFONTDIR" Value="$(var.MASTERBUILDDIR)_Font"/>
<?define HarvestWxiDir = .\master?>
<?else?>
Expand Down Expand Up @@ -254,6 +256,7 @@
<WixVariable Id="UPDATEurL10NDIR" Value="$(var.UPDATEBUILDDIR)_L10n\ur"/>
<WixVariable Id="UPDATEviL10NDIR" Value="$(var.UPDATEBUILDDIR)_L10n\vi"/>
<WixVariable Id="UPDATEzhL10NDIR" Value="$(var.UPDATEBUILDDIR)_L10n\zh-CN"/>
<WixVariable Id="UPDATEzlmL10NDIR" Value="$(var.UPDATEBUILDDIR)_L10n\zlm"/>
<WixVariable Id="UPDATEFONTDIR" Value="$(var.UPDATEBUILDDIR)_Font"/>
<?define HarvestWxiDir = .\update?>
<?endif?>
Expand Down Expand Up @@ -284,5 +287,6 @@
<?include $(var.HarvestWxiDir)\urHarvest.wxi?>
<?include $(var.HarvestWxiDir)\viHarvest.wxi?>
<?include $(var.HarvestWxiDir)\zhHarvest.wxi?>
<?include $(var.HarvestWxiDir)\zlmHarvest.wxi?>
<?include Fonts.wxi?>
</Include>

0 comments on commit e2bb08b

Please sign in to comment.