Hi, what is the possible ways to localize enum ?
# announcements
s
Hi, what is the possible ways to localize enum ?
stackoverflow 3
c
What about it are you trying to Localize?
s
the application is English and I want to get the enum values in another language but I don't know how to do that
g
Enum names shouldn’t be localized and shouldn’t be visible for user. If you want to have some UI title for enum use special String field with this title instead of enum name itself
4
s
is it possible to have the enum values support multi language
g
What are enum values? some enum property?
e
Even if it is possible, enum are not supposed to do this
g
Do you already use any localization solution in your application (not for enums)?
s
yes I did
g
And is it based on what? Strings?
Just add string property to enum and translate this property
something like:
Copy code
enum class Location(val title: String) {
  HOME("Home"),
  WORK("Work")
}

MyLocalizationTool.localize(Location.WORK.title)
s
(MyLocalizationTool.localize ) this refers to what or just an example??!
e
Example, you should replace that with your own localization helper
g
There is a bunch of approaches for localizations, I cannot recommend anything particular, just use anything what you have, but do not use Enum.name for that
g
Ohh, so it’s Android. Just use string resource id for Enum property instead of String
👍 1
On Android:
Copy code
enum class Location(@StringRes val title: Int) {
  HOME(R.string.home),
  WORK(R.string.work)
}

textView.setText(Location.HOME.title)
s
it's not working the (R & @StringRes) turns into red
e
@StringRes
should require a import,
R
need a gradle sync or rebuild
g
Also @StringRes required support annotations (or support-v4, appcompat etc) in dependencies, but you can omit it, it’s not required
d
Looks like you can get Android Development classes here 😄
🤔 1
193 Views