mistral_translator 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,182 @@
1
+ {
2
+ "Adlm" : {
3
+ "en" : "Adlam",
4
+ "fr" : "adlam"
5
+ },
6
+ "Arab" : {
7
+ "en" : "Arabic",
8
+ "fr" : "arabe"
9
+ },
10
+ "Aran" : {
11
+ "en" : "Nastaliq",
12
+ "fr" : "nastaliq"
13
+ },
14
+ "Armn" : {
15
+ "en" : "Armenian",
16
+ "fr" : "arménien"
17
+ },
18
+ "Beng" : {
19
+ "en" : "Bangla",
20
+ "fr" : "bengali"
21
+ },
22
+ "Cakm" : {
23
+ "en" : "Chakma",
24
+ "fr" : "chakma"
25
+ },
26
+ "Cans" : {
27
+ "en" : "Syllabics",
28
+ "fr" : "syllabaire"
29
+ },
30
+ "Cher" : {
31
+ "en" : "Cherokee",
32
+ "fr" : "cherokee"
33
+ },
34
+ "Cyrl" : {
35
+ "en" : "Cyrillic",
36
+ "fr" : "cyrillique"
37
+ },
38
+ "Deva" : {
39
+ "en" : "Devanagari",
40
+ "fr" : "dévanagari"
41
+ },
42
+ "Ethi" : {
43
+ "en" : "Ethiopic",
44
+ "fr" : "éthiopique"
45
+ },
46
+ "Geor" : {
47
+ "en" : "Georgian",
48
+ "fr" : "géorgien"
49
+ },
50
+ "Grek" : {
51
+ "en" : "Greek",
52
+ "fr" : "grec"
53
+ },
54
+ "Gujr" : {
55
+ "en" : "Gujarati",
56
+ "fr" : "goudjarâtî"
57
+ },
58
+ "Guru" : {
59
+ "en" : "Gurmukhi",
60
+ "fr" : "gourmoukhî"
61
+ },
62
+ "Hans" : {
63
+ "en" : "Simplified Han",
64
+ "fr" : "sinogrammes simplifiés"
65
+ },
66
+ "Hant" : {
67
+ "en" : "Traditional Han",
68
+ "fr" : "sinogrammes traditionnels"
69
+ },
70
+ "Hebr" : {
71
+ "en" : "Hebrew",
72
+ "fr" : "hébreu"
73
+ },
74
+ "Hmng" : {
75
+ "en" : "Pahawh Hmong",
76
+ "fr" : "pahawh hmong"
77
+ },
78
+ "Jpan" : {
79
+ "en" : "Japanese",
80
+ "fr" : "japonais"
81
+ },
82
+ "Kana" : {
83
+ "en" : "Katakana",
84
+ "fr" : "katakana"
85
+ },
86
+ "Khmr" : {
87
+ "en" : "Khmer",
88
+ "fr" : "khmer"
89
+ },
90
+ "Knda" : {
91
+ "en" : "Kannada",
92
+ "fr" : "kannara"
93
+ },
94
+ "Kore" : {
95
+ "en" : "Korean",
96
+ "fr" : "coréen"
97
+ },
98
+ "Laoo" : {
99
+ "en" : "Lao",
100
+ "fr" : "lao"
101
+ },
102
+ "Latn" : {
103
+ "en" : "Latin",
104
+ "fr" : "latin"
105
+ },
106
+ "Mand" : {
107
+ "en" : "Mandaean",
108
+ "fr" : "mandéen"
109
+ },
110
+ "Mlym" : {
111
+ "en" : "Malayalam",
112
+ "fr" : "malayalam"
113
+ },
114
+ "Mtei" : {
115
+ "en" : "Meitei Mayek",
116
+ "fr" : "meitei mayek"
117
+ },
118
+ "Mymr" : {
119
+ "en" : "Myanmar",
120
+ "fr" : "birman"
121
+ },
122
+ "Nkoo" : {
123
+ "en" : "N’Ko",
124
+ "fr" : "n’ko"
125
+ },
126
+ "Olck" : {
127
+ "en" : "Ol Chiki",
128
+ "fr" : "ol-chiki"
129
+ },
130
+ "Orya" : {
131
+ "en" : "Odia",
132
+ "fr" : "odia"
133
+ },
134
+ "Rjng" : {
135
+ "en" : "Rejang",
136
+ "fr" : "rejang"
137
+ },
138
+ "Rohg" : {
139
+ "en" : "Hanifi Rohingya",
140
+ "fr" : "hanifi"
141
+ },
142
+ "Sinh" : {
143
+ "en" : "Sinhala",
144
+ "fr" : "cingalais"
145
+ },
146
+ "Syrc" : {
147
+ "en" : "Syriac",
148
+ "fr" : "syriaque"
149
+ },
150
+ "Taml" : {
151
+ "en" : "Tamil",
152
+ "fr" : "tamoul"
153
+ },
154
+ "Telu" : {
155
+ "en" : "Telugu",
156
+ "fr" : "télougou"
157
+ },
158
+ "Tfng" : {
159
+ "en" : "Tifinagh",
160
+ "fr" : "tifinagh"
161
+ },
162
+ "Thaa" : {
163
+ "en" : "Thaana",
164
+ "fr" : "thâna"
165
+ },
166
+ "Tibt" : {
167
+ "en" : "Tibetan",
168
+ "fr" : "tibétain"
169
+ },
170
+ "Vaii" : {
171
+ "en" : "Vai",
172
+ "fr" : "vaï"
173
+ },
174
+ "Wcho" : {
175
+ "en" : "Wancho",
176
+ "fr" : "wantcho"
177
+ },
178
+ "Yiii" : {
179
+ "en" : "Yi",
180
+ "fr" : "yi"
181
+ }
182
+ }
@@ -229,15 +229,15 @@ module MistralTranslator
229
229
  end
230
230
  end
231
231
 
232
- # Avec 182 codes de deux lettres, presque toute chaîne est « proche » d'un code :
233
- # on suggère à partir d'un nom de langue (exact ou mal orthographié) ou d'un code
234
- # à trois lettres (ISO 639-2 comme "fre", ou faute de frappe comme "enn").
232
+ # Avec des centaines de codes de deux et trois lettres, presque toute chaîne est
233
+ # « proche » d'un code : on suggère à partir d'un nom de langue (exact, ambigu ou
234
+ # mal orthographié) ou d'un code à trois lettres (ISO 639-2 comme "fre").
235
235
  def find_locale_suggestions(invalid_locale)
236
236
  return [] unless invalid_locale.is_a?(String)
237
237
 
238
238
  input = invalid_locale.strip.downcase
239
- named = LocaleHelper.language_to_locale(input)
240
- return [named] if LocaleHelper.locale_supported?(named)
239
+ locale = Locale.find(input)
240
+ return [locale.to_s] if locale
241
241
 
242
242
  by_code = input.length == 3 ? LocaleHelper.supported_locales.select { |code| input.start_with?(code) } : []
243
243
  by_name = LocaleHelper::LANGUAGES.select do |_code, names|
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module MistralTranslator
6
+ # Une locale BCP 47 : une langue, et éventuellement une écriture et une région
7
+ # ("fr", "pt-BR", "zh-Hant-TW", "haw"). Objet valeur : deux locales aux mêmes
8
+ # sous-étiquettes sont égales. Chaque sous-étiquette est vérifiée contre les
9
+ # données CLDR de data/, générées par bin/generate_languages.
10
+ Locale = Data.define(:language, :script, :region)
11
+
12
+ class Locale
13
+ load_data = ->(name) { JSON.parse(File.read(File.expand_path("data/#{name}.json", __dir__)), freeze: true) }
14
+
15
+ LANGUAGES = load_data.call("languages")
16
+ SCRIPTS = load_data.call("scripts")
17
+ REGIONS = load_data.call("regions")
18
+
19
+ # Code ISO 639-3 des langues qui ont un code à deux lettres : "hat" → "ht"
20
+ ALPHA3 = LANGUAGES.filter_map { |code, names| [names["alpha3"], code] if names["alpha3"] }.to_h.freeze
21
+
22
+ # Noms français, anglais et natifs acceptés à la place du code. Un nom porté
23
+ # par plusieurs langues (« konkani ») est écarté : il faut alors le code.
24
+ NAMES = LANGUAGES
25
+ .flat_map { |code, names| names.values_at("fr", "en", "native").map { |name| [name.downcase, code] } }
26
+ .uniq
27
+ .group_by(&:first)
28
+ .filter_map { |name, entries| [name, entries.first.last] if entries.one? }
29
+ .to_h.freeze
30
+
31
+ # langue[-écriture][-région], séparées par "-" ou "_". Les variantes et les
32
+ # extensions ("ca-ES-valencia", "en-US-u-ca-gregory") ne sont pas acceptées.
33
+ TAG = /\A(?<language>[a-z]{2,3})(?:[-_](?<script>[a-z]{4}))?(?:[-_](?<region>[a-z]{2}|\d{3}))?\z/i
34
+
35
+ class << self
36
+ # Locale désignée par un code ou un nom de langue ; UnsupportedLanguageError sinon.
37
+ def parse(input)
38
+ find(input) || raise(UnsupportedLanguageError, input.to_s)
39
+ end
40
+
41
+ # Comme parse, mais renvoie nil pour une entrée inconnue.
42
+ def find(input)
43
+ return input if input.is_a?(Locale)
44
+
45
+ text = input.to_s.strip
46
+ from_tag(text) || from_name(text)
47
+ end
48
+
49
+ private
50
+
51
+ def from_tag(text)
52
+ return unless (match = TAG.match(text))
53
+
54
+ code = match[:language].downcase
55
+ language = LANGUAGES.key?(code) ? code : ALPHA3[code]
56
+ script = match[:script]&.capitalize
57
+ region = match[:region]&.upcase
58
+ new(language:, script:, region:) if language && known?(SCRIPTS, script) && known?(REGIONS, region)
59
+ end
60
+
61
+ def known?(table, code) = code.nil? || table.key?(code)
62
+
63
+ def from_name(text)
64
+ language = NAMES[text.downcase]
65
+ new(language:) if language
66
+ end
67
+ end
68
+
69
+ def initialize(language:, script: nil, region: nil)
70
+ super
71
+ end
72
+
73
+ # Forme canonique BCP 47 : "pt-BR", "zh-Hant-TW"
74
+ def to_s = [language, script, region].compact.join("-")
75
+
76
+ # Nom français suivi du code, pour les prompts : « portugais (Brésil) (pt-BR) »
77
+ def prompt_name
78
+ name = LANGUAGES.dig(language, "fr")
79
+ details = [SCRIPTS.dig(script, "fr"), REGIONS.dig(region, "fr")].compact
80
+ name = "#{name} (#{details.join(", ")})" if details.any?
81
+ "#{name} (#{self})"
82
+ end
83
+ end
84
+ end
@@ -1,45 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
-
5
3
  module MistralTranslator
4
+ # Fonctions historiques sur les locales, qui délèguent à Locale.
6
5
  module LocaleHelper
7
- # Codes ISO 639-1 et noms issus de CLDR, générés par bin/generate_languages
8
- LANGUAGES = JSON.parse(File.read(File.expand_path("data/languages.json", __dir__)), freeze: true)
6
+ # Langues CLDR, générées par bin/generate_languages
7
+ LANGUAGES = Locale::LANGUAGES
9
8
 
10
9
  # Nom de chaque langue dans sa propre langue
11
10
  SUPPORTED_LANGUAGES = LANGUAGES.transform_values { |names| names["native"].downcase }.freeze
12
11
 
13
- # Noms anglais et français acceptés à la place du code
14
- LANGUAGE_ALIASES = LANGUAGES.flat_map do |code, names|
15
- [[names["en"].downcase, code], [names["fr"].downcase, code]]
16
- end.to_h.freeze
12
+ # Noms acceptés à la place du code
13
+ LANGUAGE_ALIASES = Locale::NAMES
17
14
 
18
15
  class << self
19
16
  def locale_to_language(locale)
20
- locale_code = normalize_locale(locale)
21
- SUPPORTED_LANGUAGES[locale_code] || locale_code.to_s
17
+ found = Locale.find(locale)
18
+ found ? SUPPORTED_LANGUAGES[found.language] : locale.to_s
22
19
  end
23
20
 
24
- # Nom français suivi du code, pour les prompts : « anglais (en) »
21
+ # Nom français suivi du code, pour les prompts : « portugais (Brésil) (pt-BR) »
25
22
  def prompt_language_name(locale)
26
- locale_code = normalize_locale(locale)
27
- name = LANGUAGES.dig(locale_code, "fr")
28
- name ? "#{name} (#{locale_code})" : locale_code
23
+ Locale.find(locale)&.prompt_name || locale.to_s
29
24
  end
30
25
 
31
26
  def language_to_locale(language)
32
27
  normalized_language = language.to_s.downcase.strip
33
-
34
- # Vérifier d'abord les alias
35
- return LANGUAGE_ALIASES[normalized_language] if LANGUAGE_ALIASES.key?(normalized_language)
36
-
37
- # Recherche directe par valeur
38
- found_locale = SUPPORTED_LANGUAGES.find { |_, lang| lang.downcase == normalized_language }
39
- return found_locale[0] if found_locale
40
-
41
- # Retourne la langue telle quelle si pas trouvée
42
- normalized_language
28
+ LANGUAGE_ALIASES.fetch(normalized_language, normalized_language)
43
29
  end
44
30
 
45
31
  def supported_locales
@@ -51,16 +37,15 @@ module MistralTranslator
51
37
  end
52
38
 
53
39
  def locale_supported?(locale)
54
- SUPPORTED_LANGUAGES.key?(normalize_locale(locale))
40
+ !Locale.find(locale).nil?
55
41
  end
56
42
 
43
+ # Forme canonique de la locale ("pt-BR") ; UnsupportedLanguageError si elle est inconnue
57
44
  def validate_locale!(locale)
58
- normalized = normalize_locale(locale)
59
- raise UnsupportedLanguageError, normalized unless locale_supported?(normalized)
60
-
61
- normalized
45
+ Locale.parse(locale).to_s
62
46
  end
63
47
 
48
+ # Sous-étiquette de langue seule, sans vérification : "fr-FR" → "fr"
64
49
  def normalize_locale(locale)
65
50
  return "" if locale.nil? || locale.to_s.empty?
66
51
 
@@ -56,12 +56,13 @@ module MistralTranslator
56
56
  end
57
57
 
58
58
  # Fail-closed : une détection illisible ou une langue non supportée lève une erreur,
59
- # au lieu de supposer une langue et de renvoyer le texte non traduit.
59
+ # au lieu de supposer une langue et de renvoyer le texte non traduit. Seule la langue
60
+ # est gardée : un « fr-FR » détecté ne doit pas différer d'une cible « fr ».
60
61
  def parse_language_detection(response)
61
62
  data = JSON.parse(response.to_s)
62
63
  metadata = data["metadata"] if data.is_a?(Hash)
63
- detected = LocaleHelper.normalize_locale(metadata["detected_language"]) if metadata.is_a?(Hash)
64
- return detected if detected && LocaleHelper.locale_supported?(detected)
64
+ detected = Locale.find(metadata["detected_language"]) if metadata.is_a?(Hash)
65
+ return detected.language if detected
65
66
 
66
67
  raise InvalidResponseError.new("Language detection failed", response)
67
68
  rescue JSON::ParserError
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MistralTranslator
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
 
6
6
  # Informations additionnelles sur la gem
7
7
  API_VERSION = "v1"
@@ -3,6 +3,7 @@
3
3
  require_relative "mistral_translator/version"
4
4
  require_relative "mistral_translator/errors"
5
5
  require_relative "mistral_translator/configuration"
6
+ require_relative "mistral_translator/locale"
6
7
  require_relative "mistral_translator/locale_helper"
7
8
  require_relative "mistral_translator/prompt_helpers"
8
9
  require_relative "mistral_translator/prompt_builder"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mistral_translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Cherbero
@@ -160,11 +160,14 @@ files:
160
160
  - lib/mistral_translator/client_helpers.rb
161
161
  - lib/mistral_translator/configuration.rb
162
162
  - lib/mistral_translator/data/languages.json
163
+ - lib/mistral_translator/data/regions.json
164
+ - lib/mistral_translator/data/scripts.json
163
165
  - lib/mistral_translator/deprecated_options.rb
164
166
  - lib/mistral_translator/errors.rb
165
167
  - lib/mistral_translator/helpers.rb
166
168
  - lib/mistral_translator/helpers_extensions.rb
167
169
  - lib/mistral_translator/levenshtein_helpers.rb
170
+ - lib/mistral_translator/locale.rb
168
171
  - lib/mistral_translator/locale_helper.rb
169
172
  - lib/mistral_translator/logger.rb
170
173
  - lib/mistral_translator/prompt.rb