From 6cdb6ec711794c2887da03c0e5d3cfac67bded3f Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 28 Apr 2019 14:50:17 -0500 Subject: [PATCH] Add support for plurlization to locales --- locales/ar.json | 2 +- locales/en-US.json | 75 ++++++++++++++++++++++++++++------- src/invidious/helpers/i18n.cr | 20 +++++++++- 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/locales/ar.json b/locales/ar.json index 0be55904..4b9b2711 100644 --- a/locales/ar.json +++ b/locales/ar.json @@ -307,4 +307,4 @@ "Videos": "الفيديوهات", "Playlists": "قوائم التشغيل", "Current version: ": "الإصدار الحالى" -} +} \ No newline at end of file diff --git a/locales/en-US.json b/locales/en-US.json index 8dbc7e61..21c2d515 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -1,6 +1,12 @@ { - "`x` subscribers": "`x` subscribers", - "`x` videos": "`x` videos", + "`x` subscribers": { + "(\\D|^)1(\\D|$)": "`x` subscriber", + "": "`x` subscribers" + }, + "`x` videos": { + "(\\D|^)1(\\D|$)": "`x` video", + "": "`x` videos" + }, "LIVE": "LIVE", "Shared `x` ago": "Shared `x` ago", "Unsubscribe": "Unsubscribe", @@ -102,13 +108,22 @@ "Subscription manager": "Subscription manager", "Token manager": "Token manager", "Token": "Token", - "`x` subscriptions": "`x` subscriptions", - "`x` tokens": "`x` tokens", + "`x` subscriptions": { + "(\\D|^)1(\\D|$)": "`x` subscription", + "": "`x` subscriptions" + }, + "`x` tokens": { + "(\\D|^)1(\\D|$)": "`x` token", + "": "`x` tokens" + }, "Import/export": "Import/export", "unsubscribe": "unsubscribe", "revoke": "revoke", "Subscriptions": "Subscriptions", - "`x` unseen notifications": "`x` unseen notifications", + "`x` unseen notifications": { + "(\\D|^)1(\\D|$)": "`x` unseen notification", + "": "`x` unseen notifications" + }, "search": "search", "Log out": "Log out", "Released under the AGPLv3 by Omar Roth.": "Released under the AGPLv3 by Omar Roth.", @@ -126,7 +141,10 @@ "Whitelisted regions: ": "Whitelisted regions: ", "Blacklisted regions: ": "Blacklisted regions: ", "Shared `x`": "Shared `x`", - "`x` views": "`x` views", + "`x` views": { + "(\\D|^)1(\\D|$)": "`x` views", + "": "`x` views" + }, "Premieres in `x`": "Premieres in `x`", "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.": "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.", "View YouTube comments": "View YouTube comments", @@ -156,10 +174,16 @@ "This channel does not exist.": "This channel does not exist.", "Could not get channel info.": "Could not get channel info.", "Could not fetch comments": "Could not fetch comments", - "View `x` replies": "View `x` replies", + "View `x` replies": { + "(\\D|^)1(\\D|$)": "View `x` reply", + "": "View `x` replies" + }, "`x` ago": "`x` ago", "Load more": "Load more", - "`x` points": "`x` points", + "`x` points": { + "(\\D|^)1(\\D|$)": "`x` point", + "": "`x` points" + }, "Could not create mix.": "Could not create mix.", "Empty playlist": "Empty playlist", "Not a playlist.": "Not a playlist.", @@ -277,13 +301,34 @@ "Yiddish": "Yiddish", "Yoruba": "Yoruba", "Zulu": "Zulu", - "`x` years": "`x` years", - "`x` months": "`x` months", - "`x` weeks": "`x` weeks", - "`x` days": "`x` days", - "`x` hours": "`x` hours", - "`x` minutes": "`x` minutes", - "`x` seconds": "`x` seconds", + "`x` years": { + "(\\D|^)1(\\D|$)": "`x` year", + "": "`x` years" + }, + "`x` months": { + "(\\D|^)1(\\D|$)": "`x` month", + "": "`x` months" + }, + "`x` weeks": { + "(\\D|^)1(\\D|$)": "`x` week", + "": "`x` weeks" + }, + "`x` days": { + "(\\D|^)1(\\D|$)": "`x` day", + "": "`x` days" + }, + "`x` hours": { + "(\\D|^)1(\\D|$)": "`x` hour", + "": "`x` hours" + }, + "`x` minutes": { + "(\\D|^)1(\\D|$)": "`x` minute", + "": "`x` minutes" + }, + "`x` seconds": { + "(\\D|^)1(\\D|$)": "`x` second", + "": "`x` seconds" + }, "Fallback comments: ": "Fallback comments: ", "Popular": "Popular", "Top": "Top", diff --git a/src/invidious/helpers/i18n.cr b/src/invidious/helpers/i18n.cr index 5acdcfc5..c3d3fbf4 100644 --- a/src/invidious/helpers/i18n.cr +++ b/src/invidious/helpers/i18n.cr @@ -7,8 +7,24 @@ def translate(locale : Hash(String, JSON::Any) | Nil, translation : String, text # puts "Could not find translation for #{translation.dump}" # end - if locale && locale[translation]? && !locale[translation].as_s.empty? - translation = locale[translation].as_s + if locale && locale[translation]? + case locale[translation] + when .as_h? + match_length = 0 + + locale[translation].as_h.each do |key, value| + if md = text.try &.match(/#{key}/) + if md[0].size >= match_length + translation = value.as_s + match_length = md[0].size + end + end + end + when .as_s? + if !locale[translation].as_s.empty? + translation = locale[translation].as_s + end + end end if text