R.swift icon indicating copy to clipboard operation
R.swift copied to clipboard

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

Open mac-cain13 opened this issue 6 years ago • 6 comments

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!

mac-cain13 avatar Apr 09 '18 06:04 mac-cain13

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.

tomlokhorst avatar Apr 09 '18 09:04 tomlokhorst

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

DevTchernov avatar Dec 05 '18 10:12 DevTchernov

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

fabb avatar Dec 17 '18 15:12 fabb

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

tomlokhorst avatar Dec 18 '18 07:12 tomlokhorst

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.

Brudus avatar Jan 08 '21 16:01 Brudus

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

tamassengel avatar Oct 09 '23 14:10 tamassengel