在没有 psd1 文件的情况下翻译 PS 脚本

Translating PS script without psd1 file

我是一名休闲代码编写者。我在自分配线程中创建一个脚本来安装一个仅作为 Windows 的 exe 文件可用的软件,并自定义一些存储在 xml 文件中的默认值。这对我来说是一个step/step的过程,每一个都是在网上大量阅读和多次尝试。作为 ATM 估计旅行的 5%,我想到了一个(其他 :-( ) 想法:除了法语之外,还为讲英语的人做这份工作,并发布脚本,以便任何其他人可以轻松地添加他们自己的语言字符串消息。我发现了使用 DATA 部分、psd1 文件和一些相关命令的 MS 脚本国际化方式。

我的目标是提供一个 ps1 文件,dot.根据我最近阅读的内容,我的最新想法是将所有字符串都放在 ps1 文件中,然后基于 $PSUIlocale export/create 一个临时的 .\LG-lg\SetupThisSoft.psd1 我可以 Import-LocalizedData...我猜是愚蠢的(当我们在文件中有字符串时为什么要导出?)。

你有什么实现目标的想法吗?我现在有了一些(更好的?)想法,使用一系列尚未翻译的语言环境,例如$AvailLng = "fr-FR","en-EN" 然后每个 $Item 都有一个数据部分我可以 ForEach 测试 $PSUILocale ,但我不知道如何“指向”/“启用”好的数据部分。当 $PSUILocale 与任何先前的 $Item 不匹配时,“en-EN”将作为 default/fallback 的最后一个 $Item...

谢谢你的想法

将您的 Data 部分存储在哈希表中,并使用区域设置标识符(fr-FRen-US 等)作为键:

# Store messages for multiple languages in $localizedMessages
$localizedMessages = @{
    'en-US' = Data {
        ConvertFrom-StringData @'
        Error = an error occurred
        Success = something wonderful happened
'@
    }
    'fr-FR' = Data {
        ConvertFrom-StringData @'
        Error = une erreur est survenue
        Success = quelque chose de merveilleux est arrivé
'@
    }
}

$defaultLanguage = 'fr-FR'

# Assign the appropriate language version to the variable that holds the messages for later use
$Messages = if($localizedMessages.ContainsKey($PSUICulture)){
    $localizedMessages[$PSUICulture]
} else {
    # If we don't have localized messages for the current culture, we fall back to our default language
    $localizedMessages[$defaultLanguage]
}

# ...

# This will now throw a different error message based on the current UI culture
Write-Error $Messages['Error']