Swiftgen: how to generate strings from multiple .strings file

Created on 18 Sep 2018  Â·  3Comments  Â·  Source: SwiftGen/SwiftGen

how to generate all the strings in multiple Strings file .
i had many *.strings file
like
$(SRROOT)/Resources/Localize/Localizable.strings
$(SRROOT)/Resources/Localize/Auth.strings
$(SRROOT)/Resources/Localize/News.strings
is there a way to give a Path not a file in the .swiftgen.yml instead of file path
YML strings: paths: Resources/Locale/Base.lproj/Localizable.strings templateName: structured-swift4 output: Resources/Locale/Localized.swift fallback: Base

and a lot of .string file may be added later , so it would be great to not explicate add each file

question

All 3 comments

The key paths in your configuration file can be an array of files instead of a file. So you can do this at least:

input_dir: Resources/Locale/Base.lproj/
strings:
  paths:
    - Localizable.strings
    - Auth.strings
    - News.strings
  templateName: structured-swift4 
  output: Resources/Locale/Localized.swift 

We have an issue open (https://github.com/SwiftGen/SwiftGen/issues/355) to allow glob patterns in the future though, so you'd be able both to use env vars ($SRCROOT) and to use Resources/Localize/*.strings for example. That hasn't been implemented yet, and isn't planned for the soon-to-be-released 6.0, but any PR to implement that #355 would be welcome anyway :)

Also, regarding accepting a path to a folder instead of a single .strings file, that would totally be possible and probably very easy to implement. Mainly it'd consist to amend this parse(path: Path) function in the StringsParser.swift to add a check if the path.isDirectory, and if so iterate over all items in it, recursively calling parse(path: Path) on the children.

The change itself would probably be quite small in the code — just don't forget to add the corresponding unit tests if you do a PR for that though.


I think the original reason why we didn't accept a folder but only a file was that if we did accept folders, people had the tendency to pass the Resources/ folder to it, not understanding that you should only pass one of the .lproj folder and not all the languages folders (because if you pass both Base.lproj and fr.lproj for example, it will find the same keys in both strings files and generate duplicate constants), so forcing to pass a file now makes sure people are not tempted to pass every lproj ever.

So for that reason, and also because if people pass a Resources folder containing multiple .lproj subfolders, that would lead to bad generated code, maybe if you add support to accept a directory for the strings inputs, it would be a good idea to only accept directories ending in .lproj only then, as a security? Or generate a warning if that's not the case at least, if not an error?

Btw just saw that we already have an issue to track this feature we want to add in the future: https://github.com/SwiftGen/SwiftGen/issues/32.

Closing this one to avoid duplicates, but don't hesitate to comment/reply on #32 to continue the conversation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kacper1703 picture kacper1703  Â·  3Comments

SummerHF picture SummerHF  Â·  3Comments

KonstantinBerkow picture KonstantinBerkow  Â·  5Comments

drjasonharrison picture drjasonharrison  Â·  5Comments

romk1n picture romk1n  Â·  7Comments