Class: MyLocaleManager

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/my_locale_manager.rb

Overview

Handle provided locale on app the first defined locale will be default locale

Constant Summary collapse

EN =
:en
TH =
:th
CN =
:cn
ES =
:es
FR =
:fr
DE =
:de
RU =
:ru
MS =
:ms
KO =
:ko
JA =
:ja
ID =
:id
VI =
:vi
ZH =

Simplified Chinese (existing CN is Traditional)

:zh
AVAILABLE_LOCALES =

Available locales constant for better discoverability and maintainability

[EN, TH, CN, ES, FR, DE, RU, MS, KO, JA, ID, VI, ZH].freeze
REQUIRED_TRANSLATIONS =
[EN, TH]
AVAILABLE_LOCALES_FOR_EMAIL =

Email template availability - only EN and TH templates exist for most mailers Some mailers like notify_points_expiration also have CN template

[EN, TH].freeze
AVAILABLE_LOCALES_FOR_EMAIL_WITH_CN =
[EN, TH, CN].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMyLocaleManager

Returns a new instance of MyLocaleManager.



31
32
33
34
35
36
37
# File 'app/my_lib/my_locale_manager.rb', line 31

def initialize
  # Order matters: first locale becomes default
  # Existing locales kept at beginning to avoid changing default (EN)
  # CN currently mapped to Traditional Chinese (繁體中文)
  # Added locales: es, fr, de, ru, ms, ko, ja, id, vi, zh (Simplified Chinese)
  @available_locales = AVAILABLE_LOCALES
end

Instance Attribute Details

#available_localesObject (readonly)

Returns the value of attribute available_locales.



6
7
8
# File 'app/my_lib/my_locale_manager.rb', line 6

def available_locales
  @available_locales
end

Class Method Details

.available_locale_with_namesObject



111
112
113
114
115
116
117
118
119
# File 'app/my_lib/my_locale_manager.rb', line 111

def available_locale_with_names
  instance = new
  instance.human_names.map do |k, v|
    {
      id: k,
      name: v,
    }
  end
end

.normalize_localeObject



86
87
88
89
90
91
92
# File 'app/my_lib/my_locale_manager.rb', line 86

def normalize_locale
  instance = new
  instance.available_locales.each do |locale|
    return locale.to_sym if I18n.locale.to_s.include?(locale.to_s)
  end
  instance.default_locale
end

.parse(param) ⇒ Object



94
95
96
97
98
99
100
101
# File 'app/my_lib/my_locale_manager.rb', line 94

def parse(param)
  parsed_locale = param.to_s.downcase
  instance = new
  instance.available_locales.each do |locale|
    return locale.to_sym if parsed_locale.include?(locale.to_s)
  end
  instance.default_locale
end

.to_culture_nameObject



103
104
105
106
107
108
109
# File 'app/my_lib/my_locale_manager.rb', line 103

def to_culture_name
  instance = new
  nl = normalize_locale
  return instance.culture_names[nl] if instance.culture_names.key?(nl)

  instance.default_culture_name
end

Instance Method Details

#culture_namesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/my_lib/my_locale_manager.rb', line 39

def culture_names
  {
    en: 'en_US',
    th: 'th_TH',
    cn: 'zh-HK',   # Traditional Chinese (HK)
    es: 'es-ES',
    fr: 'fr-FR',
    de: 'de-DE',
    ru: 'ru-RU',
    ms: 'ms-MY',
    ko: 'ko-KR',
    ja: 'ja-JP',
    id: 'id-ID',
    vi: 'vi-VN',
    zh: 'zh-CN',   # Simplified Chinese (Mainland)
  }
end

#default_culture_nameObject



75
76
77
# File 'app/my_lib/my_locale_manager.rb', line 75

def default_culture_name
  culture_names[default_locale]
end

#default_localeObject



79
80
81
# File 'app/my_lib/my_locale_manager.rb', line 79

def default_locale
  available_locales.first
end

#human_namesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/my_lib/my_locale_manager.rb', line 57

def human_names
  {
    en: 'English',
    th: 'ภาษาไทย',
    cn: '繁體中文',          # Traditional Chinese
    es: 'Español',
    fr: 'Français',
    de: 'Deutsch',
    ru: 'Русский',
    ms: 'Bahasa Melayu',
    ko: '한국어',
    ja: '日本語',
    id: 'Bahasa Indonesia',
    vi: 'Tiếng Việt',
    zh: '简体中文',          # Simplified Chinese
  }
end