|
| 1 | +package translator |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +// Tl translates a string based on the given language tag and key. |
| 8 | +func (t *Translator) tl(loc Localizer, key string, args ...interface{}) string { |
| 9 | + translator, exists := t.languages[loc.GetLocale()] |
| 10 | + if !exists { |
| 11 | + return fmt.Sprintf(DefaultNoTranslationTL, key) |
| 12 | + } |
| 13 | + |
| 14 | + if !translator.IsTranslated(key) { |
| 15 | + // check if translation is in the pot file, if not, add it |
| 16 | + if _, ok := t.uniqueKeys[key]; !ok { |
| 17 | + t.uniqueKeys[key] = uniqueKey{singular: key} |
| 18 | + |
| 19 | + err := t.addToPotFileIfNotExists(translationKey{"", key, false}) |
| 20 | + if err != nil { |
| 21 | + fmt.Println(err) |
| 22 | + } |
| 23 | + } |
| 24 | + return fmt.Sprintf(DefaultNoTranslationTL, key) |
| 25 | + } |
| 26 | + |
| 27 | + translated := translator.Get(key, args...) |
| 28 | + return t.removePrefix(translated) |
| 29 | +} |
| 30 | + |
| 31 | +func (t *Translator) ctl(loc Localizer, ctx, key string, args ...interface{}) string { |
| 32 | + translator, exists := t.languages[loc.GetLocale()] |
| 33 | + if !exists { |
| 34 | + return fmt.Sprintf(DefaultNoTranslationCTL, ctx, key) |
| 35 | + } |
| 36 | + |
| 37 | + if !translator.IsTranslatedC(key, ctx) { |
| 38 | + if t.uniqueKeysCtx[ctx] == nil { |
| 39 | + t.uniqueKeysCtx[ctx] = make(map[string]uniqueKey) |
| 40 | + } |
| 41 | + |
| 42 | + if _, ok := t.uniqueKeysCtx[ctx][key]; !ok { |
| 43 | + t.uniqueKeysCtx[ctx][key] = uniqueKey{singular: key} |
| 44 | + |
| 45 | + err := t.addToPotFileIfNotExists(translationKey{ctx, key, false}) |
| 46 | + if err != nil { |
| 47 | + fmt.Println(err) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return fmt.Sprintf(DefaultNoTranslationCTL, ctx, key) |
| 52 | + } |
| 53 | + |
| 54 | + translated := translator.GetC(key, ctx, args...) |
| 55 | + return t.removePrefix(translated) |
| 56 | +} |
| 57 | + |
| 58 | +// tn method for handling plurals |
| 59 | +func (t *Translator) tn(loc Localizer, singular, plural string, n int, args ...any) string { |
| 60 | + translator, exists := t.languages[loc.GetLocale()] |
| 61 | + if !exists { |
| 62 | + return fmt.Sprintf(DefaultNoTranslationTN, singular, plural) |
| 63 | + } |
| 64 | + |
| 65 | + if !translator.IsTranslatedN(singular, n) { |
| 66 | + // check if translation is in the pot file, if not, add it |
| 67 | + if _, ok := t.uniqueKeys[singular]; !ok { |
| 68 | + t.uniqueKeys[singular] = uniqueKey{singular: singular, plural: plural} |
| 69 | + |
| 70 | + err := t.addToPotFileIfNotExists(translationKey{"", singular, true}) |
| 71 | + if err != nil { |
| 72 | + fmt.Println(err) |
| 73 | + } |
| 74 | + } |
| 75 | + return fmt.Sprintf(DefaultNoTranslationTN, singular, plural) |
| 76 | + } |
| 77 | + |
| 78 | + translated := translator.GetN(singular, plural, n, args...) |
| 79 | + return t.removePrefix(translated) |
| 80 | +} |
| 81 | + |
| 82 | +func (t *Translator) ctn(loc Localizer, ctx, singular, plural string, n int, args ...any) string { |
| 83 | + translator, exists := t.languages[loc.GetLocale()] |
| 84 | + if !exists { |
| 85 | + return fmt.Sprintf(DefaultNoTranslationCTN, ctx, singular, plural) |
| 86 | + } |
| 87 | + |
| 88 | + if !translator.IsTranslatedNC(singular, n, ctx) { |
| 89 | + if t.uniqueKeysCtx[ctx] == nil { |
| 90 | + t.uniqueKeysCtx[ctx] = make(map[string]uniqueKey) |
| 91 | + } |
| 92 | + |
| 93 | + if _, ok := t.uniqueKeysCtx[ctx][singular]; !ok { |
| 94 | + t.uniqueKeysCtx[ctx][singular] = uniqueKey{singular: singular, plural: plural} |
| 95 | + |
| 96 | + err := t.addToPotFileIfNotExists(translationKey{ctx, singular, true}) |
| 97 | + if err != nil { |
| 98 | + fmt.Println(err) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return fmt.Sprintf(DefaultNoTranslationCTN, ctx, singular, plural) |
| 103 | + } |
| 104 | + |
| 105 | + translated := translator.GetNC(singular, plural, n, ctx, args...) |
| 106 | + return t.removePrefix(translated) |
| 107 | +} |
| 108 | + |
| 109 | +// Tl translates a string based on the given language tag and key. |
| 110 | +func (t *Translator) Tl(loc Localizer, key string, args ...interface{}) string { |
| 111 | + return t.tl(loc, key, args...) |
| 112 | +} |
| 113 | + |
| 114 | +// Tn method for handling plurals |
| 115 | +func (t *Translator) Tn(loc Localizer, singular, plural string, n int) string { |
| 116 | + return t.tn(loc, singular, plural, n) |
| 117 | +} |
| 118 | + |
| 119 | +// Ctl method for handling string translation with context |
| 120 | +func (t *Translator) Ctl(loc Localizer, ctx, key string, args ...interface{}) string { |
| 121 | + return t.ctl(loc, ctx, key, args...) |
| 122 | +} |
| 123 | + |
| 124 | +// Ctn method for handling plurals with context |
| 125 | +func (t *Translator) Ctn(loc Localizer, ctx, singular, plural string, n int) string { |
| 126 | + return t.ctn(loc, ctx, singular, plural, n) |
| 127 | +} |
| 128 | + |
| 129 | +// Details return the header of the language file |
| 130 | +func (t *Translator) Details(loc Localizer) map[string][]string { |
| 131 | + return t.languages[loc.GetLocale()].Headers |
| 132 | +} |
0 commit comments