Skip to content

Instantly share code, notes, and snippets.

@Gernot
Created March 12, 2019 19:15
Show Gist options
  • Save Gernot/3ad5396053fc26b29b9cac4ae45f59ef to your computer and use it in GitHub Desktop.
Save Gernot/3ad5396053fc26b29b9cac4ae45f59ef to your computer and use it in GitHub Desktop.
A proposal to have much nicer, backwards compatible localizable strings in Swift
/* Here is what I want to write, and what Xcode sees. */
struct Loc {
struct someDomain {
/// Here is some description of what this string is doing. It's readable in the AutoComplete Description!
static let myStringName = "Woah, woah, this is the actual content of the String!"
}
}
/* And now some Parser/Code Generator comes in and generates this out of it*/
/* This is what the localization tools and the compiler see, when the App actually gets compiled */
struct Loc {
struct someDomain {
static let myStringName = NSLocalizedString(
"SomeDomain.MyStringName",
value:"Woah, woah, this is the actual content of the String!",
comment: "Here is some description of what this string is doing. It's readable in the AutoComplete Description!")
}
}
/* Is this possible? Does this exist? Does anybody what to build this with me? */
@rist
Copy link

rist commented Mar 12, 2019

how would you do the actual localization? Where would you put "Oh, pardauz, das ist der Inhalt der Zeichenkette!""
We use https://github.com/mac-cain13/R.swift/blob/master/Documentation/Examples.md#localized-strings for having compile time safety when using strings and other assets. Not sure if R.swift would parse comments in the strings file and adds them as comment in the generated code - can check that tomorrow at work

@Gernot
Copy link
Author

Gernot commented Mar 13, 2019

That would be no different from how it works now. The localization tools se the NSLocalizedStrings, do the import/export dance, etc.

I don't know why you mean with "compile time safety", but this approach guarantees that there is always a String behind the symbol, and in the worst case, if the translation isn't available, the string is the original content in the base language, and never ever the localization key.

@rist
Copy link

rist commented Mar 13, 2019

as we create the R files at each build we can be sure that there is a string behind R.string.localizable.myStringName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment