mac-cain13 / R.swift

Strong typed, autocompleted resources like images, fonts and segues in Swift projects
MIT License
9.5k stars 763 forks source link

Keep dots in localized string keys for readability/preservation of hierarchy #402

Open mac-cain13 opened 6 years ago

mac-cain13 commented 6 years ago

Extracted from #41.

@fabb suggests:

Is there a reason why R.swift contrary to Laurine did not use nested structs for accessing parts of localized strings which are separated by a .? E.g. Laurine allows to write things like Localizations.Profile.NavigationBar.Items.Done which looks very nice.

[@mac-cain13 replies:]()

No there is no specific reason for this other then that it's in line with all other name conversions we have in the codebase. I agree it would be nice to have this behaviour, we also use this dot-pattern a lot in our own strings files and the conversion R.swift applies now makes it less convenient to read.

I think it would be a change to consider. I'll create a separate issue for this improvement so we can discuss/implement it!

tomlokhorst commented 6 years ago

Note that 1 level of "dots" is already possible by simply placing strings in separate .strings files, e.g.: R.strings.timeline.title & R.strings.settings.title.

But I agree, now that we have hierarchy in images via Folders in .xcassets files, it would be nice to also add this hierarchy capability to strings.

DevTchernov commented 5 years ago

It's done and tested with current strings, can't append more test cause impossible to check command options in ResourceApp's XCTests. Maybe parse options will be append in root R.generated structure for checking its in runtime https://github.com/mac-cain13/R.swift/pull/467

fabb commented 5 years ago

Was this released in 5.0.0? It‘s not in the release notes, and still open, but has the milestone set.

tomlokhorst commented 5 years ago

This is not part of the 5.0 release. I've removed the milestone.

Brudus commented 3 years ago

Is there any update on this? I think it would be a great addition to R.Swift. With the usage of typealiases, this would be really convenient.

For example, I have this Localizable.strings:

// SettingViewController
"SettingsViewController.Title" = "Settings";

// SignInViewController
"SignInViewController.Title" = "Sign In";

What I currently have in my code is the following:

final class SettingsViewController: UIViewController {
    private typealias Localization = R.string.localizable

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = Localization.settingsViewControllerTitle()
    }

    ...
}
final class SignInViewController: UIViewController {
    private typealias Localization = R.string.localizable

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = Localization.signInViewControllerTitle()
    }
}

Now I get all the strings suggested when I type 'Localization.' but I only want those that are relevant for my current context. Instead with that solution in place, I could do the following:

final class SettingsViewController: UIViewController {
    private typealias Localization = R.string.localizable.signInViewController

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = Localization.title()
    }

    ...
}

This makes it easier to read and reason about. I know in which context I am. I don't need to read signInViewController or settingsViewController over and over again throughout my file. The code completion becomes more useful with that, too.

tamassengel commented 1 year ago

+1, this would be really useful. For now, use SwiftGen if you want this feature.

wvteijlingen-npo commented 3 weeks ago

Is there still a plan to implement this? I would be great if R.swift implemented this naming strategy!