Closed scarrick68 closed 9 months ago
HI @scarrick68, if that helps, we did it by passing the format like this:
local_time(@date_time, :time_with_tz)
and specifying the format in our config/locales/en.yml
file as such:
en:
time:
formats:
time_with_tz: "%H:%Mh %Z"
Ah I see what happened. Sorry this is confusing, but rails helpers (like #local_time
) will call a different default format than the one specified in LocalTime.config.i18n
.
See line 55 here: https://github.com/basecamp/local_time/blob/7b60d1d59ba0717a2e62b7b94fe4500ef1d81274/app/helpers/local_time_helper.rb#L45-L57
Changing this is not trivial. Perhaps the action item here is adding a note to the readme saying that default values shouldn't be changed as it won't behave as one might expect. I'll look into it.
Actually, I'm going to reopen so we remember to document this somehow
Done in https://github.com/basecamp/local_time/commit/629419427bb7db508829c28cf4a16e048603d616
For the time being, I'd only recommend using the i18n configuration after understanding which helpers use our getI18nValue
JS helper behind the scenes. Not specifying a format will not result in grabbing the i18n config's default format — that's different to the mattr accessors defined on the Ruby side here:
https://github.com/basecamp/local_time/blob/7013aa954882fa7e6db306ec97bea4365b17b0be/lib/local_time.rb#L2-L3
"default" is admittedly a confusing choice of name here. Might revisit later.
@josefarias jose, i really confuse of this config
if we leave it(just pure copy-paste), the translation works, but if we change neither in rails views or the js it will revert to english again even we set the i18n in Indonesian
<%= local_time(submitted_at, "%a, %d %b %Y ~ %H:%M %Z") %>
LocalTime.config.i18n["id"] = {
date: {
dayNames: ["Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"],
abbrdayNames: ["Min", "Sen", "Sel", "Rab", "Kam", "Jum", "Sab"],
monthNames: ["Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "Nopember", "Desember"],
abbrmonthNames: ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Agu", "Sep", "Okt", "Nop", "Des"],
yesterday: "kemarin",
today: "hari ini",
tomorrow: "besok",
on: "pada {date}",
formats: {
default: "%b %e, %Y",
thisYear: "%b %e",
}
},
time: {
am: "",
pm: "",
singular: "{time}",
singularAn: "{time}",
elapsed: "{time} lalu",
second: "detik",
seconds: "detik",
minute: "menit",
minutes: "menit",
hour: "hari",
hours: "hari",
formats: {
default: "%l:%M%P",
default_24h: "%H:%M %Z"
}
},
datetime: {
at: "{date} pada {time}",
formats: {
default: "%B %e, %Y pada %l:%M%P %Z",
default_24h: "%B %e, %Y pada %H:%M %Z"
}
}
}
LocalTime.config.locale = "id"
LocalTime.config.useFormat24 = true
@kwhandy sorry you're finding this confusing!
If I understand correctly, the example you shared is working correctly. That makes sense, it looks correct to me!
But if you change it, then it stops working. Can I see an example where it stopped working? What did you change exactly?
@josefarias hi jose, some quick update, this finally works
turns out, it's about library import ordering issue
this is what it should:
import "@hotwired/turbo-rails"
import LocalTime from "local-time"
import * as ActiveStorage from "@rails/activestorage"
import morphdom from "morphdom"
import "./controllers"
and this what i did:
import "@hotwired/turbo-rails"
import * as ActiveStorage from "@rails/activestorage" // this dude should below local-time when we import it
import LocalTime from "local-time"
import morphdom from "morphdom"
import "./controllers"
for some reason, activestorage block local-time to translate the object which make me confused for days to figure out WHY, even think switch to luxon lol but glad it wouldn't happen in near future(at least) :D
I am trying to set a default DateTime format for my app, so I did this in a JS file and included it into my application.js
I printed in the console and see the updated default format, but all of my DateTimes still show in the original default format. I am only interested in DateTime. I set the default on all of them just in case I was passing an unexpected type.
I called local_time in the view like this with :my_format being added to the formats instead of overriding default: