Last active
May 14, 2023 07:37
-
-
Save RobbWatershed/07c4c7623c2932d7d8007122f1e4a0e6 to your computer and use it in GitHub Desktop.
fromXls.ps1
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
Get-ChildItem '.' -Directory | ForEach-Object { | |
$folder = $_ | |
if (!$folder.Name.StartsWith('values') -or $folder.Name.EndsWith('dp')) { return } | |
$isEN = ($folder.Name -eq "values") | |
Write-Output $folder.Name | |
Get-ChildItem $folder -File | ForEach-Object { | |
$file = $_ | |
$fileName = $file.BaseName | |
if (!$file.FullName.EndsWith('csv')) { return } | |
if ($file.FullName.EndsWith('_final.csv')) { return } | |
if (!($fileName.StartsWith('strings') -or $fileName.StartsWith('array'))) { return } | |
Write-Output $file.FullName | |
# pre-sort CSV | |
$finalPath = $file.DirectoryName+"/"+$file.BaseName+"_final.csv" | |
Import-Csv $file.FullName | sort Key | Export-Csv -Path $finalPath -Encoding utf8 -NoTypeInformation | |
} | |
Get-ChildItem $folder -File | ForEach-Object { | |
$file = $_ | |
$fileName = $file.BaseName | |
if (!$file.FullName.EndsWith('_final.csv')) { return } | |
if (!($fileName.StartsWith('strings') -or $fileName.StartsWith('array'))) { return } | |
$xmlsettings = New-Object System.Xml.XmlWriterSettings | |
$xmlsettings.Indent = $true | |
$xmlWriter = [System.XML.XmlWriter]::Create($file.FullName+"_.xml", $xmlsettings) | |
$xmlWriter.WriteStartDocument() | |
$xmlWriter.WriteStartElement('resources') | |
$xmlWriter.WriteAttributeString("xmlns","tools",$null,"http://schemas.android.com/tools") | |
$pluralKey = '' | |
$arrayKey = '' | |
Import-Csv $file.FullName | ForEach-Object { | |
if ($_.Type -eq 'plurals' -or $_.Key -contains ".") { | |
$keyParts = $_.Key.Split(".") | |
if (!($pluralKey -eq $keyParts[0])) | |
{ | |
if (!($pluralKey -eq '')) { $xmlWriter.WriteEndElement() } | |
$xmlWriter.WriteStartElement('plurals') | |
$XmlWriter.WriteAttributeString('name', $keyParts[0]) | |
} | |
if (!($arrayKey -eq '')) { | |
$arrayKey = '' | |
$xmlWriter.WriteEndElement() | |
} | |
$pluralKey = $keyParts[0] | |
$xmlWriter.WriteStartElement('item') | |
$XmlWriter.WriteAttributeString('quantity', $keyParts[1]) | |
$XmlWriter.WriteValue($_.Value) | |
$xmlWriter.WriteEndElement() | |
} elseif ($_.Type -eq 'string-array') { | |
$keyParts = $_.Key.Split("/") | |
if (!($arrayKey -eq $keyParts[0])) | |
{ | |
if (!($arrayKey -eq '')) { $xmlWriter.WriteEndElement() } | |
$xmlWriter.WriteStartElement('string-array') | |
$XmlWriter.WriteAttributeString('name', $keyParts[0]) | |
} | |
if (!($pluralKey -eq '')) { | |
$pluralKey = '' | |
$xmlWriter.WriteEndElement() | |
} | |
$arrayKey = $keyParts[0] | |
$xmlWriter.WriteStartElement('item') | |
$XmlWriter.WriteValue($_.Value) | |
$xmlWriter.WriteEndElement() | |
} else { | |
if (!($pluralKey -eq '')) { | |
$pluralKey = '' | |
$xmlWriter.WriteEndElement() | |
} | |
if (!($arrayKey -eq '')) { | |
$arrayKey = '' | |
$xmlWriter.WriteEndElement() | |
} | |
$xmlWriter.WriteStartElement($_.Type) | |
$XmlWriter.WriteAttributeString('name', $_.Key) | |
$XmlWriter.WriteValue($_.Value) | |
$xmlWriter.WriteEndElement() | |
} | |
} | |
if (!($pluralKey -eq '')) { | |
$pluralKey = '' | |
$xmlWriter.WriteEndElement() | |
} | |
if (!($arrayKey -eq '')) { | |
$arrayKey = '' | |
$xmlWriter.WriteEndElement() | |
} | |
$xmlWriter.WriteEndElement() | |
$xmlWriter.WriteEndDocument() | |
$xmlWriter.Flush() | |
$xmlWriter.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment