Skip to content

Commit

Permalink
String Catalog (.xcstrings) support
Browse files Browse the repository at this point in the history
* Total refactor of the XLIFF parser logic.
* More readable and robust code that can handle out-of-order translation
units and special cases.
* XLIFF parser can now also parse `.xcstrings` files on top of the
existing `.strings` and `.stringsdict`.
* Only simple "plural." rules of `.xcstrings` files are parsed. The rest
of the rule types (device variations, substitutions) produce and log an
error and are not pushed to CDS.
* Improves XLIFF parsing logic.
* Adds unit test for simple `.xcstrings` plurals.
* Documents limitations.
  • Loading branch information
stelabouras committed Apr 1, 2024
1 parent 59dc221 commit a73f4ca
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 219 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ command. For example, for iOS applications the option can be set to `--base-sdk

##### Pushing pluralizations limitations

Currently (version 0.1.0) pluralization is supported but only for cases where one variable is
used per pluralization rule. More advanced cases such as nested pluralization rules (for
example: "%d out of %d values entered") will be supported in future releases.
Generally, pluralization is supported but only for cases where one variable is used per pluralization rule.

Also, at the moment of writing (version 0.1.0), the `.stringsdict` specification only supports
plural types (`NSStringPluralRuleType`) which is the only possible value of the
`NSStringFormatSpecTypeKey` key ([Ref](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPInternational/StringsdictFileFormat/StringsdictFileFormat.html#//apple_ref/doc/uid/10000171i-CH16-SW4)).
Both the existing `.stringsdict` and the newly introduced string catalog (`.xcstrings`) files are supported with some limitations mentioned below.

If more rule types are added in the `.stringsdict` specification, the XLIFF parser must be
updated in order to be able to extract them properly and to construct the ICU format out
of them.
We are actively working on adding support for more variations in future releases.

Width Variants in `.stringsdict` files are also not currently supported ([Ref](https://help.apple.com/xcode/mac/current/#/devaf8b4090a)).
###### String Catalogs (`.xcstrings`)

Only plural rules are supported for string catalogs. Device variation [^1] and substitution rules are not currently supported.

###### Strings Dictionary Files (`.stringsdict`)

Only the plural type is supported (`NSStringPluralRuleType`) which is the only possible value of the `NSStringFormatSpecTypeKey` key [^2]. Width Variants are not currently supported [^3] [^4].

#### Pulling

Expand Down Expand Up @@ -186,3 +186,8 @@ command can be simplified to:
## License

Licensed under Apache License 2.0, see [LICENSE](LICENSE) file.

[^1]: https://developer.apple.com/documentation/xcode/localizing-and-varying-text-with-a-string-catalog#Vary-strings-by-device
[^2]: https://developer.apple.com/documentation/xcode/localizing-strings-that-contain-plurals
[^3]: https://help.apple.com/xcode/mac/current/#/devaf8b4090a
[^4]: https://developer.apple.com/documentation/xcode/creating-width-and-device-variants-of-strings
17 changes: 15 additions & 2 deletions Sources/TXCli/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,21 @@ Emulate a content push, without doing actual changes.

// If the result contains string dict elements, convert them to
// ICU format and use that as a source string
if let icuRule = result.generateICURuleIfPossible() {
sourceString = icuRule
switch result.generateICURuleIfPossible() {
case .success((let icuRule, let icuRuleType)):
// Only support plural rule type for now
if icuRuleType == .Plural {
sourceString = icuRule
}
case .failure(let error):
switch error {
case .noRules:
break
default:
logHandler.error("Error: \(error)")
// Do not add a translation unit in case of an error.
continue
}
}

let translationUnit = TXSourceString(key: key,
Expand Down
Loading

0 comments on commit a73f4ca

Please sign in to comment.