Next.js Discord

Discord Forum

Database Localization

Answered
Ojos Azules posted this in #help-forum
Open in Discord
Ojos AzulesOP
I have a database that i create my products and fetch them to my store.
But my store has two languages available. How i can store to my database english language products and french language products?
Answered by averydelusionalperson
model ProductTranslations{
  id Int
  language String
  productId Int
  translation String
}

If you want to add multiple lang, ^ this would be a good start ig
View full answer

84 Replies

have two rows named. productName, and frenchProductName?
Ojos AzulesOP
And in front-store if someone selects french lang i will let to map the frenchProductNames?
I believe so.

SudoCode:
{frenchLang ? row.frenchProductName : row.productName}
Ojos AzulesOP
i will try it
Thank you
In case I had 10 languages, it would be very annoying.
hmm
Ojos AzulesOP
In my case its ok but i mind for the future
IDK about you, but I would've stored json
don't know about you, if you would store json in mysql tho
Silver Fox
You can always make a new table with a productId, languageId and a translation
yeah
after all, sql is famous for relations
Ojos AzulesOP
It's sounds good, but i think the way that Kelp suggested is more simple to me for my case
i just add one more field
model ProductTranslations{
  id Int
  language String
  productId Int
  translation String
}

If you want to add multiple lang, ^ this would be a good start ig
Answer
Silver Fox
Yes true, however if you already know in advance that you will use more than two languages in the future, it could be a big pain later 😜
and in the frontend: productTranslations[language].translation
Ojos AzulesOP
model Service {
id String @id @default(uuid())
storeId String
store Store @relation("StoreToService", fields: [storeId], references: [id])
name String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@index([storeId])
}

so what i change here? @averydelusionalperson
just add frenchProductName?
similar to name
I mean that with productTranslations[lang]
oh, connect producttranslations and service.

one (service) -> many (productTranslations)
Ojos AzulesOP
model Service {
  id                  String                @id @default(uuid())
  storeId             String
  store               Store                 @relation("StoreToService", fields: [storeId], references: [id])
  name                String                @unique
  createdAt           DateTime              @default(now())
  updatedAt           DateTime              @updatedAt
  ServiceTranslations ServiceTranslations[]

  @@index([storeId])
}

model ServiceTranslations {
  id          String  @id @default(uuid())
  serviceId   String
  Service     Service @relation(fields: [serviceId], references: [id])
  language    String
  translation String
}
yeah
like that
Ojos AzulesOP
so in language what i store? the name of the language?
@Ojos Azules so in language what i store? the name of the language?
yeah, and that language is the accessor key you would use in frontend. translations[language <- this].translation
you could keep languages constant
or store languages in db too
Ojos AzulesOP
so, products[language].map((product) => ....)

language can i get it from i18n?
I assume so, never used i18n
but yeah, that's the idea
Ojos AzulesOP
hmm
One more thing
in model Service i store the default name of service? e.x the english translation?
hmm, upto you ig, if you want something like default language, when any language isn't provided. then store it
I would say yes
Ojos AzulesOP
if i dont, how i can store two translations in the other model?
wdym by how to store?
Ojos AzulesOP
in model ServiceTranslations i can store only one translationProduct?
no, you can store multiple
Ojos AzulesOP
yes yes
my mind has blown
its ok
lmao
no worries
Ojos AzulesOP
thank you so much
i will try it
Ojos AzulesOP
@averydelusionalperson
In this form i create service. I cant understand how i can add translationProduct here
do i have to add fieldArray in this form? So to add many fields for the translations?
yeah, create a new post for further doubts, but that's the plan
use fieldarray
Ojos AzulesOP
yy thank you!
Ojos AzulesOP
@averydelusionalperson This is one field with language english, so i create second field and i add french language and translated name?
or one field should have and english product and also french product?
i ask you because i get confused
i thought that i understand what i was doing
if you're keeping seperate table for translations
store each translation in seperate row
-id, service Id, language, translation.
-1, 4848, english, Hello.
-2 , 4848, french, Bonjour.
Ojos AzulesOP
This is the model that i use

model Service {
  id                  String                @id @default(uuid())
  storeId             String
  store               Store                 @relation("StoreToService", fields: [storeId], references: [id])
  createdAt           DateTime              @default(now())
  updatedAt           DateTime              @updatedAt
  ServiceTranslations ServiceTranslations[]

  @@index([storeId])
}

model ServiceTranslations {
  id          String   @id @default(uuid())
  serviceId   String?
  Service     Service? @relation(fields: [serviceId], references: [id])
  language    String
  translation String
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}
oh oh ok
so serviceId must be the same
so to be connected each other
yeah
yeah
Ojos AzulesOP
but before i created a servicetranslation but Service model was empty
i wanted to store serviceId with service.id but service its empty
prolly doing something wrong
schema is good
Ojos AzulesOP
yeah the issue is on server actions
i create only for serviceTranslations
and i dont pass them to Service
creating serviceTranslations should be enough
Ojos AzulesOP
yeah
it's connected
Ojos AzulesOP
yes but i have two service here
ugh, imma kinda busy rn (deadline), see you later, till then try to solve on your own
it's working fine imo
Ojos AzulesOP
its ok thank you for your time