{"record":{"id":"2433ab1098fa6bd7","repo":"ViewComponent/view_component","slug":"translate-can-t-be-used-before-rendering-as-it","errorCode":null,"errorMessage":"`#translate` can't be used before rendering as it depends on the view context that only exists once a ViewComponent is passed to the Rails render pipeline.\n\nIt's sometimes possible to fix this issue by moving code dependent on `#translate` to a [`#before_render` method](https://viewcomponent.org/api.html#before_render--void).","messagePattern":"`#translate` can't be used before rendering as it depends on the view context that only exists once a ViewComponent is passed to the Rails render pipeline\\.\n\nIt's sometimes possible to fix this issue by moving code dependent on `#translate` to a \\[`#before_render` method\\]\\(https://viewcomponent\\.org/api\\.html#before_render--void\\)\\.","errorType":"exception","errorClass":"ViewComponent::TranslateCalledBeforeRenderError","httpStatus":null,"severity":"error","filePath":"lib/view_component/translatable.rb","lineNumber":94,"sourceCode":"      # Ensure the Simple backend won't load paths from ::I18n.load_path\n      def load_translations\n        super(@__vc_load_paths)\n      end\n\n      def scope_data(data)\n        @__vc_i18n_scope.reverse_each do |part|\n          data = {part => data}\n        end\n        data\n      end\n\n      def store_translations(locale, data, options = EMPTY_HASH)\n        super(locale, scope_data(data), options)\n      end\n    end\n\n    def translate(key = nil, **options)\n      raise ViewComponent::TranslateCalledBeforeRenderError if view_context.nil?\n\n      return @view_context.translate(key, **options) unless __vc_i18n_backend\n      return key.map { |k| translate(k, **options) } if key.is_a?(Array)\n\n      locale = options.delete(:locale) || ::I18n.locale\n      key = self.class.__vc_i18n_key(key, options.delete(:scope))\n      as_html = HTML_SAFE_TRANSLATION_KEY.match?(key)\n\n      __vc_html_escape_translation_options!(options) if as_html\n\n      if key.start_with?(__vc_i18n_scope + \".\")\n        translated =\n          catch(:exception) do\n            __vc_i18n_backend.translate(locale, key, options)\n          end\n\n        # Fallback to the global translations\n        if translated.is_a? ::I18n::MissingTranslation","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/ViewComponent/view_component/blob/9f22c36fa7d7b44098a8ed97472e3b4674ae420f/lib/view_component/translatable.rb#L76-L112","documentation":"Error \"`#translate` can't be used before rendering as it depends on the view context that only exists once a ViewComponent is passed to the Rails render pipeline.\n\nIt's sometimes possible to fix this issue by moving code dependent on `#translate` to a [`#before_render` method](https://viewcomponent.org/api.html#before_render--void).\" thrown in ViewComponent/view_component.","triggerScenarios":"Thrown at lib/view_component/translatable.rb:94 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Move code that calls `#translate`/`#t` into a `#before_render` method or the template, where the view context exists.","If a translation is needed at initialization time, call `I18n.t` directly with an explicit scope instead of the component's `#translate` helper."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"9f22c36fa7d7b44098a8ed97472e3b4674ae420f","analyzedAt":"2026-08-23T10:20:47.008Z","schemaVersion":2},"datasetVersion":"2026-08-23T13:39:53.451Z"}