02-12-2018 09:09
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-12-2018 09:09
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
I'm building a clock face that also shows a 3 character month and day of week (i.e. Jan and Fri). Right now I have this hardcoded by pulling the current datetime and using a function like this to translate month (and a similar one for day of week).
However - this has a glaring flaw where it would only ever display in English.
Is there a best practice way to read the user's language and return the appropriate string for this without having to hardcode specific strings for every language/month and language/day of week combo? I'd rather make this more dynamic, especially if Fitbit adds more language support in the future.
export function getMonth(i) { switch (i) { case 0: return 'Jan'; case 1: return 'Feb'; case 2: return 'Mar'; case 3: return 'Apr'; case 4: return 'May'; case 5: return 'Jun'; case 6: return 'Jul'; case 7: return 'Aug'; case 8: return 'Sep'; case 9: return 'Oct'; case 10: return 'Nov'; case 11: return 'Dec'; default: return 'Error'; } }
Answered! Go to the Best Answer.
Accepted Solutions
02-12-2018 11:25
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-12-2018 11:25
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
You can do something like this:
*/this can be in utils.js for example */
export var weekday= { de: ["So", "Mo", "Di",(...)],
en: ["Su", "Mo", "Tu", (...)],
*/add more languages here */ };
Import it in index.js and the use it the following way:
import { locale } from "user-settings";
let lang = locale.language; //locale.language returns "en-US" for example
let wday = today.getDay();
let pre = lang.substring(0,2);
util.weekday[pre][wday];
I am not sure if this is best practice, but I think it does the job.
02-12-2018 09:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


02-12-2018 09:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
The JavaScript on the device only supports ES5.1, so it's not capable of doing toLocaleString() to get the localized versions, but you should be able to do that on the companion. Combine that with the locale from user settings:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
https://dev.fitbit.com/build/reference/companion-api/user-settings/
Be aware that the Fitbit locale is currently underscores, so you'd need to convert that to BCP 47. e.g. en_GB to en-GB
02-12-2018 09:41
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-12-2018 09:41
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Can a clock face have a companion, or is that just limited to full apps?

02-12-2018 11:25
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-12-2018 11:25
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
You can do something like this:
*/this can be in utils.js for example */
export var weekday= { de: ["So", "Mo", "Di",(...)],
en: ["Su", "Mo", "Tu", (...)],
*/add more languages here */ };
Import it in index.js and the use it the following way:
import { locale } from "user-settings";
let lang = locale.language; //locale.language returns "en-US" for example
let wday = today.getDay();
let pre = lang.substring(0,2);
util.weekday[pre][wday];
I am not sure if this is best practice, but I think it does the job.
02-12-2018 13:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


02-12-2018 13:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
wrote:
Can a clock face have a companion, or is that just limited to full apps?
Both clocks and apps can have companions, and settings.

02-13-2018 14:29
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-13-2018 14:29
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I like JonFitbit's solution which is more elegant if you're already using a companion, however, what I didn't realize before and noticed today is that you have to explicitly enable your app for specific locales, which means a generic solution isn't really necessary (and therefore that's one less thing to rely on a companion for), so I marked Senort's solution as accepted.

