Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Best practice on date multilingual support

ANSWERED

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';
    }
}
Best Answer
1 BEST ANSWER

Accepted Solutions

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. 

 

View best answer in original post

Best Answer
5 REPLIES 5

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

 

Best Answer

Can a clock face have a companion, or is that just limited to full apps?

Best Answer
0 Votes

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. 

 

Best Answer

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.

Best Answer
0 Votes

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.

Best Answer
0 Votes