Stringly

Stringly 从源 yamljsontoml 文件生成类型安全的本地化文件。目前仅支持 Apple 平台的输出,但添加 Android R.strings 的生成器很容易。

用法

查看帮助

stringly help

生成所有语言的所有文件

stringly generate Strings.yml

生成特定语言的单个文件

stringly generate-file Strings.yml Strings.strings --language de

安装

请先确保已安装 Xcode 13。

Mint

mint install yonaskolb/stringly

Swift Package Manager

用作 CLI

git clone https://github.com/yonaskolb/Stringly.git
cd Stringly
swift run stringly

用作依赖库

将以下内容添加到您的 Package.swift 文件的 dependencies 中

.package(url: "https://github.com/yonaskolb/Stringly.git", from: "0.7.0"),

然后在需要的地方导入:import StringlyKit

示例

给定一个源文件 Strings.yml

auth: # grouping of strings
  loginButton: Log In # If you don't specify a language it defaults to a base language
  emailTitle:
    en: Email # specifying a language
  passwordTitle: 
    en: Password
    de: Passwort # multiple languages
  error: # infinitely nested groups
    wrongEmailPassword: Incorrect email/password combination
home:
  title: Hello {name} # this is a placeholder. Without a type defaults to %@ on apple platforms
  postCount: "Total posts: {postCount:d}" # the placeholder now has a type %d
  day: "Day: {}" # an unnamed placeholder
  escaped: Text with escaped \{braces} # escape braces in text by using \{
  articles: # this is a pluralized string
    en: You have {articleCount:d} # placeholder will be replaced with pluralization
    en.articleCount: # supports pluralizing multiple placeholders in a single string
      none: no articles
      one: one article
      other: {articleCount:d} articles

这将为多种语言生成 .swift.strings.stringsdict 文件。

然后 Swift 文件允许像这样使用

errorLabel.text = Strings.auth.error.wrongEmailPassword
welcomeLabel.text = Strings.home.title(name: "John")
postsLabel.text = Strings.home.postCount(postCount: 10)
day.text = Strings.home.day("Monday")
articleLabel.text = Strings.home.articles(articleCount: 4)

未来方向