Can I conditionally render elements of Liquid Shopify template depending on the localization Language? - localization

Im updating a Shopify site to display in English and Japanese languages.
I used the 'Translate and Adapt' Shopify app and have fixed up the questionable auto translations to a point where I'm mostly happy with them.
There is one problem. I have product-thumbnails that have a minimum price.
In English the format 'from ¥900' is fine but in Japanese I want it to display as '¥900 ~' so I'd want to conditionally change the display order of the products.general.from string variable and the price variable.
I've found the code in product-thumbnail.liquid that conditionally shows the value of products.general.from;
{% if product.price_varies and product.price_min > 0 %}
<small><em>{{ 'products.general.from' | t }}</em></small>
{% endif %}
Inspecting the elements in Chrome shows that the lang and data-current-lang are set to 'ja'.
Is there a way to access the value of data-current-lang in the liquid template in order to conditionally render a different version of the code-block above if the language is set to 'ja'?

You can use the request object to get the current language code.
{% if request.locale.iso_code == 'en' %}
// English language.
{% elsif request.locale.iso_code == 'ja' %}
// Japanese language
{% endif %}

Related

testing a frontmatter date in eleventy

I would like to control publication of articles in 11ty by testing the frontmatter date. How do set that up?
---
pubslish_after: 2023-02-01
---
Should be tested by
{% if collectionItem.publish_after <= <??? how do i specifiy today here> %}
...
{% endif %}

Fastapi + jinja2 : how to check if user is authenticated in jinja to template conditions

How can we check is user is authenticated and populate any value
'''{% if user.is_authenticated %}{{'Hello'}}{% endif %}'''
After setting up AuthenticationMiddleware like in the documentation, you can access Request.user.is_authenticated, which should be a boolean.
The 'request' variable should be passed to your Jinja template.
In your Jinja template, use an if block to display 'Hello' if the user is authenticated.
{% if request.user.is_authenticated %}
<p>Hello</p>
{% endif %}

Get all tags used by a Shopify shop in one request

When I try this URL in my browser:
https://USERNAME:PASSWORD#SHOP.myshopify.com/admin/products/tags.json
It gives me a nice list of all the tags in my shop.
I need this list, but when I try an API call with Python it says 'Not found'. Since I can do it with my browser, there must be a way to do it programmatically.
Getting all the tags by looping over all products is too long (+- 5-10 seconds), whereas this gives me everything I need instantly.
How can I make this request?
Thanks
Currently, Shopify Rest API still doesn't have an endpoint to get all tags, vendors, types. And GraphQL Admin API can only get at maximum first 250 results.
You can get all of them through Shopify liquid ( Not working with the passworded store ).
Create a new search template name: search.foo.liquid
{% layout none %}
{
"vendors" : {{shop.vendors|json}},
"types" : {{shop.types|json}},
"tags" : {% capture output %}
{% for collection in collections %}
{% if forloop.index == 1 %}""{% endif %}
{% if collection.all_tags.size > 0 %}
{% for tag in collection.all_tags %}
{% if output contains tag %}
{% else %}
,"{{tag}}"
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endcapture %}
{{ output | strip_newlines | prepend: '[' | append: ']' }}
}
Access: https://your-store.myshopify.com/search.json?view=foo in browser to view the result, Use cURL to get it in your application.
The tags aren't exposed via the REST API, but they're accessible via the GraphQL Admin API:
{
shop{
productTags(first: 100){
edges{
node
}
}
}
}
Check this for extra info

October CMS how to get current locale name?

Could you tell me please how i can get current language name or somthing like this.
I want customize Locale Switcher on web site based on October CMS.
It will be greate to recevie something like
...
{{ set var = ****.getLocale();}}
...
then use it for switch($var){}
...
In twig you can access the current language with {{ activeLocale }}, the full language name with {{ activeLocaleName }} and an array with all locales available with {{ locales }}.
You could use {{ dump() }} to see all variables available on a page. If you try it you will find the locale variables right there as well.
i tried with activeLocale and also with locales array but one can not stop the loop and thus it was impossible for me to render some content conditionally. this is how i solved it, after reading the logic of plugin that how it is working, I did this in one of my partials.
==
use RainLab\Translate\Classes\Translator;
protected $translator;
function onStart()
{
$this->translator = Translator::instance();
$this['SelectedLanguage'] = $this->activeLocale = $this->translator->getLocale();
}
==
{% set CurrentLanguage = SelectedLanguage %}
now {{CurrentLanguage}} will give me code for current language so now using twig i can do some conditional rendering like this
{% if CurrentLanguage is same as('en') %}{% endif %}
{% if CurrentLanguage is same as('tr') %}{% endif %}
{% if CurrentLanguage is same as('gr') %}{% endif %}
Maybe there could be a another solution. but this one worked like charm.
Update:
Although, In case of components or elsewhere one can use session to retrieve the current language this way,
Session::get('rainlab.translate.locale')
Assuming that you use RainLab.Translate plugin?
Create a partial that uses the localePicker component and then use your custom code:
<div>{{ activeLocale }} - {{ activeLocaleName }}</div>
{% for code, name in localePicker.locales %}
<div>{{ code }} - {{ name }}</div>
{% endfor %}
And just call that partial from where you want to use it.

Can I pass a variable from a Jekyll template to an include then render data with that variable?

In foo.html (a post), I have the following:
{% assign table_name="application" %}
{% include table.html %}
This assignment appears to work fine in table.html
{% assign tableName = table_name %}
<p>table name is: {{ tableName }}</p> # renders "table name is: application"
Now, I'm trying to sort through an array of data that I have defined in config.yml by doing the following:
{% for header in site.table.tableName.headers %}
<th>{{ header }}</th>
{% endfor %}
This gives me no output whatsoever.
If I change the for statement to include the variable's content, not the variable, it works fine.
{% for header in site.table.application.headers %}
This leads me to believe that it's not a problem with my array but that it's either a shortcoming of Jekyll, a bug in Jekyll, or I'm not accurately constructing my statements.
Any idea how I could make this work?
Looks like this can be done. I had to think about it more programmatically. What seems to be happening is that Jekyll was expecting an object and I was feeding it a string.
{% assign tableName = "aStringName" %}
{% include table.html %}
so,
# in _includes/table.html
{% for header in site.table.tableName.headers %}
was being interpreted as
{% for header in site.table."aStringName".headers %}
When I switched to bracket notation for the object, it was perfect.
Final result:
{% for header in site.table[tableName].headers %}
or, as Jekyll sees it,
{% for header in site.table['aStringName'].headers %}

Resources