Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to access the localized string programmatically #22

Closed
ricsantos opened this issue Aug 17, 2022 · 4 comments
Closed

Is it possible to access the localized string programmatically #22

ricsantos opened this issue Aug 17, 2022 · 4 comments

Comments

@ricsantos
Copy link

Firstly, thanks @kishikawakatsumi for the awesome resource, its very well done.

I'm wondering, is there a way to access the strings at runtime?

For example:
ACTIVE_ENERGY_TITLE_EMBEDDED
"Active Energy"
HealthKit.framework
Localizable-DataTypes.strings

I am trying to get it like so, but it's not working, either because I'm missing something

let bundle = Bundle(for: HKWorkout.self)
let string = NSLocalizedString("ACTIVE_ENERGY_TITLE_EMBEDDED", tableName: nil, bundle: bundle, value: "", comment: "")
@kishikawakatsumi
Copy link
Owner

@ricsantos

ACTIVE_ENERGY_TITLE_EMBEDDED key is defined in Localizable-DataTypes.strings

Screen Shot 2022-08-18 at 2 39 45

So you need to specify tableNeme "Localizable-DataTypes".

let bundle = Bundle(for: HKWorkout.self)
let string = NSLocalizedString("ACTIVE_ENERGY_TITLE_EMBEDDED", tableName: "Localizable-DataTypes", bundle: bundle, value: "", comment: "")

The above code should return "Active Energy" string.
I only check on a simulator, but on a device should work as same.

@ricsantos
Copy link
Author

That makes perfect sense! Thank you very much @kishikawakatsumi, I appreciate your help.

I actually have a follow up question, I am also trying to get the strings for the HKWorkoutActivityType enum, eg "Basketball"

I notice that HKWorkoutActivityTypeBasketball it's in FitnessUI.framework which is private, so I guess I can't use that.

I notice also that it's in HealthUI.framework but I can't see that, only HealthKitUI.framework, maybe it's also private?

@kishikawakatsumi
Copy link
Owner

kishikawakatsumi commented Aug 17, 2022

Yes, HealthUI.framework is a private framework. If you'd like to access the bundle, you can use Bundle(path:) initializer. Like the following:

let bundle = Bundle(path: "/System/Library/PrivateFrameworks/HealthUI.framework")!
let string = NSLocalizedString("HKWorkoutActivityTypeBasketball", tableName: "HealthUI-Localizable", bundle: bundle, value: "", comment: "")

^ this should show "Baseball" string. It should work only on a device. Because a simulator runs on host's file system.

@ricsantos
Copy link
Author

Oh yeah it works, you're the best. Thanks again!

Repository owner locked and limited conversation to collaborators Aug 19, 2022
@kishikawakatsumi kishikawakatsumi converted this issue into discussion #23 Aug 19, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants