Giter VIP home page Giter VIP logo

Comments (8)

solaluset avatar solaluset commented on July 18, 2024 1

Thanks for opening the issue. I agree that this can be a useful feature.

from i18nice.

solaluset avatar solaluset commented on July 18, 2024 1

It's already covered by tests and described in the readme. You probably got confused because the library uses placeholder delimiter (defaults to %) for placeholders (and now static refs) and namespace delimiter (defaults to .) for keys.

To create a static reference, you put it as key prefixed with namespace delimiter ("%{.DUMMY_1}, and %{.DUMMY_2} with %{placeholder_2}" in your case)

Let me know if you have any suggestions for description improvement, because I'm kind of bad when it comes to explaining things ;)

from i18nice.

solaluset avatar solaluset commented on July 18, 2024 1

New version is released 🎉
Thanks for your input. In case if something doesn't work as intended, please open a new issue

from i18nice.

zyf722 avatar zyf722 commented on July 18, 2024

I have created a simple wrapper class to address this issue.

import i18n
from string import Template

i18n.set('file_format', 'json')
i18n.set('filename_format', '{locale}.{format}')
i18n.load_path.append('./i18n/')

class KeyRefFormatter(Template):
    delimiter = "$"
    
    def __init__(self, template, **kwargs):
        template = i18n.t(template, **kwargs)
        super().__init__(template)
        self.kwargs = kwargs
    
    # From https://github.com/python/cpython/issues/90465
    # For Python 3.11+, use library method string.Template.get_identifiers() instead
    def get_identifiers(self):
        return list(
            set(
                filter(
                    lambda v: v is not None,
                    (mo.group('named') or mo.group('braced') 
                     for mo in self.pattern.finditer(self.template))
                )
            )
        )
        
    def format(self):
        sub_dict = { key: i18n.t(key, **self.kwargs) for key in self.get_identifiers() }
        return self.safe_substitute(sub_dict)

def _(key, **kwargs):
    return KeyRefFormatter(key, **kwargs).format()

print(_('MENU_A_DUMMY', placeholder_1="def", placeholder_2="abc"))
# Output: dummy1 with def, and dummy2 with abc

The corresponding en.json file is as follows:

{
    "en": {
        "DUMMY_1": "dummy1 with %{placeholder_1}",
        "DUMMY_2": "dummy2",
        "MENU_A_DUMMY": "${DUMMY_1}, and ${DUMMY_2} with %{placeholder_2}",
        "MENU_B_DUMMY": "${DUMMY_1}"
    }
}

This is a workaround and a temporary solution. Perhaps it can be further integrated into this project in the future?

from i18nice.

solaluset avatar solaluset commented on July 18, 2024

You can take a look at my pull request and share your thoughts.

from i18nice.

zyf722 avatar zyf722 commented on July 18, 2024

Looks good and thanks for your work!

Let me confirm, so the static reference feature and placeholder feature share the delimiter %, right?

Additionally, could you explain the behavior of the feature when the referenced value contains a placeholder?

For instance, in my example above, MENU_A_DUMMY includes the placeholder %{placeholder_2} and also references DUMMY_1, which itself contains the placeholder %{placeholder_1}. In this case, would i18n.t('MENU_A_DUMMY', placeholder_1="def", placeholder_2="abc") work as expected? Perhaps this could be added as a test case.

from i18nice.

zyf722 avatar zyf722 commented on July 18, 2024

Oh, I figure it out now. It would be great if everything works just as expected! 👏
The description is just fine and thank you for the update. Looking forward to the new version being released on PyPI!

from i18nice.

solaluset avatar solaluset commented on July 18, 2024

I'll keep this open until the new release is published

from i18nice.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.